Search code examples
rcsvlme4summary

How to extract scaled residuals from glmer summary


I am trying to write a .csv file that appends the important information from the summary of a glmer analysis (from the package lme4).

I have been able to isolate the coefficients, AIC, and random effects , but I have not been able to isolate the scaled residuals (Min, 1Q, Median, 3Q, Max).

I have tried using $residuals, but I get a very long output, not the information shown in the summary.

> 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 

I can isolate information above by using the call summary(aes.glmer)$AIC

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

I do not know the call to isolate the above information

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

I can isolate this information using the ranef function

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

And I can isolate the information above using 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

If anyone knows what call I can use to isolate the "scaled residuals" I would be very greatful.


Solution

  • I haven't got your data, so we'll use example data from the lme4 vignette.

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

    This is for the residuals. tidy from the broom package puts it in to a tibble, which you can then export to a 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 
    

    Also here are some of the other bits that you might find useful, using glance from 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
    

    And

    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