如何从glmer摘要中提取缩放后的残差

时间:2019-09-17 20:58:47

标签: r csv lme4 summary

我正在尝试编写一个.csv文件,该文件附加了glmer分析摘要中的重要信息(来自软件包lme4)。

我能够隔离系数,AIC和随机效应,但无法隔离缩放后的残差(最小值,1Q,中位数,3Q,最大值)。

我曾尝试使用$ residuals,但得到的输出非常长,而不是摘要中显示的信息。

> library(lme4)
> setwd("C:/Users/Arthur Scully/Dropbox/! ! ! ! PHD/Chapter 2 Lynx Bobcat BC/ResourceSelection")
> #simple vectors
> 
> x <- c("a","b","b","b","b","d","b","c","c","a")
> 
> y <- c(1,1,0,1,0,1,1,1,1,0)
>  
> 
> # Simple data frame
> 
> aes.samp <- data.frame(x,y)
> aes.samp
   x y
1  a 1
2  b 1
3  b 0
4  b 1
5  b 0
6  d 1
7  b 1
8  c 1
9  c 1
10 a 0
> 
> # Simple glmer
> 
> aes.glmer <- glmer(y~(1|x),aes.samp,family ="binomial")
boundary (singular) fit: see ?isSingular
> 
> summary(aes.glmer)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
Formula: y ~ (1 | x)
   Data: aes.samp

     AIC      BIC   logLik deviance df.resid 
    16.2     16.8     -6.1     12.2        8 

我可以使用呼叫摘要(aes.glmer)$ AIC隔离上述信息

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-1.5275 -0.9820  0.6546  0.6546  0.6546 

我不知道要隔离上述信息的电话

Random effects:
 Groups Name        Variance Std.Dev.
 x      (Intercept) 0        0       
Number of obs: 10, groups:  x, 4

我可以使用ranef函数隔离此信息

Fixed effects:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.8473     0.6901   1.228     0.22

我可以使用summary(aes.glmer)$ coefficient

隔离上述信息。
convergence code: 0
boundary (singular) fit: see ?isSingular
> 
> #Pull important
> ##write call to select important output
> aes.glmer.coef <- summary(aes.glmer)$coefficient
> aes.glmer.AIC <- summary(aes.glmer)$AIC
> aes.glmer.ran <-ranef(aes.glmer)
> 
> ##
> data.frame(c(aes.glmer.coef, aes.glmer.AIC, aes.glmer.ran))
  X0.847297859077025 X0.690065555425105 X1.22785125618255 X0.219502810378876      AIC      BIC    logLik deviance df.resid X.Intercept.
a          0.8472979          0.6900656          1.227851          0.2195028 16.21729 16.82246 -6.108643 12.21729        8            0
b          0.8472979          0.6900656          1.227851          0.2195028 16.21729 16.82246 -6.108643 12.21729        8            0
c          0.8472979          0.6900656          1.227851          0.2195028 16.21729 16.82246 -6.108643 12.21729        8            0
d          0.8472979          0.6900656          1.227851          0.2195028 16.21729 16.82246 -6.108643 12.21729        8            0

如果有人知道我可以用来隔离“缩放残差”的呼叫,我将非常感激。

1 个答案:

答案 0 :(得分:0)

我没有您的数据,因此我们将使用lme4小插图中的示例数据。

library(lme4)
library(lattice)
library(broom)

gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
             data = cbpp, family = binomial)

这是用于残差的。 tidy软件包中的broom将其放入小标题,然后可以将其导出到csv。

x <- tidy(quantile(residuals(gm1, "pearson", scaled = TRUE)))
x

# A tibble: 5 x 2
  names      x
  <chr>  <dbl>
1 0%    -2.38 
2 25%   -0.789
3 50%   -0.203
4 75%    0.514
5 100%   2.88 

使用glance中的broom,还有一些其他有用的地方。

y <- glance(gm1)
y

# A tibble: 1 x 6
  sigma logLik   AIC   BIC deviance df.residual
  <dbl>  <dbl> <dbl> <dbl>    <dbl>       <int>
1     1  -92.0  194.  204.     73.5          51

z <- tidy(gm1)
z

# A tibble: 5 x 6
  term                estimate std.error statistic  p.value group
  <chr>                  <dbl>     <dbl>     <dbl>    <dbl> <chr>
1 (Intercept)           -1.40      0.231     -6.05  1.47e-9 fixed
2 period2               -0.992     0.303     -3.27  1.07e-3 fixed
3 period3               -1.13      0.323     -3.49  4.74e-4 fixed
4 period4               -1.58      0.422     -3.74  1.82e-4 fixed
5 sd_(Intercept).herd    0.642    NA         NA    NA       herd 
相关问题