在数据框中运行lmer时遇到的问题

huangapple go评论72阅读模式
英文:

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)

请注意,这只是您提供的代码的翻译部分,不包括错误处理或其他附加信息。如果您需要更多帮助或解决错误,请提供更多上下文。

英文:

在数据框中运行lmer时遇到的问题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 &lt;- formula(&quot;. ~ year + (1|subject)&quot;)

# define the function to apply lmer to each column in a data frame
lmer_function &lt;- function(df, cols) {
  lmer_results &lt;- list()
  for (col in cols) {
    col_formula &lt;- as.formula(paste(col, deparse(lmer_formula)[2], sep = &quot;~&quot;))
    lmer_result &lt;- lmer(col_formula, data = df)
    lmer_results[[col]] &lt;- lmer_result
  }
  return(lmer_results)
}


# apply the lmer function to each data frame in the list for all columns
cols &lt;- names(FAI_amb_Study)[7:length(names(FAI_amb_Study))]
lmer_results &lt;- 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 &lt;- list()
## response variables to consider
cols &lt;- names(FAI_amb_Study)[7:ncol(FAI_amb_Study)]
## formula for first model
form1 &lt;- reformulate(c(&quot;year&quot;,&quot;(1|subject)&quot;), response = cols[1])
## fit first model
lmer_results[[1]] &lt;- lmer(form1, data = FAI_amb_Study)
for (i in 2:length(cols)) {
  ## refit first model with successive columns as response
  lmer_results[[i]] &lt;- refit(lme_results[[1]], 
                             FAI_amb_study[[cols[i]]])
}

huangapple
  • 本文由 发表于 2023年5月15日 09:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定