要考虑的主要问题是nlme::lmList
返回list
对象,所以你必须使用它的列表方法。
两种方法可以使用iris
数据上的模型。第一种方法为每个组估计单独的模型,而第二种方法适合多层次模型,观察嵌套在组内。
首先,利用nlme
lmList
和cooks.distance
从base
R:
library(nlme)
# run the models and store them
modlist <- lmList(object = Petal.Length ~ Sepal.Length | Species, data = iris)
# see the results
summary(modlist)
这将返回:
Call:
Model: Petal.Length ~ Sepal.Length | Species
Data: iris
Coefficients:
(Intercept)
Estimate Std. Error t value Pr(>|t|)
setosa 0.8030518 0.5310388 1.5122280 0.1326674
versicolor 0.1851155 0.4305590 0.4299423 0.6678803
virginica 0.6104680 0.3882233 1.5724662 0.1180371
Sepal.Length
Estimate Std. Error t value Pr(>|t|)
setosa 0.1316317 0.10582369 1.243877 2.155658e-01
versicolor 0.6864698 0.07226626 9.499174 6.483105e-17
virginica 0.7500808 0.05866167 12.786556 1.714921e-25
Residual standard error: 0.2611123 on 144 degrees of freedom
现在得到的Cook距离:
cooks1 <- lapply(modlist, cooks.distance)
其次,使用lmList
从lme4
和CookD
从predictmeans
:
library(predictmeans)
# this loads lme4 as a required package
# run the models and store them
modlist2 <- lmer(Petal.Length ~ Sepal.Length + (1 | Species), data = iris)
# see the results
summary(modlist2)
这将返回:
Linear mixed model fit by REML ['lmerMod']
Formula: Petal.Length ~ Sepal.Length + (1 | Species)
Data: iris
REML criterion at convergence: 69.3
Scaled residuals:
Min 1Q Median 3Q Max
-2.71305 -0.62672 0.02935 0.61922 2.83011
Random effects:
Groups Name Variance Std.Dev.
Species (Intercept) 2.52912 1.5903
Residual 0.07984 0.2826
Number of obs: 150, groups: Species, 3
Fixed effects:
Estimate Std. Error t value
(Intercept) 0.05252 0.95576 0.055
Sepal.Length 0.63414 0.04525 14.014
Correlation of Fixed Effects:
(Intr)
Sepal.Lngth -0.277
获取Cook距离和相关剧情:
cooks2 <- CookD(model = modlist2)
CookD
也抛出了一些故障在这里汇聚警告,但情节似乎没有问题,有影响的一点很明显。
我的回答对你有帮助吗?或者你需要更多的帮助来解决你的问题? – meenaparam