如何在函数中使用lmer

时间:2022-11-17 07:39:49

I'm trying to write a function that collects some calls I use often in scripts
I use the sleepstudy data of the lme4 package in my examples
Here's (a simplified version of) the function I started with:

我正在尝试编写一个函数来收集我在脚本中经常使用的一些调用,我在这里的示例中使用了lme4包的sleepstudy数据(简化版)我开始使用的函数:

trimModel1 <- function(frm, df) {
  require(LMERConvenienceFunctions)
  require(lme4)

  lm<-lmer(frm,data=df)
  lm.trimmed = romr.fnc(lm, df)
  df = lm.trimmed$data
  # update initial model on trimmed data
  lm<-lmer(frm,data=df)
#   lm@call$formula<-frm
  mcp.fnc(lm)
  lm
}

When I call this function like below:

当我这样调用这个函数时:

(fm1<-trimModel1(Reaction ~ Days + (Days|Subject),sleepstudy))

The first three lines of the output look like this:

输出的前三行如下:

Linear mixed model fit by REML 
Formula: frm    
Data: df

If I had called the commands of the trimModel1 function in the console the first three lines of the summary of the model look like this:

如果我在控制台中调用了trimModel1函数的命令,那么模型总结的前三行是这样的:

Linear mixed model fit by REML 
Formula: Reaction ~ Days + (Days | Subject) 
   Data: sleepstudy 

The difference is a problem because several packages that use the lme4 package make use of the formula and data fields. For instance the effects package uses these fields and a command like below will not work when I use the trimModel1 function above:

差异是一个问题,因为使用lme4包的几个包使用公式和数据字段。例如,当我使用上面的trimModel1函数时,效果包使用这些字段和下面的命令将不起作用:

library(effects)
plot(allEffects(fm1))

I looked around on * and R discussion groups for a solution and saw that you could change the formula field of the model. If you uncomment the lm@call$formula<-frm line in the trimModel1 function the formula field in the summary is displayed correctly. Unfortunately when I run a function from the effects package now I still get the error:

我在*和R讨论组中查找了一个解决方案,发现可以更改模型的公式字段。如果在trimModel1函数中取消对lm@call$formula<-frm line的注释,则会正确显示summary中的公式字段。不幸的是,当我在effects包中运行一个函数时,仍然会出现以下错误:

Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type

This is because the data field is still incorrect.
Another possible solution I found is this function:

这是因为数据字段仍然不正确。我找到的另一个可能的解是这个函数:

trimModel2 <- function(frm, df) {
  require(LMERConvenienceFunctions)
  require(lme4)

  lm<-do.call("lmer",list(frm,data=df))
  lm.trimmed = romr.fnc(lm, df)
  df = lm.trimmed$data
  # update initial model on trimmed data
  lm<-do.call("lmer",list(frm,data=df))
  mcp.fnc(lm)
  lm
}

When I now type the following commands in the console I get no errors:

当我现在在控制台输入以下命令时,我没有得到错误:

(fm2<-trimModel2(Reaction ~ Days + (Days|Subject),sleepstudy))
plot(allEffects(fm2))

The allEffects function works but now the problem is that the the summary of the fm2 model displays the raw sleepstudy data. That is not a big problem with the sleepstudy data but with very large datasets sometimes Rstudio crashed when displaying a model.
Does anyone know how to make one (or both) of these functions work correctly?
I think I have to change the fm1@call$data field but I don't know how.

allEffects函数有效,但现在的问题是fm2模型的摘要显示了原始的睡眠研究数据。这对于sleepstudy数据来说不是一个大问题,但是对于非常大的数据集,Rstudio在显示模型时有时会崩溃。有人知道如何使这些函数中的一个(或两个)正确工作吗?我想我必须更改fm1@call$data字段,但我不知道如何更改。

1 个解决方案

#1


3  

Do it like this:

这样做:

trimModel1 <- function(frm, df) {
  require(LMERConvenienceFunctions)
  require(lme4)
  dfname <- as.name(deparse(substitute(df)))

  lm<-lmer(frm,data=df)
  lm.trimmed = romr.fnc(lm, df)
  df = lm.trimmed$data
  # update initial model on trimmed data
  lm<-lmer(frm,data=df)
  lm@call$formula <- frm
  lm@call$data <- dfname
  mcp.fnc(lm)
  lm
}

It's the "deparse-substitute trick" to get an object name from the object itself.

从对象本身获取对象名是“背离替代技巧”。

#1


3  

Do it like this:

这样做:

trimModel1 <- function(frm, df) {
  require(LMERConvenienceFunctions)
  require(lme4)
  dfname <- as.name(deparse(substitute(df)))

  lm<-lmer(frm,data=df)
  lm.trimmed = romr.fnc(lm, df)
  df = lm.trimmed$data
  # update initial model on trimmed data
  lm<-lmer(frm,data=df)
  lm@call$formula <- frm
  lm@call$data <- dfname
  mcp.fnc(lm)
  lm
}

It's the "deparse-substitute trick" to get an object name from the object itself.

从对象本身获取对象名是“背离替代技巧”。