Skip to content

Instantly share code, notes, and snippets.

@iracooke
Last active August 29, 2015 14:05
Show Gist options
  • Save iracooke/05f8c97e28434254a45b to your computer and use it in GitHub Desktop.
Save iracooke/05f8c97e28434254a45b to your computer and use it in GitHub Desktop.
Flysleep Results

If we condense the data completely (ie average over the results on each day) we have a data frame that looks like this

>  cdatadaynum
   biorep daynight treatment count nflies
1       1      day        cs 12620   2662
2       1      day       trh  8371   1509
3       1    night        cs 10820   1972
4       1    night       trh  6132    987
5       2      day        cs 18783   4520
6       2      day       trh 25412   4820
7       2    night        cs 12897   2237
8       2    night       trh 11446   1834
9       3      day        cs 18449   4247
10      3      day       trh 21226   3329
11      3    night        cs  9597   1740
12      3    night       trh  8356   1398
13      4      day        cs 17858   4549
14      4      day       trh 13668   2926
15      4    night        cs 13580   2525
16      4    night       trh  9861   1605
17      5      day        cs 14132   3498
18      5      day       trh 20023   4165
19      5    night        cs  8392   1604
20      5    night       trh 18655   3076
> 

This is easy to analyze using a GLM with nflies as the offset

> gm1=glm(count~offset(log(nflies))+treatment+daynight,cdatadaynum,family=poisson)

And the results look like this

> summary(gm1)

Call:
glm(formula = count ~ offset(log(nflies)) + treatment + daynight, 
    family = poisson, data = cdatadaynum)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max 
-12.579   -5.726   -1.290    3.974   29.411 

Coefficients:
              Estimate Std. Error z value Pr(>|z|) 
(Intercept)   1.460224   0.003090  472.50   <2e-16 ***
treatmenttrh  0.183433   0.003779   48.54   <2e-16 ***
daynightnight 0.204349   0.003870   52.80   <2e-16 ***
---
Signif. codes:  0***0.001**0.01*0.05.0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 6879.7  on 19  degrees of freedom
Residual deviance: 1747.9  on 17  degrees of freedom
AIC: 1980.3

Number of Fisher Scoring iterations: 4

As it happens we can get an almost identical result via a much fancier statistical route. A Generalized Linear Mixed Effects Model. This performs no averaging across the days for a given experiment, but instead treats days as a random effect nested within biological replicates.

> gmr1=glmer(count~offset(log(nflies))+treatment+daynight+(1|biorep)+(1|biorep:daynum),cdata,family=poisson) # Wins in AIC tests
> summary(gmr1)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: poisson  ( log )
Formula: count ~ offset(log(nflies)) + treatment + daynight + (1 | biorep) +      (1 | biorep:daynum)
   Data: cdata

     AIC      BIC   logLik deviance df.resid 
  2122.7   2133.1  -1056.3   2112.7       55 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-7.9337 -3.7030 -0.1025  2.8903 16.3533 

Random effects:
 Groups        Name        Variance  Std.Dev.
 biorep:daynum (Intercept) 0.0004911 0.02216 
 biorep        (Intercept) 0.0028091 0.05300 
Number of obs: 60, groups:  biorep:daynum, 15; biorep, 5

Fixed effects:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept)   1.459669   0.024584   59.38   <2e-16 ***
treatmenttrh  0.190293   0.003834   49.64   <2e-16 ***
daynightnight 0.209005   0.003889   53.74   <2e-16 ***
---
Signif. codes:  0***0.001**0.01*0.05.0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) trtmnt
treatmnttrh -0.078       
daynghtnght -0.062 -0.006

Also a model with day-night interaction effect

> gmr1=glmer(count~offset(log(nflies))+treatment*daynight+(1|biorep)+(1|biorep:daynum),cdata,family=poisson) # Wins in AIC tests
> summary(gmr1)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: poisson  ( log )
Formula: count ~ offset(log(nflies)) + treatment * daynight + (1 | biorep) +      (1 | biorep:daynum)
   Data: cdata

     AIC      BIC   logLik deviance df.resid 
  1906.6   1919.2   -947.3   1894.6       54 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-8.6709 -3.9065  0.3286  2.3405 14.4777 

Random effects:
 Groups        Name        Variance  Std.Dev.
 biorep:daynum (Intercept) 0.0004911 0.02216 
 biorep        (Intercept) 0.0027018 0.05198 
Number of obs: 60, groups:  biorep:daynum, 15; biorep, 5

Fixed effects:
                            Estimate Std. Error z value Pr(>|z|)    
(Intercept)                 1.436783   0.024197   59.38   <2e-16 ***
treatmenttrh                0.234689   0.004874   48.15   <2e-16 ***
daynightnight               0.267185   0.005520   48.40   <2e-16 ***
treatmenttrh:daynightnight -0.114671   0.007766  -14.77   <2e-16 ***
---
Signif. codes:  0***0.001**0.01*0.05.0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) trtmnt dynght
treatmnttrh -0.103              
daynghtnght -0.092  0.450       
trtmnttrh:d  0.065 -0.617 -0.709
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment