寻找厨师在R中的科目内的距离

时间:2017-08-16 12:58:36

标签: r

我有一个包含700个不同帐户的数据集。每个帐户都有多行,带有x和y变量。所以总共有大约26000行数据。我试图在特定帐户中找到“有影响力”的点。我使用了lmlist函数来获得GLR的估计系数。

model3 <- lmList(y ~ x | grp, data = dat)

我试图使用影响力然后使用CookD函数,但我认为它与lmList不兼容。

我知道烹饪功能可以与lmer一起使用,但是lmer会发出一个关于无法与我的数据集融合的警告:

model1 <- lmer(y ~ x + (x | grp) , data = dat)

我已经阅读了很多关于此的帖子,但仍然感到困惑。我希望我有更多的专业知识来写出我想要实现的目标。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

要考虑的主要问题是nlme::lmList会返回list个对象,因此您必须在其上使用列表方法。

使用iris数据上的组合模型,有两种方法可以做到这一点。第一种方法估计每组的单独模型,而第二种方式适合多级模型,观察嵌套在组内。

首先,使用lmList中的nlme和来自cooks.distance R的base

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

现在得到库克的距离:

cooks1 <- lapply(modlist, cooks.distance)

其次,使用lmList中的lme4CookD中的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

获取库克的距离和相关情节:

cooks2 <- CookD(model = modlist2)

CookD也引发了一些未能在这里收集警告的情况,但情节似乎还不错,有影响力的观点非常明显。