Morehouse et al. (2016) used genetic analysis to determine the parentage of bear cubs, and cross-classified cubs and their parents as causing problems around humans (see Table 4.2)

The data are here

Morehouse, A. T., Graves, T. A., Mikle, N. & Boyce, M. S. (2016). Nature vs. Nurture: Evidence for social learning of conflict behaviour in Grizzly Bears. PLoS One, 11.

Preliminaries

First, load the required packages (vcd)

Import morehouse data file

morehouse <- read.csv("../data/morehouse.csv")
head(morehouse)

Tabulate the data

morehouse.tab<-xtabs(count~mother + offspring, data=morehouse)
morehouse.tab
      offspring
mother  N  Y
     N 50 18
     Y  3  5

Calculate chi-sq. test and odds ratios

Output odds and odds ratio on raw, rather than log, scale

chisq.test(morehouse.tab, correct=F)$exp
Warning: Chi-squared approximation may be incorrect
      offspring
mother       N       Y
     N 47.4211 20.5789
     Y  5.5789  2.4211
morehouse.chi<- chisq.test(morehouse.tab, correct=F)
Warning: Chi-squared approximation may be incorrect
morehouse.chi

    Pearson's Chi-squared test

data:  morehouse.tab
X-squared = 4.4, df = 1, p-value = 0.036
morehouse.chi$residuals
      offspring
mother       N       Y
     N  0.3745 -0.5685
     Y -1.0919  1.6574
#Get odds
lodds(morehouse.tab, log = FALSE)
 odds for mother by offspring 

     N      Y 
16.667  3.600 
morehouse.odds <- loddsratio(morehouse.tab, log = FALSE)
summary(morehouse.odds)

z test of coefficients:

        Estimate Std. Error z value Pr(>|z|)
N:Y/N:Y     4.63       3.61    1.28      0.2
confint(morehouse.odds)
         2.5 % 97.5 %
N:Y/N:Y 1.0031 21.367

Run exact tests

fisher.test(morehouse.tab)

    Fisher's Exact Test for Count Data

data:  morehouse.tab
p-value = 0.05
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
  0.78881 32.10874
sample estimates:
odds ratio 
    4.5204 
oddsratio.midp(morehouse.tab)
Warning: Chi-squared approximation may be incorrect
$data
       offspring
mother   N  Y Total
  N     50 18    68
  Y      3  5     8
  Total 53 23    76

$measure
      odds ratio with 95% C.I.
mother estimate   lower  upper
     N   1.0000      NA     NA
     Y   4.4419 0.95041 25.039

$p.value
      two-sided
mother midp.exact fisher.exact chi.square
     N         NA           NA         NA
     Y   0.057989     0.049897   0.035881

$correction
[1] FALSE

attr(,"method")
[1] "median-unbiased estimate & mid-p exact CI"

Do loglinear model

morehouse.glm <- glm(count~mother*offspring, family=poisson, data=morehouse)
summary(morehouse.glm)

Call:
glm(formula = count ~ mother * offspring, family = poisson, data = morehouse)

Coefficients:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)           2.378      0.195   12.19  < 2e-16 ***
mother1               1.024      0.195    5.25  1.5e-07 ***
offspring1            0.128      0.195    0.65     0.51    
mother1:offspring1    0.383      0.195    1.96     0.05 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance:  7.0387e+01  on 3  degrees of freedom
Residual deviance: -4.8850e-15  on 0  degrees of freedom
AIC: 24.96

Number of Fisher Scoring iterations: 3
anova(morehouse.glm)
Analysis of Deviance Table

Model: poisson, link: log

Response: count

Terms added sequentially (first to last)

                 Df Deviance Resid. Df Resid. Dev
NULL                                 3       70.4
mother            1     54.2         2       16.2
offspring         1     12.2         1        4.0
mother:offspring  1      4.0         0        0.0
morehouse1.glm <- glm(count~mother+offspring, family=poisson, data=morehouse)
summary(morehouse1.glm)

Call:
glm(formula = count ~ mother + offspring, family = poisson, data = morehouse)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)    2.372      0.193   12.27  < 2e-16 ***
mother1        1.070      0.187    5.73    1e-08 ***
offspring1     0.417      0.125    3.34  0.00083 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 70.3870  on 3  degrees of freedom
Residual deviance:  4.0054  on 1  degrees of freedom
AIC: 26.97

Number of Fisher Scoring iterations: 5
anova(morehouse.glm, morehouse1.glm, test="Chisq")
Analysis of Deviance Table

Model 1: count ~ mother * offspring
Model 2: count ~ mother + offspring
  Resid. Df Resid. Dev Df Deviance Pr(>Chi)  
1         0       0.00                       
2         1       4.01 -1    -4.01    0.045 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
lrtest(morehouse.glm, morehouse1.glm)
Likelihood ratio test

Model 1: count ~ mother * offspring
Model 2: count ~ mother + offspring
  #Df LogLik Df Chisq Pr(>Chisq)  
1   4  -8.48                      
2   3 -10.48 -1  4.01      0.045 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
plot(morehouse1.glm)

Warning: NaNs producedWarning: NaNs produced

LS0tCnRpdGxlOiAiUSAmIEsgQm94IDEzLjUiCm91dHB1dDoKICAgIGh0bWxfbm90ZWJvb2s6CiAgICAgIHRoZW1lOmZsYXRseQotLS0KCmBgYHtyIHNldHVwLCBpbmNsdWRlPUZBTFNFfQprbml0cjo6b3B0c19jaHVuayRzZXQoZWNobyA9IFRSVUUpCmBgYAoKTW9yZWhvdXNlIGV0IGFsLiAoMjAxNikgdXNlZCBnZW5ldGljIGFuYWx5c2lzIHRvIGRldGVybWluZSB0aGUgcGFyZW50YWdlIG9mIGJlYXIgY3VicywgYW5kIGNyb3NzLWNsYXNzaWZpZWQgY3VicyBhbmQgdGhlaXIgcGFyZW50cyBhcyBjYXVzaW5nIHByb2JsZW1zIGFyb3VuZCBodW1hbnMgKHNlZSBUYWJsZSA0LjIpCgpUaGUgZGF0YSBhcmUgW2hlcmVdKC4uL2RhdGEvbW9yZWhvdXNlLmNzdikKCk1vcmVob3VzZSwgQS4gVC4sIEdyYXZlcywgVC4gQS4sIE1pa2xlLCBOLiAmIEJveWNlLCBNLiBTLiAoMjAxNikuIE5hdHVyZSB2cy4gTnVydHVyZTogRXZpZGVuY2UgZm9yIHNvY2lhbCB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