Passing strings into 'contrasts' argument of lme/lmer

时光怂恿深爱的人放手 提交于 2021-01-28 11:47:09

问题


I am writing a sub-routine to return output of longitudinal mixed-effects models. I want to be able to pass elements from lists of variables into lme/lmer as the outcome and predictor variables. I would also like to be able to specify contrasts within these mixed-effects models, however I am having trouble with getting the contrasts() argument to recognise the strings as the variable names referred to in the model specification within the same lme/lme4 call.

Here's some toy data,

set.seed(345)
A0 <- rnorm(4,2,.5)
B0 <- rnorm(4,2+3,.5)
A1 <- rnorm(4,6,.5)
B1 <- rnorm(4,6+2,.5)
A2 <- rnorm(4,10,.5)
B2 <- rnorm(4,10+1,.5)
A3 <- rnorm(4,14,.5)
B3 <- rnorm(4,14+0,.5)
score <- c(A0,B0,A1,B1,A2,B2,A3,B3)
id <- rep(1:8,times = 4, length = 32)
time <- factor(rep(0:3, each = 8, length = 32))
group <- factor(rep(c("A","B"), times =2, each = 4, length = 32))
df <- data.frame(id = id, group = group, time = time,  score = score)

Now the following call to lme works just fine, with contrasts specified (I know these are the default so this is all purely pedagogical).

mod <- lme(score ~ group*time, random = ~1|id, data = df, contrasts = list(group = contr.treatment(2), time = contr.treatment(4)))

The following also works, passing strings as variable names into lme using the reformulate() function.

t <- "time"
g <- "group"
dv <- "score"

mod1R <- lme(reformulate(paste0(g,"*",t), response = "score"), random = ~1|id, data = df)

But if I want to specify contrasts, like in the first example, it doesn't work

mod2R <- lme(reformulate(paste0(g,"*",t), response = "score"), random = ~1|id, data = df, contrasts = list(g = contr.treatment(2), t = contr.treatment(4)))

# Error in `contrasts<-`(`*tmp*`, value = contrasts[[i]]) : contrasts apply only to factors

How do I get lme to recognise that the strings specified to in the contrasts argument refer to the variables passed into the reformulate() function?


回答1:


You should be able to use setNames() on the list of contrasts to apply the full names to the list:

# Using a %>% pipe so need to load magrittr
library(magrittr)

mod2R <- lme(reformulate(paste0(g,"*",t), response = "score"), 
             random = ~1|id, 
             data = df, 
             contrasts = list(g = contr.treatment(2), t = contr.treatment(4)) %>%
                 setNames(c(g, t))
             )


来源:https://stackoverflow.com/questions/52730766/passing-strings-into-contrasts-argument-of-lme-lmer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!