# load data
require(faraway)
## Loading required package: faraway
require(car)
## Loading required package: car
## 
## Attaching package: 'car'
## The following objects are masked from 'package:faraway':
## 
##     logit, vif
data(fpe)


species <- gala$Species
endemics <- gala$Endemics
area <- gala$Area
elevation <- gala$Elevation
nearest <- gala$Nearest
scruz <- gala$Scruz
adjacent <- gala$Adjacent

poisson_gala <- glm(
  species 
  ~ endemics 
  + area
  + elevation
  + nearest
  + scruz
  + adjacent,
  family = poisson(link = "log"))

summary(poisson_gala)
## 
## Call:
## glm(formula = species ~ endemics + area + elevation + nearest + 
##     scruz + adjacent, family = poisson(link = "log"))
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -4.9919  -2.9305  -0.4296   1.3254   7.4735  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  2.828e+00  5.958e-02  47.471  < 2e-16 ***
## endemics     3.388e-02  1.741e-03  19.459  < 2e-16 ***
## area        -1.067e-04  3.741e-05  -2.853  0.00433 ** 
## elevation    2.638e-04  1.934e-04   1.364  0.17264    
## nearest      1.048e-02  1.611e-03   6.502 7.91e-11 ***
## scruz       -6.835e-04  5.802e-04  -1.178  0.23877    
## adjacent     4.539e-05  4.800e-05   0.946  0.34437    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3510.73  on 29  degrees of freedom
## Residual deviance:  313.36  on 23  degrees of freedom
## AIC: 488.19
## 
## Number of Fisher Scoring iterations: 5
g <- lm(species ~ endemics + area + elevation + nearest + scruz + adjacent, data = gala)
lambda <- powerTransform(g)
lam <- lambda$lambda
boxcox_gala <- lm(species^lam ~ endemics + area + elevation + nearest + scruz + adjacent, data = gala)

summary(boxcox_gala)
## 
## Call:
## lm(formula = species^lam ~ endemics + area + elevation + nearest + 
##     scruz + adjacent, data = gala)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.6863 -1.0684 -0.3981  1.0026  3.8970 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.3931431  0.6368955   3.758  0.00103 ** 
## endemics     0.3350357  0.0325224  10.302 4.35e-10 ***
## area        -0.0002086  0.0007707  -0.271  0.78907    
## elevation   -0.0008701  0.0032168  -0.270  0.78920    
## nearest      0.0248540  0.0338516   0.734  0.47024    
## scruz       -0.0028778  0.0071562  -0.402  0.69130    
## adjacent     0.0001690  0.0008028   0.210  0.83516    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.958 on 23 degrees of freedom
## Multiple R-squared:  0.9623, Adjusted R-squared:  0.9525 
## F-statistic: 97.87 on 6 and 23 DF,  p-value: 3.33e-15
compareCoefs(poisson_gala, boxcox_gala, se = FALSE)
## 
## Call:
## 1: glm(formula = species ~ endemics + area + elevation + nearest + 
##   scruz + adjacent, family = poisson(link = "log"))
## 2: lm(formula = species^lam ~ endemics + area + elevation + nearest + 
##   scruz + adjacent, data = gala)
##                Est. 1    Est. 2
## (Intercept)  2.83e+00  2.39e+00
## endemics     3.39e-02  3.35e-01
## area        -1.07e-04 -2.09e-04
## elevation    2.64e-04 -8.70e-04
## nearest      1.05e-02  2.49e-02
## scruz       -6.83e-04 -2.88e-03
## adjacent     4.54e-05  1.69e-04
ei <- fpe$EI
a <- fpe$A
b <- fpe$B
c <- fpe$C
d <- fpe$D
e <- fpe$E
f <- fpe$F
g <- fpe$G
h <- fpe$H
j <- fpe$J
k <- fpe$K
a2 <- fpe$A2
b2 <- fpe$B2
n <- fpe$N

poisson_fpe <- glm(
  ei 
  ~ a 
  + b
  + c
  + d
  + e
  + f
  + g
  + h
  + j
  + k
  + a2
  + b2
  + n,
  family = poisson(link = "log"))

summary(poisson_fpe)
## 
## Call:
## glm(formula = ei ~ a + b + c + d + e + f + g + h + j + k + a2 + 
##     b2 + n, family = poisson(link = "log"))
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.6115  -1.0870  -0.0518   1.3408   4.1528  
## 
## Coefficients: (1 not defined because of singularities)
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  4.5477568  0.0528856  85.992  < 2e-16 ***
## a           -0.0418217  0.0125528  -3.332 0.000863 ***
## b           -0.0005237  0.0099014  -0.053 0.957818    
## c            0.0140079  0.0056616   2.474 0.013354 *  
## d           -0.0371494  0.0092584  -4.013 6.01e-05 ***
## e            0.0072545  0.0193684   0.375 0.707992    
## f           -0.0222218  0.0089555  -2.481 0.013088 *  
## g           -0.0002333  0.0242565  -0.010 0.992327    
## h           -0.0663190  0.0303968  -2.182 0.029126 *  
## j            0.0226055  0.0408362   0.554 0.579877    
## k           -0.1286581  0.0216346  -5.947 2.73e-09 ***
## a2           0.0371011  0.0105182   3.527 0.000420 ***
## b2           0.0012190  0.0080790   0.151 0.880066    
## n                   NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3389.10  on 23  degrees of freedom
## Residual deviance:  113.21  on 11  degrees of freedom
## AIC: 320.87
## 
## Number of Fisher Scoring iterations: 4
WLS_fpe1 <- lm(ei ~., fpe, weight = 1 / n)
WLS_fpe2 <- lm(ei ~., fpe, weight = 1 / (n^2))

summary(WLS_fpe1)
## Warning in summary.lm(WLS_fpe1): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = ei ~ ., data = fpe, weights = 1/n)
## 
## Weighted Residuals:
##        Min         1Q     Median         3Q        Max 
## -7.425e-14 -8.890e-15 -4.160e-16  1.593e-14  3.890e-14 
## 
## Coefficients: (1 not defined because of singularities)
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept) -3.205e-13  7.469e-14 -4.292e+00  0.00158 ** 
## EI           1.000e+00  7.323e-15  1.366e+14  < 2e-16 ***
## A           -5.452e-14  2.232e-14 -2.443e+00  0.03469 *  
## B            4.307e-14  2.018e-14  2.134e+00  0.05864 .  
## C            1.331e-14  1.466e-14  9.080e-01  0.38528    
## D           -3.773e-14  1.718e-14 -2.196e+00  0.05280 .  
## E           -7.335e-14  3.924e-14 -1.869e+00  0.09111 .  
## F           -3.554e-14  1.791e-14 -1.984e+00  0.07533 .  
## G           -1.533e-13  5.539e-14 -2.767e+00  0.01988 *  
## H            1.032e-13  5.991e-14  1.722e+00  0.11574    
## J            1.478e-13  7.577e-14  1.950e+00  0.07969 .  
## K            4.055e-14  5.636e-14  7.190e-01  0.48837    
## A2           5.422e-14  2.256e-14  2.403e+00  0.03709 *  
## B2          -2.484e-14  2.187e-14 -1.136e+00  0.28239    
## N                   NA         NA         NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.772e-14 on 10 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 5.44e+30 on 13 and 10 DF,  p-value: < 2.2e-16
summary(WLS_fpe2)
## Warning in summary.lm(WLS_fpe2): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = ei ~ ., data = fpe, weights = 1/(n^2))
## 
## Weighted Residuals:
##        Min         1Q     Median         3Q        Max 
## -4.047e-15 -1.257e-15  9.030e-17  1.344e-15  3.752e-15 
## 
## Coefficients: (1 not defined because of singularities)
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept) -8.229e-14  1.633e-14 -5.039e+00 0.000507 ***
## EI           1.000e+00  2.139e-15  4.675e+14  < 2e-16 ***
## A           -1.656e-14  5.917e-15 -2.798e+00 0.018857 *  
## B            1.071e-14  4.545e-15  2.357e+00 0.040189 *  
## C           -1.621e-16  3.959e-15 -4.100e-02 0.968153    
## D           -1.096e-14  4.548e-15 -2.409e+00 0.036741 *  
## E           -2.665e-14  1.029e-14 -2.590e+00 0.026969 *  
## F           -1.177e-14  4.899e-15 -2.404e+00 0.037089 *  
## G           -5.091e-14  1.459e-14 -3.489e+00 0.005834 ** 
## H            3.640e-14  1.539e-14  2.365e+00 0.039584 *  
## J            4.467e-14  1.737e-14  2.572e+00 0.027798 *  
## K            1.240e-14  1.653e-14  7.500e-01 0.470526    
## A2           1.748e-14  6.398e-15  2.732e+00 0.021131 *  
## B2          -3.700e-15  5.378e-15 -6.880e-01 0.507107    
## N                   NA         NA         NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.007e-15 on 10 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 7.903e+31 on 13 and 10 DF,  p-value: < 2.2e-16
compareCoefs(poisson_fpe, WLS_fpe1, WLS_fpe2)
## 
## Call:
## 1: glm(formula = ei ~ a + b + c + d + e + f + g + h + j + k + a2 + b2 
##   + n, family = poisson(link = "log"))
## 2: lm(formula = ei ~ ., data = fpe, weights = 1/n)
## Warning in summary.lm(object): essentially perfect fit: summary may be
## unreliable
## 
## 3: lm(formula = ei ~ ., data = fpe, weights = 1/(n^2))
## Warning in summary.lm(object): essentially perfect fit: summary may be
## unreliable
## 
##                Est. 1      SE 1    Est. 2      SE 2    Est. 3      SE 3
## (Intercept)  4.55e+00  5.29e-02 -3.21e-13  7.47e-14 -8.23e-14  1.63e-14
## a           -4.18e-02  1.26e-02                                        
## b           -5.24e-04  9.90e-03                                        
## c            1.40e-02  5.66e-03                                        
## d           -3.71e-02  9.26e-03                                        
## e            7.25e-03  1.94e-02                                        
## f           -2.22e-02  8.96e-03                                        
## g           -2.33e-04  2.43e-02                                        
## h           -6.63e-02  3.04e-02                                        
## j            2.26e-02  4.08e-02                                        
## k           -1.29e-01  2.16e-02                                        
## a2           3.71e-02  1.05e-02                                        
## b2           1.22e-03  8.08e-03                                        
## n                                                                      
## EI                               1.00e+00  7.32e-15  1.00e+00  2.14e-15
## A                               -5.45e-14  2.23e-14 -1.66e-14  5.92e-15
## B                                4.31e-14  2.02e-14  1.07e-14  4.55e-15
## C                                1.33e-14  1.47e-14 -1.62e-16  3.96e-15
## D                               -3.77e-14  1.72e-14 -1.10e-14  4.55e-15
## E                               -7.34e-14  3.92e-14 -2.67e-14  1.03e-14
## F                               -3.55e-14  1.79e-14 -1.18e-14  4.90e-15
## G                               -1.53e-13  5.54e-14 -5.09e-14  1.46e-14
## H                                1.03e-13  5.99e-14  3.64e-14  1.54e-14
## J                                1.48e-13  7.58e-14  4.47e-14  1.74e-14
## K                                4.05e-14  5.64e-14  1.24e-14  1.65e-14
## A2                               5.42e-14  2.26e-14  1.75e-14  6.40e-15
## B2                              -2.48e-14  2.19e-14 -3.70e-15  5.38e-15
## N