eleph <- read.csv("http://cknudson.com/data/elephant.csv")
attach(eleph)
hist(MATINGS)

modE <- lm(MATINGS ~ AGE)
resE <- residuals(modE)
plot(AGE , MATINGS)
abline(modE)

residuals(MATINGS ~ AGE)
## NULL
plot(resE)

agemat<-c(mean(subset(eleph, AGE==27)$MATINGS),mean(subset(eleph, AGE==28)$MATINGS),mean(subset(eleph, AGE==29)$MATINGS),mean(subset(eleph, AGE==30)$MATINGS),mean(subset(eleph, AGE==32)$MATINGS),mean(subset(eleph, AGE==33)$MATINGS),mean(subset(eleph, AGE==34)$MATINGS),mean(subset(eleph, AGE==36)$MATINGS),mean(subset(eleph, AGE==37)$MATINGS),mean(subset(eleph, AGE==38)$MATINGS),mean(subset(eleph, AGE==39)$MATINGS),mean(subset(eleph, AGE==41)$MATINGS),mean(subset(eleph, AGE==42)$MATINGS),mean(subset(eleph, AGE==43)$MATINGS),mean(subset(eleph, AGE==44)$MATINGS),mean(subset(eleph, AGE==45)$MATINGS),mean(subset(eleph, AGE==47)$MATINGS),mean(subset(eleph, AGE==48)$MATINGS),mean(subset(eleph, AGE==52)$MATINGS))
agemat
##  [1] 0.000000 1.500000 1.000000 1.000000 2.000000 3.000000 1.750000
##  [8] 5.500000 2.666667 2.000000 1.000000 3.000000 4.000000 3.600000
## [15] 3.000000 5.000000 7.000000 2.000000 9.000000
a2<-log(agemat)
plot(a2)

ages<-c(27,28,29,30,32,33,34,36,37,38,39,41,42,43,44,45,47,48,52)

plot(ages, a2)

#log of matings is a better determiner of age than just age, and as age goes up so does matings
#No evidence for quadratic, looks more linear
attach(eleph)
## The following objects are masked from eleph (pos = 3):
## 
##     AGE, MATINGS
AGEexp <- exp(AGE)
AGEsq <- AGE^2

modE2 <- glm(MATINGS ~ AGE, family = "poisson")
modE3 <- glm(MATINGS ~ AGEsq, family = "poisson")
modE4 <- glm(MATINGS ~ AGEexp, family = "poisson")
modE5 <- glm(MATINGS ~ 1, family = "poisson")
modE6 <- glm(MATINGS ~ AGEexp + AGE, family = "poisson")
summary(modE6)
## 
## Call:
## glm(formula = MATINGS ~ AGEexp + AGE, family = "poisson")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.74369  -0.87056  -0.09645   0.55428   2.28465  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.373e+00  6.147e-01  -2.234   0.0255 *  
## AGEexp       7.886e-24  1.091e-23   0.723   0.4696    
## AGE          6.276e-02  1.604e-02   3.914 9.09e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 50.509  on 38  degrees of freedom
## AIC: 157.95
## 
## Number of Fisher Scoring iterations: 5
summary(modE2)
## 
## Call:
## glm(formula = MATINGS ~ AGE, family = "poisson")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.80798  -0.86137  -0.08629   0.60087   2.17777  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.58201    0.54462  -2.905  0.00368 ** 
## AGE          0.06869    0.01375   4.997 5.81e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 51.012  on 39  degrees of freedom
## AIC: 156.46
## 
## Number of Fisher Scoring iterations: 5
summary(modE3)
## 
## Call:
## glm(formula = MATINGS ~ AGEsq, family = "poisson")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.76066  -0.84290  -0.06587   0.58710   2.25632  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -0.2588755  0.2847181  -0.909    0.363    
## AGEsq        0.0008635  0.0001711   5.047  4.5e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 51.590  on 39  degrees of freedom
## AIC: 157.04
## 
## Number of Fisher Scoring iterations: 5
summary(modE4)
## 
## Call:
## glm(formula = MATINGS ~ AGEexp, family = "poisson")
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.2462  -1.0928  -0.3413   0.2920   3.1527  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept) 9.252e-01  9.958e-02   9.290  < 2e-16 ***
## AGEexp      3.327e-23  9.076e-24   3.666 0.000247 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 65.782  on 39  degrees of freedom
## AIC: 171.23
## 
## Number of Fisher Scoring iterations: 5
summary(modE5)
## 
## Call:
## glm(formula = MATINGS ~ 1, family = "poisson")
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.3164  -1.1799  -0.4368   0.1899   3.0252  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.98691    0.09535   10.35   <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: 75.372  on 40  degrees of freedom
## Residual deviance: 75.372  on 40  degrees of freedom
## AIC: 178.82
## 
## Number of Fisher Scoring iterations: 5
sqrt(AGE^2)
##  [1] 27 28 28 28 28 29 29 29 29 29 29 30 32 33 33 33 33 33 34 34 34 34 36
## [24] 36 37 37 37 38 39 41 42 43 43 43 43 43 44 45 47 48 52
#and increase in 1 for age leads to an incrase in 1.03 matings 

modE2 <- glm(MATINGS ~ AGE, family = "poisson")

confint(modE4, parm="AGEexp",level = .95)
## Waiting for profiling to be done...
##        2.5 %       97.5 % 
## 1.356193e-23 4.956250e-23
teststat<-deviance(modE2)-deviance(modE5)
pchisq(teststat,df=1,lower.tail=FALSE)
## [1] 1
attach(eleph)
## The following objects are masked from eleph (pos = 3):
## 
##     AGE, MATINGS
## The following objects are masked from eleph (pos = 4):
## 
##     AGE, MATINGS
modE7 <- glm(MATINGS ~ AGE , family = "poisson")
modE8 <- glm(MATINGS ~  AGEsq, family = "poisson")
modE9 <- glm(MATINGS ~  AGEsq + AGE, family = "poisson")
summary(modE7)
## 
## Call:
## glm(formula = MATINGS ~ AGE, family = "poisson")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.80798  -0.86137  -0.08629   0.60087   2.17777  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.58201    0.54462  -2.905  0.00368 ** 
## AGE          0.06869    0.01375   4.997 5.81e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 51.012  on 39  degrees of freedom
## AIC: 156.46
## 
## Number of Fisher Scoring iterations: 5
summary(modE8)
## 
## Call:
## glm(formula = MATINGS ~ AGEsq, family = "poisson")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.76066  -0.84290  -0.06587   0.58710   2.25632  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -0.2588755  0.2847181  -0.909    0.363    
## AGEsq        0.0008635  0.0001711   5.047  4.5e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 75.372  on 40  degrees of freedom
## Residual deviance: 51.590  on 39  degrees of freedom
## AIC: 157.04
## 
## Number of Fisher Scoring iterations: 5
teststat2 <-deviance(modE9)-deviance(modE7)
pchisq(teststat2 ,df=1, lower.tail=FALSE)
## [1] 1