#Likelihood HW
#pdf for Poisson: (e^-λ * λχ)/χ!
#We now need to find the equation for the loglikelihood of Poisson's pdf
#LogLik = -15λ + log(λ)Σχ   , n=15
#To start we have to create a function of our log likelihood
llfunct <- function(lambda, sumx=100, n=15)
{
  sumx * log(lambda) + -n*lambda
}

#Choose a range of lamda values and plug them in to the loglikelihood function
lambda <- seq(.1,10, by = .1)
Likelihood <- llfunct(lambda)

#We will want our chosen lambda values on the x axis
#We want the log likelihood of these lambda values occuring on the y axis
#In this case our lamdba values range from 0-10
plot(lambda, Likelihood,type = 'l', xlab = "lambda", ylab = "LogLikelihood")

#Question 3
data("swiss")
attach(swiss)

#We want to determine whether to drop agriculture form the model
#To begin we make a model with all of our predictors
mod0 <- lm(Infant.Mortality~Fertility+Agriculture+Examination+Education+Catholic)

#And one model without Agriculture
mod1 <- lm(Infant.Mortality~Fertility+Examination+Education+Catholic)

#anova
anova(mod0,mod1)
## Analysis of Variance Table
## 
## Model 1: Infant.Mortality ~ Fertility + Agriculture + Examination + Education + 
##     Catholic
## Model 2: Infant.Mortality ~ Fertility + Examination + Education + Catholic
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     41 295.07                           
## 2     42 296.32 -1   -1.2563 0.1746 0.6783
#Since our p value is non significant, we can conclude that the model is not better without Agriculture according to the F test

#AIC
AIC(mod0)
## [1] 233.7217
AIC(mod1)
## [1] 231.9214
#Since mod1 is not 10 less than mod0, we can conclude mod1 is not significant

#BIC
BIC(mod0)
## [1] 246.6727
BIC(mod1)
## [1] 243.0222
#Since mod1 is not 10 less than mod0, we can conclude mod1 is not significantly better

#Likelihood ratio test
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

lrtest(mod0,mod1)
## Likelihood ratio test
## 
## Model 1: Infant.Mortality ~ Fertility + Agriculture + Examination + Education + 
##     Catholic
## Model 2: Infant.Mortality ~ Fertility + Examination + Education + Catholic
##   #Df  LogLik Df  Chisq Pr(>Chisq)
## 1   7 -109.86                     
## 2   6 -109.96 -1 0.1997      0.655
#Pvalue is .655 which is not a significant value, so model0 is okay

#Overall the model with Agriculture, while arguably worse, was not significantly worse than the model without it