英文:
Issues with running lmer on multiple columns stored in a dataframe
问题
以下是您提供的代码的翻译部分:
# 定义lmer的公式
lmer_formula <- formula(". ~ year + (1|subject)")
# 定义将lmer应用于数据框中每列的函数
lmer_function <- function(df, cols) {
lmer_results <- list()
for (col in cols) {
col_formula <- as.formula(paste(col, deparse(lmer_formula)[2], sep = "~"))
lmer_result <- lmer(col_formula, data = df)
lmer_results[[col]] <- lmer_result
}
return(lmer_results)
}
# 对列表中的每个数据框的所有列应用lmer函数
cols <- names(FAI_amb_Study)[7:length(names(FAI_amb_Study))]
lmer_results <- lmer_function(df = FAI_amb_Study, cols = cols)
请注意,这只是您提供的代码的翻译部分,不包括错误处理或其他附加信息。如果您需要更多帮助或解决错误,请提供更多上下文。
英文:
I have a data frame with 10 subjects each of which has multiple angle movement measurements repeated 5 times at year 0 and 1. The Entire data is in long format and I am trying to find the difference between year 0 and 1, because there are multiple measurements for each subject I am trying to use the lmer function.
I used the follow code but keep getting an error. Any help would be appreciated.
# define the formula for lmer
lmer_formula <- formula(". ~ year + (1|subject)")
# define the function to apply lmer to each column in a data frame
lmer_function <- function(df, cols) {
lmer_results <- list()
for (col in cols) {
col_formula <- as.formula(paste(col, deparse(lmer_formula)[2], sep = "~"))
lmer_result <- lmer(col_formula, data = df)
lmer_results[[col]] <- lmer_result
}
return(lmer_results)
}
# apply the lmer function to each data frame in the list for all columns
cols <- names(FAI_amb_Study)[7:length(names(FAI_amb_Study))]
lmer_results <- lmer_function(df = FAI_amb_Study, cols = cols)
> Error in terms.formula(formula, data = data) : invalid model
> formula in ExtractVars
答案1
得分: 2
refit
是一种方便(且更快速)的方法,用于使用不同的响应变量重新拟合模型,前提是模型的其余部分保持不变。
英文:
refit
is a convenient (and faster) way to refit a model with different response variables, provided the rest of the model is the same.
## set up structure for results
lmer_results <- list()
## response variables to consider
cols <- names(FAI_amb_Study)[7:ncol(FAI_amb_Study)]
## formula for first model
form1 <- reformulate(c("year","(1|subject)"), response = cols[1])
## fit first model
lmer_results[[1]] <- lmer(form1, data = FAI_amb_Study)
for (i in 2:length(cols)) {
## refit first model with successive columns as response
lmer_results[[i]] <- refit(lme_results[[1]],
FAI_amb_study[[cols[i]]])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论