有没有办法修改(覆盖)lmer模型中的随机效果?对于固定效果,有一个名为my_lmer@beta的插槽,我可以使用以下命令更改固定效果:
my_lmer@beta[1] <- 0.5对于随机效果,有没有类似的方法?lmer-object是否已经包含随机效果,或者是稍后由ranef()计算的。
发布于 2016-08-31 08:51:09
找出你想要做的事情确实是件好事。修改reference class对象的内部结构特别危险--您可能会意外地修改对象的副本或导致分段错误……来自here,
library(lme4)
fm1 <- lmer(Reaction~Days+(1|Subject),sleepstudy) ## just for example
fm1@pp$getRefClass()$methods()我会向你展示这些方法。然而,你必须走得更远一点...事实证明(src/predModule.cpp的1.85) b实际上做的是获取内部u
VectorXd merPredD::b(const double& f) const {return d_Lambdat.adjoint() * u(f);}这反过来会调用
VectorXd merPredD::u(const double& f) const {return d_u0 + f * d_delu;}这意味着为了更改b,您需要更改u0的相应值;目前,我认为这是不可能的。
作为参考,这是一些代码(来自here),当随机效果被(向量) z从它们的估计值移位时,它会评估偏差……
rr <- m@resp ## extract response module
u0 <- getME(m,"u") ## conditional modes
L <- getME(m,"L")
## sd <- 1/getME(pp,"L")@x
## filled elements of L matrix==diag for simple case
## for more general case need the following -- still efficient
sd <- sqrt(diag(chol2inv(L)))
## fixed-effects contribution to linear predictor
fc <- getME(m,"X") %*% getME(m,"beta")
ZL <- t(getME(m,"Lambdat") %*% getME(m,"Zt"))
## evaluate the unscaled conditional density on the deviance scale
dc <- function(z) {
uu <- u0 + z * sd ## displace conditional modes
## should still work if z is a vector (by recycling, because u values
## applying to each group are stored adjacent to each other)
rr$updateMu(fc + ZL %*% uu) ## update linear predictor
drc <- unname(as.vector(tapply(rr$devResid(), ff, sum)))
uuc <- colSums(matrix(uu * uu,nrow=nvar))
(drc + uuc)[grps]
}https://stackoverflow.com/questions/39232183
复制相似问题