gpt4 book ai didi

r - 如何使用随机效应进行逐步模型(lme4 + lmerTest?)

转载 作者:行者123 更新时间:2023-12-03 16:51:10 27 4
gpt4 key购买 nike

我正在尝试执行具有随机效应的逐步模型,其中我可以获得 BIC 值。

lmerTest 包说它适用于 lme4,但只有从模型中删除我的一个自变量(这是一个具有两个选项 (TM) 的因素),我才能让它工作

错误代码是:

Error in $<-(*tmp*, formula, value = Terms) : no method for assigning subsets of this S4 class



或者

Error in as_lmerModLmerTest(model) : model not of class 'lmerMod': cannot coerce to class 'lmerModLmerTest



我在某处读过它可能与 drop1 有关,但我仍然没有弄清楚。我也愿意接受其他包和功能的建议。

之前,当尝试
full.model <- lm ( ...
一切正常。改成lmer后就不行了。

我现在使用的代码:
full.model <- lme4::lmer(dep ~ TM + ind + (1 | dorp),  data=test)  #lmerTest:: give same outcome

step.model<- lmerTest::step(full.model, direction="both",k=log(16)) # n=16

summary(step.model)

BIC(step.model)
#Example dataset

test <- data.frame(TM = as.factor(c(rep("org", 3), rep("min", 3),rep("org", 3), rep("min", 3),rep("org", 3), rep("min", 3))),
dep = runif(18,0,20),
ind = runif(18,0,7),
dorp = as.factor(c(rep(1,6),rep(2,6),rep(3,6))))

最佳答案

问题是lmerTest::step.lmerModLmerTest在随机效应选择阶段从模型中消除所有随机效应时中断。它可能不应该(我认为该包的早期版本可能不会),但解决这个问题并不太难。您可以指定不应简化随机效应模型 ( step(full.model, reduce.random=FALSE) ),或者,当您遇到此错误时,丢弃模型的随机效应分量,然后使用 step()在得到的线性模型上:

fixmodel <- lm(formula(full.model,fixed.only=TRUE),
data=eval(getCall(full.model)$data))
step(fixmodel)

(因为它包含 eval() ,这仅适用于 R 可以找到由 data= 参数引用的数据框的环境)。

我已经提交了 issue关于这个问题。

此外(令人困惑), stats::stepstep.lmerModLmerTest 有不同的论点/做出不同的假设在 lmerTest包裹。 stats::step被定义为
step(object, scope, scale = 0,
direction = c("both", "backward", "forward"),
trace = 1, keep = NULL, steps = 1000, k = 2, ...)

step.lmerModLmerTest用途
step(object, ddf = c("Satterthwaite",
"Kenward-Roger"), alpha.random = 0.1, alpha.fixed = 0.05,
reduce.fixed = TRUE, reduce.random = TRUE, keep, ...)

特别是 direction参数不适用( step.lmerModLmerTest 仅进行向后消除);不是 k (我相信 step.lmerModLmerTest 使用 AIC,但我必须仔细检查)。
set.seed(1001)
dd <- data.frame(x1=rnorm(500),x2=rnorm(500),
x3=rnorm(500),f=factor(rep(1:50,each=10)))
library(lme4)
dd$y <- simulate(~x1+x2+x3+(1|f),
newdata=dd,
newparams=list(theta=1,beta=c(1,2,0,0),
sigma=1),
family=gaussian)[[1]]
library(lmerTest)
full.model <- lmer(y~x1+x2+x3+(1|f), data=dd)
step.model<- step(full.model)
step.model有课 step_list ;有打印方法,但没有摘要方法。

关于r - 如何使用随机效应进行逐步模型(lme4 + lmerTest?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55638476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com