#ELMR 3.4
require(ISLR)
require(leaps)
library("faraway")
data(africa, package="faraway")
names(africa)
## [1] "miltcoup"  "oligarchy" "pollib"    "parties"   "pctvote"   "popn"     
## [7] "size"      "numelec"   "numregim"
head(africa)
##          miltcoup oligarchy pollib parties pctvote popn size numelec
## Angola          0         0      2      38      NA  9.7 1247       0
## Benin           5         7      1      34   45.68  4.6  113       8
## Botswana        0         0     NA       7   20.30  1.2  582       5
## Burkina         6        13      2      62   17.50  8.8  274       5
## Burundi         2        13      2      10   34.39  5.3   28       3
## Cameroon        0         0      2      34   30.30 11.6  475      14
##          numregim
## Angola          1
## Benin           3
## Botswana        1
## Burkina         3
## Burundi         3
## Cameroon        3

LM shows oligarch, pollib and parties as being significant. Plot shows a trend and not spread of data.

lmod<-lm(miltcoup ~., africa)
plot(predict(lmod),residuals(lmod),xlab="Fitted",ylab="Residuals")

summary(lmod)
## 
## Call:
## lm(formula = miltcoup ~ ., data = africa)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.89967 -0.90106  0.00466  0.77182  2.60931 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  1.8360534  1.0887267   1.686  0.10324   
## oligarchy    0.1325624  0.0578618   2.291  0.02999 * 
## pollib      -1.2275514  0.4191202  -2.929  0.00684 **
## parties      0.0627987  0.0234284   2.680  0.01238 * 
## pctvote      0.0205813  0.0144650   1.423  0.16624   
## popn         0.0240960  0.0137765   1.749  0.09164 . 
## size        -0.0005773  0.0004559  -1.266  0.21631   
## numelec     -0.0502585  0.0878190  -0.572  0.57186   
## numregim    -0.1193285  0.3054271  -0.391  0.69909   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.264 on 27 degrees of freedom
##   (11 observations deleted due to missingness)
## Multiple R-squared:  0.6036, Adjusted R-squared:  0.4861 
## F-statistic: 5.138 on 8 and 27 DF,  p-value: 0.0005869

Stripchart shows some outliers on the size variable

stripchart(africa)

Set linear model with sqrt of response variable while adding log to predictor variable size. Parties variable is no longer signficant

lmod<-lm(sqrt(miltcoup) ~.-size+log(size), family=poisson, africa)
plot(predict(lmod),residuals(lmod),xlab="Fitted",ylab="Residuals")

summary(lmod)
## 
## Call:
## lm(formula = sqrt(miltcoup) ~ . - size + log(size), data = africa, 
##     family = poisson)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.71223 -0.35268 -0.05894  0.28168  0.92559 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  1.040669   0.455156   2.286  0.03030 * 
## oligarchy    0.071439   0.023298   3.066  0.00488 **
## pollib      -0.409679   0.170740  -2.399  0.02359 * 
## parties      0.022552   0.009549   2.362  0.02565 * 
## pctvote      0.011945   0.005872   2.034  0.05187 . 
## popn         0.008310   0.005673   1.465  0.15452   
## numelec     -0.004258   0.036190  -0.118  0.90721   
## numregim     0.071176   0.122296   0.582  0.56539   
## log(size)   -0.151683   0.055425  -2.737  0.01084 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5122 on 27 degrees of freedom
##   (11 observations deleted due to missingness)
## Multiple R-squared:  0.6581, Adjusted R-squared:  0.5568 
## F-statistic: 6.495 on 8 and 27 DF,  p-value: 0.0001003

Persons F-test shows pollit and parties with higher AIC values

lmod<-glm(miltcoup ~oligarchy+pollib+parties, family=poisson,  africa)
summary(lmod)
## 
## Call:
## glm(formula = miltcoup ~ oligarchy + pollib + parties, family = poisson, 
##     data = africa)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.4495  -1.0868  -0.3699   0.5723   1.7526  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.27416    0.36432   0.753  0.45172    
## oligarchy    0.09892    0.02012   4.916 8.82e-07 ***
## pollib      -0.51592    0.19511  -2.644  0.00819 ** 
## parties      0.01641    0.00865   1.897  0.05778 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 79.124  on 41  degrees of freedom
## Residual deviance: 42.242  on 38  degrees of freedom
##   (5 observations deleted due to missingness)
## AIC: 123.92
## 
## Number of Fisher Scoring iterations: 5
plot(predict(lmod),residuals(lmod),xlab="Fitted",ylab="Residuals")

halfnorm(residuals(lmod))

GLM with oligarch and polib

lmod<-glm(miltcoup ~oligarchy+pollib, family=poisson,  africa)
summary(lmod)
## 
## Call:
## glm(formula = miltcoup ~ oligarchy + pollib, family = poisson, 
##     data = africa)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.4078  -1.1368  -0.3286   0.4070   1.9516  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.41840    0.36544   1.145   0.2522    
## oligarchy    0.10079    0.01974   5.107 3.28e-07 ***
## pollib      -0.42752    0.19134  -2.234   0.0255 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 79.124  on 41  degrees of freedom
## Residual deviance: 45.498  on 39  degrees of freedom
##   (5 observations deleted due to missingness)
## AIC: 125.18
## 
## Number of Fisher Scoring iterations: 5
plot(predict(lmod),residuals(lmod),xlab="Fitted",ylab="Residuals")

halfnorm(residuals(lmod))

GLM with Parties and polib

lmod<-glm(miltcoup ~pollib+parties, family=poisson,  africa)
summary(lmod)
## 
## Call:
## glm(formula = miltcoup ~ pollib + parties, family = poisson, 
##     data = africa)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.9418  -1.4580  -0.1855   0.8156   2.6868  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  1.034517   0.305296   3.389 0.000703 ***
## pollib      -0.627335   0.182239  -3.442 0.000577 ***
## parties      0.022481   0.009247   2.431 0.015052 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 79.124  on 41  degrees of freedom
## Residual deviance: 65.847  on 39  degrees of freedom
##   (5 observations deleted due to missingness)
## AIC: 145.53
## 
## Number of Fisher Scoring iterations: 5
plot(predict(lmod),residuals(lmod),xlab="Fitted",ylab="Residuals")

halfnorm(residuals(lmod))