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.
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
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
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"
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