经过多次故障排除后,我能够解决这个问题。关键是指定在 lme 函数之外使用哪些数据。
myfunc <- function(x){
out <- with(mydata, lme(zglobcog ~ age_c + factor(apoee4_carrier) + age_c*factor(apoee4_carrier)
+ factor(i) + age_c*factor(i), random = ~ age_c | pathid, method = "ML", na.action = na.exclude))
out
}
lapply(mydata[5:7], myfunc)
或者使用函数内的 lapply 运行该函数
myfunc <- function(X){
lapply(X, function(.col){
out <- with(mydata, lme(zglobcog ~ age_c + factor(apoee4_carrier) + age_c*factor(apoee4_carrier)
+ factor(i) + age_c*factor(i), random = ~ age_c | pathid, method = "ML", na.action = na.exclude))
out
})
}
myfucn(mydata[5:7])
最后仅提供一个使用 Orthodont 数据集的示例
library(nlme)
attach(Orthodont)
head(Orthodont)
myfunc <- function(x){
out <- with(Orthodont, lme(distance ~ age + x, random = ~ age | Subject, method = "ML", na.action
= na.exclude))
out
}
myfunc(Sex)