如何修改 lmer 中的随机效果

How to modify random effects in lmer

有没有办法修改(覆盖)lmer 模型中的随机效应?对于固定效果,有一个名为 my_lmer@beta 的插槽,我可以使用以下方式更改固定效果:

my_lmer@beta[1] <- 0.5

对于随机效果,是否有类似的方法? lmer-object 是否已经包含随机效应,或者稍后由例如计算ranef().

如果能弄清楚您要做什么,那确实很好。修改 reference class objects is particularly dangerous -- you could accidentally modify copies of objects or cause segmentation faults ... from here

的内部结构
library(lme4)
fm1 <- lmer(Reaction~Days+(1|Subject),sleepstudy) ## just for example
fm1@pp$getRefClass()$methods()

将向您展示方法...但是,您必须比这更深入一些...事实证明(src/predModule.cpp 的第 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]
}