如何在函数内使用lmer [英] How to use lmer inside a function
问题描述
我正在尝试编写一个函数,该函数收集脚本中经常使用的一些调用
我在示例中使用了lme4包的sleepstudy数据
这是我开始使用的功能(的简化版本):
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:
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
如果我在控制台中调用了trimModel1函数的命令,则模型摘要的前三行如下所示:
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:
Linear mixed model fit by REML
Formula: Reaction ~ Days + (Days | Subject)
Data: sleepstudy
差异是一个问题,因为使用lme4软件包的多个软件包都使用了公式和数据字段.例如,效果包使用了这些字段,而当我使用上面的trimModel1函数时,如下所示的命令将不起作用:
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:
library(effects)
plot(allEffects(fm1))
我在stackoverflow和R讨论组中四处寻找解决方案,发现可以更改模型的公式字段.如果取消注释trimModel1函数中的lm@call$formula<-frm
行,则摘要中的公式字段将正确显示.不幸的是,当我现在从effects包运行一个函数时,仍然出现错误:
I looked around on stackoverflow 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:
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))
allEffects函数可以工作,但是现在的问题是fm2模型的摘要显示了原始睡眠研究数据.睡眠研究数据并不是一个大问题,但是对于非常大的数据集,有时Rstudio在显示模型时会崩溃.
有谁知道如何使其中一个(或两个)功能正常工作?
我想我必须更改fm1 @ call $ data字段,但我不知道如何.
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.
推荐答案
这样做:
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.
这篇关于如何在函数内使用lmer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!