#2

loglike = function(lam, sumx = 100, n = 15) #log likelihood function
{
sumx * log(lam) + -n*lam
}

lamvals = seq(.1, 10, by = 0.1) #range of lambda values
llvals = loglike(lamvals) #log likelihood values
plot(lamvals, llvals, type = "l", xlab = expression(lambda), ylab = "log likelihood") #plot of lambda values x log likelihood values

max(llvals) #local maximum
## [1] 89.71075
#3

mod1 = lm(Infant.Mortality ~ Fertility + Agriculture + Examination + Education + Catholic, data = swiss) #model with Agriculture
mod2 = lm(Infant.Mortality ~ Fertility + Examination + Education + Catholic, data = swiss) #model without Agriculture

#a: likelihood ratio test
2*(logLik(mod1)-logLik(mod2))
## 'log Lik.' 0.1996846 (df=7)
#df = 5 - 4 = 1
#We would expect the test statistic to be = 1, but it's actually 0.1997, which favors the smaller model (the one without Agriculture). 

#b: Wald test
summary(mod1)
## 
## Call:
## lm(formula = Infant.Mortality ~ Fertility + Agriculture + Examination + 
##     Education + Catholic, data = swiss)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.2512 -1.2860  0.1821  1.6914  6.0937 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  8.667e+00  5.435e+00   1.595  0.11850   
## Fertility    1.510e-01  5.351e-02   2.822  0.00734 **
## Agriculture -1.175e-02  2.812e-02  -0.418  0.67827   
## Examination  3.695e-02  9.607e-02   0.385  0.70250   
## Education    6.099e-02  8.484e-02   0.719  0.47631   
## Catholic     6.711e-05  1.454e-02   0.005  0.99634   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.683 on 41 degrees of freedom
## Multiple R-squared:  0.2439, Adjusted R-squared:  0.1517 
## F-statistic: 2.645 on 5 and 41 DF,  p-value: 0.03665
#The p-value for Agriculture is > 0.05, so the coefficient for Agriculture isn't significantly different from zero.

#c: F-test/anova
anova(mod2, mod1)
## Analysis of Variance Table
## 
## Model 1: Infant.Mortality ~ Fertility + Examination + Education + Catholic
## Model 2: Infant.Mortality ~ Fertility + Agriculture + Examination + Education + 
##     Catholic
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     42 296.32                           
## 2     41 295.07  1    1.2563 0.1746 0.6783
#Since the p-value is > 0.05, adding Agriculture to the model doesn't lead to a better fit.

#d: AIC
AIC(mod1)
## [1] 233.7217
AIC(mod2)
## [1] 231.9214
#Since the AIC for mod2 is lower than the AIC for mod1, the model without Agriculture is better than the larger model.

#e: BIC
BIC(mod1)
## [1] 246.6727
BIC(mod2)
## [1] 243.0222
#Since the BIC for mod2 is lower than the BIC for mod1, the model without Agriculture is better than the larger model.

#The results from each of these tests indicate that Agriculture should be dropped from the model.