R语言 非线性混合效应模型中杠杆和Cook距离的计算

rqmkfv5c  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(121)

我试图计算杠杆和厨师的距离为我开发的模型使用nlme,但我得到的错误说
cooks.distance.lme(模型)中的错误:未为“nlme”对象实现
如何计算nlme中的Cook距离?

formula = log_Lobs ~ log(150*((1 + ((150/Lt_1)^(1/exp(p))-1)*exp(-exp(k)*td/365))^(-exp(p))))
model <- do.call(nlme,
                    list(formula,
                         fixed = c(p ~ 1, k ~ 1 + season2),
                         random = k ~ 1 | id,
                         data = data_select,
                         start = list(fixed = c(p, k)),
                         na.action = na.exclude,
                         control=list(maxIter=1e6, msMaxIter = 1e6, msVerbose = TRUE)
                    ))

#Calculate leverage
lev<-hat(model.matrix(model))

字符集
错误(x$项%)||% attr(x,“terms”)%||% stop(“no terms component nor attribute”):无术语组件或属性

#Calculate Cook's Distance
cd<-cooks.distance(model)


cooks.distance.lme(模型)中的错误:未为“nlme”对象实现

vngu2lb8

vngu2lb81#

你可以用蛮力来做:从Wikipedia
Cook的观测距离i定义为从回归模型中移除观测值i时回归模型中所有变化的总和
使用Nieuwenhuis 2012的公式

library(nlme)
mod1 <- lme(distance ~ age*Sex, random = ~age|Subject, data = Orthodont)
coef1 <- fixef(mod1)
cooksd <- rep(NA, nrow(Orthodont))
Vinv <- solve(vcov(mod1))
np <- length(coef1)
for (i in seq(nrow(Orthodont))) {
   newmod <- try(update(mod1, data = Orthodont[-i, ]), silent = TRUE)
   if (inherits(newmod, "try-error")) next
   newcoef <- fixef(newmod)
   cdiff <- coef1-newcoef
   cooksd[i] <- (cdiff %*% Vinv %*% cdiff)/np
}

字符集
尼文赫伊斯2012.“Influence.ME:Tools for Detecting Influential Data in Mixed Effects Models”R Journal 4/2 https://journal.r-project.org/archive/2012/RJ-2012-011/RJ-2012-011.pdf

相关问题