Home > database >  How can I loop a list of models to get slope estimate
How can I loop a list of models to get slope estimate

Time:11-11

I have a list of models as specified by the following code:

varlist <- list("PRS_Kunkle", "PRS_Kunkle_e07", 
   "PRS_Kunkle_e06","PRS_Kunkle_e05", "PRS_Kunkle_e04", 
   "PRS_Kunkle_e03", "PRS_Kunkle_e02", "PRS_Kunkle_e01",  
   "PRS_Kunkle_e00", "PRS_Jansen", "PRS_deroja_KANSL")

PRS_age_pacc3 <- lapply(varlist, function(x) {    
    lmer(substitute(z_pacc3_ds ~ i*AgeAtVisit    i*I(AgeAtVisit^2)     
             APOE_score   gender   EdYears_Coded_Max20    
             VisNo   famhist   X1   X2   X3   X4   X5  
             (1 |family/DBID),
       list(i=as.name(x))), data = WRAP_all, REML = FALSE)
})

I want to obtain the slope of PRS at different age points in each of the models. How can I write code to achieve this goal? Without loop, the raw code should be:

test_stat1 <- simple_slopes(PRS_age_pacc3[[1]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat2 <- simple_slopes(PRS_age_pacc3[[2]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat3 <- simple_slopes(PRS_age_pacc3[[3]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat4 <- simple_slopes(PRS_age_pacc3[[4]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat5 <- simple_slopes(PRS_age_pacc3[[5]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat6 <- simple_slopes(PRS_age_pacc3[[6]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat7 <- simple_slopes(PRS_age_pacc3[[7]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat8 <- simple_slopes(PRS_age_pacc3[[8]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat9 <- simple_slopes(PRS_age_pacc3[[9]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat10 <- simple_slopes(PRS_age_pacc3[[10]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))
test_stat11 <- simple_slopes(PRS_age_pacc3[[11]], levels=list(AgeAtVisit=c(55,60,65,70,75,80)))

CodePudding user response:

library(lme4)
library(reghelper)
set.seed(101)
## add an additional factor variable so we can use it for an interaction
sleepstudy$foo <- factor(sample(LETTERS[1:3], size = nrow(sleepstudy),
                         replace = TRUE))
m1 <- lmer(Reaction ~ Days*foo   I(Days^2)*foo   (1|Subject), data = sleepstudy)
s1 <- simple_slopes(m1, levels=list(Days = c(5, 10, 15)))

Looking at these results, s1 is a data frame with 6 rows (number of levels of foo × number of Days values specified) and 5 columns (Days, foo, estimate, std error, t value).

The simplest way to do this:

res <- list()
for (i in seq_along(varlist)) {
   res[[i]] <- simple_slopes(model_list[[i]], ...)  ## add appropriate args here
}
res <- do.call("rbind", res)  ## collapse elements to a single data frame
## add an identifier column
res_final <- data.frame(model = rep(varlist, each = nrow(res[[1]])), res)

If you want to be fancier, you could replace the for loop with an appropriate lapply. If you want to be even fancier than that:

library(tidyverse)
(model_list
  %>% setNames(varlist)
  ## map_dfr runs the function on each element, collapses results to
  ##  a single data frame. `.id="model"` adds the names of the list elements
  ##  (set in the previous step) as a `model` column
  %>% purrr::map_dfr(simple_slopes, ... <extra args here>, .id = "model")
)

By the way, I would be very careful with simple_slopes when you have a quadratic term in the model as well. The slopes calculated will (presumably) apply only in the case where any other continuous variables in the model are zero. You might want to center your variables as in Schielzeth 2010 Methods in Ecology and Evolution ("Simple means to improve ...")

  • Related