0. Packages and functions

## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## Registered S3 method overwritten by 'rvest':
##   method            from
##   read_xml.response xml2
## ?? Attaching packages ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? tidyverse 1.2.1 ??
## ? ggplot2 3.1.1       ? purrr   0.3.2  
## ? tibble  2.1.1       ? dplyr   https://protect-eu.mimecast.com/s/uw71CWP0PfzpqRJuzlzZs?domain=0.8.0.1
## ? tidyr   0.8.3       ? stringr 1.4.0  
## ? readr   1.3.1       ? forcats 0.4.0
## ?? Conflicts ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? tidyverse_conflicts() ??
## ? dplyr::filter() masks stats::filter()
## ? dplyr::lag()    masks stats::lag()
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following object is masked from 'package:tidyr':
## 
##     expand
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## Loading required package: grid
## Loading required package: magrittr
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d
require(ggplot2)

# check and create folder
folder.check <- function(folder.name = NULL, current.dir) {
  setwd(current.dir)
  if (file.exists(folder.name)) {
    return(setwd(file.path(current.dir, folder.name)))
  } else {
    dir.create(file.path(current.dir, folder.name))
    return(setwd(file.path(current.dir, folder.name)))
  }
}

cat.cont.graph <- function(cat.table, cont.table, cat.name, cont.name) {
  temp.df <- data.frame(cat.name = cat.table[cat.name], cont.name = cont.table[cont.name])
  temp.df <- na.omit(temp.df)
  formula <- as.formula(paste0(cont.name, " ~ ", cat.name))
  m <- aggregate(formula, temp.df, mean)
  colnames(m)[colnames(m) %in% cont.name] = "mean"
  sd <- aggregate(temp.df[cont.name], temp.df[cat.name], sd)
  colnames(sd)[colnames(sd) %in% cont.name] = "sd"
  table <- merge(m, sd)

  if (nrow(table) == 2) {
  ttest <- t.test(cont.table[,cont.name]~cat.table[,cat.name])
  pval <- round(ttest$p.value, 4)
  stat <- round(ttest$statistic, 2)
  df <- round(ttest$parameter, 2)
  } else {
    pval <- ""
    stat <- ""
    df <- ""
  }
  k <- ggplot2::ggplot(table, aes_(as.name(cat.name), as.name("mean"), fill = as.name(cat.name))) + 
    geom_bar(stat = "identity") +
    geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), width = 0.2) +
    labs(x = cat.name, y = cont.name, 
         title = paste0(cont.name, " ~ ", cat.name), 
         subtitle = paste0("t[df=",df,"] = ", stat, ", p-value = ", pval)) +
    theme_bw(base_size = 16) +
    theme(axis.text=element_text(size=14),
          axis.title=element_text(size=16, face="bold"),
          plot.margin = margin(5,5,5,5, "mm"))
  
  return(k)
}

1. Data upload to R

## Parsed with column specification:
## cols(
##   id = col_double(),
##   calories_7d = col_double(),
##   aminoAcid_7d = col_double(),
##   protein_fullEF = col_double(),
##   calories_fullEF = col_double(),
##   Abx_days = col_double(),
##   birth_wt = col_double()
## )
## Warning: The following named parsers don't match the column names: sga
## Parsed with column specification:
## cols(
##   id = col_double(),
##   time_week = col_double(),
##   wt_z_score = col_double()
## )
## Parsed with column specification:
## cols(
##   id = col_double(),
##   maternal_age = col_double(),
##   maternal_race = col_character(),
##   antenatal_steroid = col_double(),
##   delivery_method = col_double(),
##   maternal_obesity = col_double(),
##   maternal_smoking = col_double()
## )
## Parsed with column specification:
## cols(
##   id = col_double(),
##   gender = col_character(),
##   apgar_1min = col_double(),
##   apgar_5min = col_double(),
##   ga_birth = col_double(),
##   birth_weight = col_double(),
##   small_for_ga = col_double()
## )
##        id          calories_7d     aminoAcid_7d   protein_fullEF 
##  Min.   :  1.00   Min.   :25.43   Min.   :1.540   Min.   :1.360  
##  1st Qu.: 27.50   1st Qu.:46.43   1st Qu.:2.950   1st Qu.:3.385  
##  Median : 50.00   Median :53.29   Median :3.100   Median :3.580  
##  Mean   : 50.65   Mean   :53.47   Mean   :3.056   Mean   :3.479  
##  3rd Qu.: 75.50   3rd Qu.:61.07   3rd Qu.:3.245   3rd Qu.:3.735  
##  Max.   :100.00   Max.   :77.00   Max.   :3.650   Max.   :4.330  
##  calories_fullEF    Abx_days        birth_wt      
##  Min.   : 72.4   Min.   : 0.00   Min.   :-2.3500  
##  1st Qu.:100.8   1st Qu.: 4.00   1st Qu.:-0.9950  
##  Median :104.3   Median : 9.00   Median :-0.3300  
##  Mean   :104.6   Mean   :11.63   Mean   :-0.4263  
##  3rd Qu.:109.8   3rd Qu.:17.00   3rd Qu.: 0.1350  
##  Max.   :120.6   Max.   :57.00   Max.   : 1.7800
##        id         pla_insuff    Gender    BPD      IVH     Sepsis  
##  Min.   :  1.00   No :57     male  :40   No :30   No :83   No :73  
##  1st Qu.: 27.50   Yes:34     female:51   Yes:61   Yes: 8   Yes:18  
##  Median : 50.00                                                    
##  Mean   : 50.65                                                    
##  3rd Qu.: 75.50                                                    
##  Max.   :100.00                                                    
##   ROP         SGA           
##  No :71   Length:91         
##  Yes:20   Class :character  
##           Mode  :character  
##                             
##                             
## 
##        id          maternal_age   maternal_race      antenatal_steroid
##  Min.   :  1.00   Min.   :15.00   Length:91          Min.   :0.000    
##  1st Qu.: 27.50   1st Qu.:23.00   Class :character   1st Qu.:1.000    
##  Median : 50.00   Median :27.00   Mode  :character   Median :1.000    
##  Mean   : 50.65   Mean   :27.93                      Mean   :0.978    
##  3rd Qu.: 75.50   3rd Qu.:32.00                      3rd Qu.:1.000    
##  Max.   :100.00   Max.   :43.00                      Max.   :1.000    
##  delivery_method  maternal_obesity maternal_smoking
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :1.0000   Median :0.0000   Median :0.0000  
##  Mean   :0.7802   Mean   :0.0989   Mean   :0.3297  
##  3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000
##        id            gender            apgar_1min      apgar_5min   
##  Min.   :  1.00   Length:91          Min.   :1.000   Min.   :2.000  
##  1st Qu.: 27.50   Class :character   1st Qu.:3.000   1st Qu.:6.000  
##  Median : 50.00   Mode  :character   Median :5.000   Median :7.000  
##  Mean   : 50.65                      Mean   :4.725   Mean   :6.857  
##  3rd Qu.: 75.50                      3rd Qu.:6.000   3rd Qu.:8.000  
##  Max.   :100.00                      Max.   :9.000   Max.   :9.000  
##     ga_birth      birth_weight      small_for_ga   
##  Min.   :23.14   Min.   :-2.3500   Min.   :0.0000  
##  1st Qu.:25.14   1st Qu.:-0.9950   1st Qu.:0.0000  
##  Median :26.29   Median :-0.3250   Median :0.0000  
##  Mean   :26.37   Mean   :-0.4261   Mean   :0.1868  
##  3rd Qu.:27.50   3rd Qu.: 0.1345   3rd Qu.:0.0000  
##  Max.   :31.57   Max.   : 1.7780   Max.   :1.0000
##        id           time_week       wt_z_score    
##  Min.   :  1.00   Min.   :24.00   Min.   :-4.530  
##  1st Qu.: 28.00   1st Qu.:30.86   1st Qu.:-2.060  
##  Median : 51.00   Median :35.00   Median :-1.420  
##  Mean   : 50.73   Mean   :35.50   Mean   :-1.544  
##  3rd Qu.: 74.00   3rd Qu.:39.43   3rd Qu.:-0.900  
##  Max.   :100.00   Max.   :50.00   Max.   : 1.430

2. Correlation analysis

## png 
##   2

## png 
##   2

## png 
##   2
## $calories_7d

## 
## $aminoAcid_7d

## 
## $protein_fullEF

## 
## $calories_fullEF

## 
## $Abx_days

## 
## $birth_wt

3. Spaghetti plot

## png 
##   2
## $pla_insuff

## 
## $Gender

## 
## $BPD

## 
## $IVH

## 
## $Sepsis

## 
## $ROP

## 
## $SGA

5. Mixed-effects modeling with lme4 without considering covariates

## Loading required package: kableExtra
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
long.df.scale <- long.df
long.df.scale[,"time_week"] <- as.numeric(long.df.scale[,"time_week"])
long.df.scale[,"time_week"] <- long.df.scale[,"time_week"] - shift.constant

formula_list <- list()

formula_list[[1]] <- "wt_z_score ~ 1 + time_week + (time_week | id)"
formula_list[[2]] <- "wt_z_score ~ 1 + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[3]] <- "wt_z_score ~ pla_insuff + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[4]] <- "wt_z_score ~ pla_insuff + pla_insuff:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[5]] <- "wt_z_score ~ pla_insuff + pla_insuff:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[6]] <- "wt_z_score ~ pla_insuff + pla_insuff:time_week + pla_insuff:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[7]] <- "wt_z_score ~ birth_weight + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[8]] <- "wt_z_score ~ birth_weight + birth_weight:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[9]] <- "wt_z_score ~ birth_weight + birth_weight:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[10]] <- "wt_z_score ~ birth_weight + birth_weight:time_week + birth_weight:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[11]] <- "wt_z_score ~ SGA + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[12]] <- "wt_z_score ~ SGA + SGA:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[13]] <- "wt_z_score ~ SGA + SGA:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"
formula_list[[14]] <- "wt_z_score ~ SGA + SGA:time_week + SGA:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id)"

summary <- list()
basic_analysis <- list()

for (i in seq_along(formula_list)) {
  formula <- as.formula(formula_list[[i]])
  lmer.analysis <- lmerTest::lmer(formula, 
                                  data = long.df.scale, 
                                  REML = FALSE,
                                  control = lme4::lmerControl(optimizer = "optimx", 
                                                              calc.derivs = FALSE,
                                                              optCtrl = list(method = "nlminb"))
 )
  summary[[i]] <- list(as.character(formula(lmer.analysis)),
                       as.numeric(summary(lmer.analysis)$AICtab["AIC"]),
                       as.numeric(summary(lmer.analysis)$AICtab["BIC"]),
                       as.numeric(summary(lmer.analysis)$AICtab["logLik"]),
                       as.numeric(summary(lmer.analysis)$AICtab["deviance"]),
                       as.numeric(summary(lmer.analysis)$AICtab["df.resid"]))
  basic_analysis[[i]] <- lmer.analysis
}

basic_AIC <- data.frame()
for (i in seq_along(1:length(formula_list))) {
  summary[[i]][[1]] <- as.character(summary[[i]][[1]])
  summary[[i]][[1]] <- paste(summary[[i]][[1]][2], summary[[i]][[1]][1], summary[[i]][[1]][3])
  for (j in seq_along(1:length(summary[[i]]))) {
    basic_AIC[i,j] <- summary[[i]][[j]]
  }
}

colnames(basic_AIC) <- c("formula", "AIC", "BIC", "logLik", "deviance", "df_resid")

folder.check(folder.name = "Results", getwd())
folder.check(folder.name = "mixed_effects_modeling", getwd())

write_csv(basic_AIC, "basic_analysis.csv")

#for html markdown
kable(basic_AIC[c(1,2,3,4,5,6),c(1:3)], row.names = FALSE) %>%
  add_header_above(c("Placental insufficiency" = 3)) %>%
  kable_styling(bootstrap_options = "striped", full_width = T)
Placental insufficiency
formula AIC BIC
wt_z_score ~ 1 + time_week + (time_week | id) 1330.3420 1361.9471
wt_z_score ~ 1 + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 713.6808 766.3561
wt_z_score ~ pla_insuff + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 689.4996 747.4423
wt_z_score ~ pla_insuff + pla_insuff:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 679.4182 742.6285
wt_z_score ~ pla_insuff + pla_insuff:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 690.8984 754.1087
wt_z_score ~ pla_insuff + pla_insuff:time_week + pla_insuff:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 681.0658 749.5437
Birth weight
formula AIC BIC
wt_z_score ~ 1 + time_week + (time_week | id) 1330.3420 1361.9471
wt_z_score ~ 1 + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 713.6808 766.3561
wt_z_score ~ birth_weight + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 645.5346 703.4774
wt_z_score ~ birth_weight + birth_weight:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 637.6573 700.8676
wt_z_score ~ birth_weight + birth_weight:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 646.5258 709.7361
wt_z_score ~ birth_weight + birth_weight:time_week + birth_weight:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 638.9894 707.4673
Small-for-gestataional age
formula AIC BIC
wt_z_score ~ 1 + time_week + (time_week | id) 1330.3420 1361.9471
wt_z_score ~ 1 + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 713.6808 766.3561
wt_z_score ~ SGA + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 680.7296 738.6724
wt_z_score ~ SGA + SGA:time_week + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 675.9549 739.1652
wt_z_score ~ SGA + SGA:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 680.1556 743.3659
wt_z_score ~ SGA + SGA:time_week + SGA:I(time_week^2) + time_week + I(time_week^2) + (time_week + I(time_week^2) | id) 676.1992 744.6770

6. Mixed-effects modeling with lme4 considering covariates

## Loading required package: foreach
## 
## Attaching package: 'foreach'
## The following objects are masked from 'package:purrr':
## 
##     accumulate, when
## Loading required package: doParallel
## Loading required package: iterators
## Loading required package: parallel
require(optimx)
require(parallel)
require(kableExtra)

basic_variables_pla <- c("id", "time_week", "wt_z_score", "pla_insuff")
basic_variables_bw <- c("time_week", "wt_z_score", "birth_wt")
nutrition_variables <- c("calories_7d", "aminoAcid_7d", "protein_fullEF", "calories_fullEF")
comorbidity_variables <- c("BPD", "IVH", "Sepsis", "ROP", "Abx_days")
demographics_variables <- c("Gender", "maternal_obesity", "maternal_smoking", "maternal_race")

long.df.scale_nutrition <- long.df.scale[,c(basic_variables_pla, nutrition_variables)]
long.df.scale_comorbidity <- long.df.scale[,c(basic_variables_pla, comorbidity_variables)]
long.df.scale_demographics <- long.df.scale[,c(basic_variables_pla, demographics_variables)]

fm <- formula_list[[4]]

## construct all possible combinations of covariates
var_list_demographics <- data.frame()
var_list_comorbidity <- data.frame()
var_list_nutrition <- data.frame()

fit <- function(variables, data) {
  var_list <- data.frame()
  for (i in seq_along(1:length(variables))) {
    v <- list()
    v <- combn(variables, i, simplify = TRUE)
    v <- t(v)
    v <- as.data.frame(v)
    v <- unite(v, variables, sep = " + ")
    var_list <- rbind(var_list, v)
  }

  for (i in seq_along(1:length(var_list$variables))) {
    formula <- str_replace(fm, "pla_insuff \\+ pla_insuff\\:time_week \\+ ", paste0(var_list$variables[i], " + pla_insuff \\+ pla_insuff\\:time_week \\+ "))
   var_list$formula[i] <- formula
  }

  var_list <- rbind(c("", fm), var_list)

  cl <- makePSOCKcluster(detectCores())
  registerDoParallel(cl)

  fit.all <- list()
  fit.all <- foreach (i = seq_along(1:nrow(var_list)),
                    .combine = rbind,
                    .packages = c("lmerTest", "lme4", "optimx")) %dopar% {
                      formula <- var_list$formula[i]
                      formula.formula <- as.formula(formula)
                      fit <- lmerTest::lmer(formula.formula, 
                                            data=data, 
                                            REML=F,
                                            control = lmerControl(optimizer = "optimx", calc.derivs = FALSE, 
                                                                  optCtrl = list(method = "nlminb")))
                      return(list(formula(fit),
                                  as.numeric(summary(fit)$AICtab["AIC"]),
                                  as.numeric(summary(fit)$AICtab["BIC"]),
                                  as.numeric(summary(fit)$AICtab["logLik"]),
                                  as.numeric(summary(fit)$AICtab["deviance"]),
                                  as.numeric(summary(fit)$AICtab["df.resid"]),
                                  summary(fit)$coefficient,
                                  fit))
                    }

for (i in seq_along(1:nrow(var_list))) {
  fit.all[[i]] <- as.character(fit.all[[i]])
  fit.all[[i]] <- paste(fit.all[[i]][2], fit.all[[i]][1],fit.all[[i]][3])
}
fit.all <- as.data.frame(fit.all)
colnames(fit.all) <- c("formula", "AIC", "BIC", "logLik", "deviance", "df_resid", "coefficient", "results")
fit.all.results <- fit.all$results
fit.all$results <- NULL
fit.all$formula <- as.character(fit.all$formula)

var_list <- left_join(var_list, fit.all, by = c("formula" = "formula"))
var_list$AIC <- unlist(var_list$AIC)
var_list$BIC <- unlist(var_list$BIC)
var_list$logLik <- unlist(var_list$logLik)
var_list$deviance <- unlist(var_list$deviance)
var_list$df_resid <- unlist(var_list$df_resid)

stopCluster(cl)

var_list <- rownames_to_column(var_list, var = "id")
var_list$id <- as.numeric(var_list$id)

return(list(var_list, fit.all.results))
}

fit_nutrition <- fit(nutrition_variables, long.df.scale_nutrition)
fit_comorbidity <- fit(comorbidity_variables, long.df.scale_comorbidity)
fit_demographics <- fit(demographics_variables, long.df.scale_demographics)

nutrition_AIC <- fit_nutrition[[1]]
comorbidity_AIC <- fit_comorbidity[[1]]
demographics_AIC <- fit_demographics[[1]]

nutrition_results <- fit_nutrition[[2]]
comorbidity_results <- fit_comorbidity[[2]]
demographics_restuls <- fit_demographics[[2]]

rm(fit_nutrition)
rm(fit_comorbidity)
rm(fit_demographics)

folder.check(folder.name = "Results", getwd())
folder.check(folder.name = "mixed_effects_modeling", getwd())

write_csv(nutrition_AIC[,c(1:8)], "nutrition_covariates.csv")
write_csv(comorbidity_AIC[,c(1:8)], "comorbidity_covariates.csv")
write_csv(demographics_AIC[,c(1:8)], "demographics_covariates.csv")

6.1 Nutrition covariates

Variable combination - order by increasing AIC
variables AIC BIC
2 calories_7d 665.7006 734.1784
7 calories_7d + protein_fullEF 667.2470 740.9923
6 calories_7d + aminoAcid_7d 667.3022 741.0476
8 calories_7d + calories_fullEF 667.5225 741.2679
14 calories_7d + protein_fullEF + calories_fullEF 668.0562 747.0691
12 calories_7d + aminoAcid_7d + protein_fullEF 668.8561 747.8690
13 calories_7d + aminoAcid_7d + calories_fullEF 669.0935 748.1063
16 calories_7d + aminoAcid_7d + protein_fullEF + calories_fullEF 669.5730 753.8534
3 aminoAcid_7d 677.9651 746.4429
1 679.4182 742.6285
9 aminoAcid_7d + protein_fullEF 679.9314 753.6768
10 aminoAcid_7d + calories_fullEF 679.9600 753.7053
5 calories_fullEF 681.3447 749.8225
4 protein_fullEF 681.4040 749.8819
15 aminoAcid_7d + protein_fullEF + calories_fullEF 681.9292 760.9421
11 protein_fullEF + calories_fullEF 683.3421 757.0875
Variable combination - order by increasing BIC
variables AIC BIC
2 calories_7d 665.7006 734.1784
7 calories_7d + protein_fullEF 667.2470 740.9923
6 calories_7d + aminoAcid_7d 667.3022 741.0476
8 calories_7d + calories_fullEF 667.5225 741.2679
1 679.4182 742.6285
3 aminoAcid_7d 677.9651 746.4429
14 calories_7d + protein_fullEF + calories_fullEF 668.0562 747.0691
12 calories_7d + aminoAcid_7d + protein_fullEF 668.8561 747.8690
13 calories_7d + aminoAcid_7d + calories_fullEF 669.0935 748.1063
5 calories_fullEF 681.3447 749.8225
4 protein_fullEF 681.4040 749.8819
9 aminoAcid_7d + protein_fullEF 679.9314 753.6768
10 aminoAcid_7d + calories_fullEF 679.9600 753.7053
16 calories_7d + aminoAcid_7d + protein_fullEF + calories_fullEF 669.5730 753.8534
11 protein_fullEF + calories_fullEF 683.3421 757.0875
15 aminoAcid_7d + protein_fullEF + calories_fullEF 681.9292 760.9421

6.2 Comorbidity covariates

Variable combination - order by increasing AIC
variables AIC BIC
6 Abx_days 674.0393 742.5172
16 ROP + Abx_days 674.2833 748.0286
25 IVH + ROP + Abx_days 674.5296 753.5425
13 IVH + Abx_days 674.6278 748.3732
10 BPD + Abx_days 675.8047 749.5500
15 Sepsis + Abx_days 675.8470 749.5923
12 IVH + ROP 676.0726 749.8179
22 BPD + ROP + Abx_days 676.0940 755.1069
26 Sepsis + ROP + Abx_days 676.2406 755.2535
29 BPD + IVH + ROP + Abx_days 676.4174 760.6978
24 IVH + Sepsis + Abx_days 676.4195 755.4324
19 BPD + IVH + Abx_days 676.4678 755.4807
31 IVH + Sepsis + ROP + Abx_days 676.4883 760.7688
18 BPD + IVH + ROP 677.2554 756.2683
23 IVH + Sepsis + ROP 677.4665 756.4794
21 BPD + Sepsis + Abx_days 677.6771 756.6899
3 IVH 677.6808 746.1587
5 ROP 678.0476 746.5254
30 BPD + Sepsis + ROP + Abx_days 678.0766 762.3570
11 IVH + Sepsis 678.2206 751.9660
28 BPD + IVH + Sepsis + Abx_days 678.3155 762.5959
32 BPD + IVH + Sepsis + ROP + Abx_days 678.3960 767.9440
7 BPD + IVH 678.4479 752.1933
9 BPD + ROP 678.5204 752.2658
27 BPD + IVH + Sepsis + ROP 678.9356 763.2160
14 Sepsis + ROP 679.0908 752.8362
2 BPD 679.3993 747.8771
1 679.4182 742.6285
4 Sepsis 679.5089 747.9867
17 BPD + IVH + Sepsis 679.5640 758.5769
20 BPD + Sepsis + ROP 680.0704 759.0833
8 BPD + Sepsis 680.3587 754.1041
Variable combination - order by increasing BIC
variables AIC BIC
6 Abx_days 674.0393 742.5172
1 679.4182 742.6285
3 IVH 677.6808 746.1587
5 ROP 678.0476 746.5254
2 BPD 679.3993 747.8771
4 Sepsis 679.5089 747.9867
16 ROP + Abx_days 674.2833 748.0286
13 IVH + Abx_days 674.6278 748.3732
10 BPD + Abx_days 675.8047 749.5500
15 Sepsis + Abx_days 675.8470 749.5923
12 IVH + ROP 676.0726 749.8179
11 IVH + Sepsis 678.2206 751.9660
7 BPD + IVH 678.4479 752.1933
9 BPD + ROP 678.5204 752.2658
14 Sepsis + ROP 679.0908 752.8362
25 IVH + ROP + Abx_days 674.5296 753.5425
8 BPD + Sepsis 680.3587 754.1041
22 BPD + ROP + Abx_days 676.0940 755.1069
26 Sepsis + ROP + Abx_days 676.2406 755.2535
24 IVH + Sepsis + Abx_days 676.4195 755.4324
19 BPD + IVH + Abx_days 676.4678 755.4807
18 BPD + IVH + ROP 677.2554 756.2683
23 IVH + Sepsis + ROP 677.4665 756.4794
21 BPD + Sepsis + Abx_days 677.6771 756.6899
17 BPD + IVH + Sepsis 679.5640 758.5769
20 BPD + Sepsis + ROP 680.0704 759.0833
29 BPD + IVH + ROP + Abx_days 676.4174 760.6978
31 IVH + Sepsis + ROP + Abx_days 676.4883 760.7688
30 BPD + Sepsis + ROP + Abx_days 678.0766 762.3570
28 BPD + IVH + Sepsis + Abx_days 678.3155 762.5959
27 BPD + IVH + Sepsis + ROP 678.9356 763.2160
32 BPD + IVH + Sepsis + ROP + Abx_days 678.3960 767.9440

6.3 Demographics covariates

Variable combination - order by increasing AIC
variables AIC BIC
1 679.4182 742.6285
3 maternal_obesity 681.0933 749.5711
2 Gender 681.4096 749.8874
4 maternal_smoking 681.4112 749.8890
6 Gender + maternal_obesity 683.0926 756.8380
9 maternal_obesity + maternal_smoking 683.0929 756.8383
7 Gender + maternal_smoking 683.4044 757.1498
12 Gender + maternal_obesity + maternal_smoking 685.0923 764.1052
5 maternal_race 688.1525 777.7005
10 maternal_obesity + maternal_race 689.8743 784.6897
11 maternal_smoking + maternal_race 690.1393 784.9548
8 Gender + maternal_race 690.1398 784.9553
13 Gender + maternal_obesity + maternal_race 691.8738 791.9568
15 maternal_obesity + maternal_smoking + maternal_race 691.8740 791.9569
14 Gender + maternal_smoking + maternal_race 692.1299 792.2129
16 Gender + maternal_obesity + maternal_smoking + maternal_race 693.8736 799.2241
Variable combination - order by increasing BIC
variables AIC BIC
1 679.4182 742.6285
3 maternal_obesity 681.0933 749.5711
2 Gender 681.4096 749.8874
4 maternal_smoking 681.4112 749.8890
6 Gender + maternal_obesity 683.0926 756.8380
9 maternal_obesity + maternal_smoking 683.0929 756.8383
7 Gender + maternal_smoking 683.4044 757.1498
12 Gender + maternal_obesity + maternal_smoking 685.0923 764.1052
5 maternal_race 688.1525 777.7005
10 maternal_obesity + maternal_race 689.8743 784.6897
11 maternal_smoking + maternal_race 690.1393 784.9548
8 Gender + maternal_race 690.1398 784.9553
13 Gender + maternal_obesity + maternal_race 691.8738 791.9568
15 maternal_obesity + maternal_smoking + maternal_race 691.8740 791.9569
14 Gender + maternal_smoking + maternal_race 692.1299 792.2129
16 Gender + maternal_obesity + maternal_smoking + maternal_race 693.8736 799.2241

7. Assessing interaction term between covariates and time_week

formula_cov <- list()
formula_cov[[1]] <- "wt_z_score ~ calories_7d + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id)"
formula_cov[[2]] <- "wt_z_score ~ calories_7d + calories_7d:time_week + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id)"
formula_cov[[3]] <- "wt_z_score ~ calories_7d + Abx_days + Abx_days:time_week + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id)"

summary_cov <- list()
cov_interaction_analysis <- list()

for (i in seq_along(formula_cov)) {
  formula <- as.formula(formula_cov[[i]])
  lmer.analysis <- lmerTest::lmer(formula, 
                                  data = long.df.scale, 
                                  REML = FALSE,
                                  control = lme4::lmerControl(optimizer = "optimx", 
                                                              calc.derivs = FALSE,
                                                              optCtrl = list(method = "nlminb"))
 )
  summary_cov[[i]] <- list(as.character(formula(lmer.analysis)),
                       as.numeric(summary(lmer.analysis)$AICtab["AIC"]),
                       as.numeric(summary(lmer.analysis)$AICtab["BIC"]),
                       as.numeric(summary(lmer.analysis)$AICtab["logLik"]),
                       as.numeric(summary(lmer.analysis)$AICtab["deviance"]),
                       as.numeric(summary(lmer.analysis)$AICtab["df.resid"]))
  cov_interaction_analysis[[i]] <- lmer.analysis
}

cov_AIC <- data.frame()
for (i in seq_along(1:length(formula_cov))) {
  summary_cov[[i]][[1]] <- as.character(summary_cov[[i]][[1]])
  summary_cov[[i]][[1]] <- paste(summary_cov[[i]][[1]][2], summary_cov[[i]][[1]][1], summary_cov[[i]][[1]][3])
  for (j in seq_along(1:length(summary_cov[[i]]))) {
    cov_AIC[i,j] <- summary_cov[[i]][[j]]
  }
}

colnames(cov_AIC) <- c("formula", "AIC", "BIC", "logLik", "deviance", "df_resid")

folder.check(folder.name = "Results", getwd())
folder.check(folder.name = "mixed_effects_modeling", getwd())

write_csv(cov_AIC, "covariate_interaction_analysis.csv")

kableExtra::kable(cov_AIC[order(cov_AIC$AIC),c(1:3)]) %>% 
  add_header_above(c("Various combination of covariate-by-time interaction - order by increasing AIC" = 4)) %>%
  kable_styling(bootstrap_options = "striped", full_width = T)
Various combination of covariate-by-time interaction - order by increasing AIC
formula AIC BIC
1 wt_z_score ~ calories_7d + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 665.4144 739.1597
3 wt_z_score ~ calories_7d + Abx_days + Abx_days:time_week + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 666.5000 745.5129
2 wt_z_score ~ calories_7d + calories_7d:time_week + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 667.3888 746.4017
Various combination of covariate-by-time interaction - order by increasing BIC
formula AIC BIC
1 wt_z_score ~ calories_7d + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 665.4144 739.1597
3 wt_z_score ~ calories_7d + Abx_days + Abx_days:time_week + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 666.5000 745.5129
2 wt_z_score ~ calories_7d + calories_7d:time_week + Abx_days + pla_insuff + time_week + I(time_week^2) + pla_insuff:time_week + (time_week + I(time_week^2) | id) 667.3888 746.4017

8. Table and plot

## Loading required package: sjPlot
## Learn more about sjPlot with 'browseVignettes("sjPlot")'.
## Model contains polynomial or cubic / quadratic terms. Consider using `terms="time_week [all]"` to get smooth plots. See also package-vignette 'Marginal Effects at Specific Values'.
## png 
##   2
## Model contains polynomial or cubic / quadratic terms. Consider using `terms="time_week [all]"` to get smooth plots. See also package-vignette 'Marginal Effects at Specific Values'.
## png 
##   2

8.1 placental insufficeincy-by-time interaction

Mixed-effects modeling - coefficient table
  wt_z_score
Predictors Estimates CI p
(Intercept) -1.441 -1.615 – -1.267 <0.001
pla_insuffYes -0.876 -1.141 – -0.611 <0.001
time_week -0.031 -0.046 – -0.015 <0.001
I(time_week^2) 0.004 0.003 – 0.005 <0.001
pla_insuffYes:time_week -0.047 -0.073 – -0.021 0.001
Random Effects
?2 0.05
?00 id 0.48
?11 id.time_week 0.00
?11 id.I(time_week^2) 0.00
?01 0.70
-0.48
N id 91
Observations 1433
Marginal R2 / Conditional R2 0.364 / 0.935

8.2 birth weight-by-time interaction

Mixed-effects modeling - coefficient table
  wt_z_score
Predictors Estimates CI p
(Intercept) -1.518 -1.660 – -1.377 <0.001
birth_weight 0.581 0.448 – 0.714 <0.001
time_week -0.038 -0.051 – -0.024 <0.001
I(time_week^2) 0.004 0.003 – 0.005 <0.001
birth_weight:time_week 0.023 0.009 – 0.038 0.002
Random Effects
?2 0.05
?00 id 0.39
?11 id.time_week 0.00
?11 id.I(time_week^2) 0.00
?01 0.74
-0.49
N id 91
Observations 1433
Marginal R2 / Conditional R2 0.461 / 0.935

9. Session Info

## R version 3.6.0 (2019-04-26)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] parallel  grid      stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] sjPlot_2.6.3      doParallel_1.0.14 iterators_1.0.10 
##  [4] foreach_1.4.4     kableExtra_1.1.0  PropCIs_0.3-0    
##  [7] effsize_0.7.4     optimx_2018-7.10  ggpubr_0.2       
## [10] magrittr_1.5      vcd_1.4-4         psych_1.8.12     
## [13] lmerTest_3.1-0    lme4_1.1-21       Matrix_1.2-17    
## [16] forcats_0.4.0     stringr_1.4.0     dplyr_0.8.0.1    
## [19] purrr_0.3.2       readr_1.3.1       tidyr_0.8.3      
## [22] tibble_2.1.1      ggplot2_3.1.1     tidyverse_1.2.1  
## 
## loaded via a namespace (and not attached):
##  [1] nlme_3.1-139       lubridate_1.7.4    RColorBrewer_1.1-2
##  [4] insight_0.3.0      webshot_0.5.1      httr_1.4.0        
##  [7] numDeriv_2016.8-1  tools_3.6.0        TMB_1.7.15        
## [10] backports_1.1.4    R6_2.4.0           sjlabelled_1.0.17 
## [13] lazyeval_0.2.2     colorspace_1.4-1   withr_2.1.2       
## [16] tidyselect_0.2.5   gridExtra_2.3      mnormt_1.5-5      
## [19] emmeans_1.3.4      compiler_3.6.0     performance_0.1.0 
## [22] cli_1.1.0          rvest_0.3.3        xml2_1.2.0        
## [25] labeling_0.3       bayestestR_0.1.0   scales_1.0.0      
## [28] mvtnorm_1.0-10     lmtest_0.9-37      digest_0.6.18     
## [31] foreign_0.8-70     minqa_1.2.4        rmarkdown_1.12    
## [34] pkgconfig_2.0.2    htmltools_0.3.6    highr_0.8         
## [37] rlang_0.3.4        readxl_1.3.1       rstudioapi_0.10   
## [40] generics_0.0.2     zoo_1.8-5          jsonlite_1.6      
## [43] Rcpp_1.0.1         munsell_0.5.0      stringi_1.4.3     
## [46] yaml_2.2.0         snakecase_0.9.2    MASS_7.3-51.1     
## [49] plyr_1.8.4         sjmisc_2.7.9       crayon_1.3.4      
## [52] lattice_0.20-38    ggeffects_0.10.0   haven_2.1.0       
## [55] cowplot_0.9.4      splines_3.6.0      sjstats_0.17.4    
## [58] hms_0.4.2          knitr_1.22         pillar_1.3.1      
## [61] boot_1.3-20        estimability_1.3   codetools_0.2-16  
## [64] glue_1.3.1         evaluate_0.13      modelr_0.1.4      
## [67] nloptr_1.2.1       cellranger_1.1.0   gtable_0.3.0      
## [70] assertthat_0.2.1   xfun_0.6           xtable_1.8-3      
## [73] broom_0.5.2        coda_0.19-2        viridisLite_0.3.0 
## [76] glmmTMB_0.2.3      ellipsis_0.1.0