loglike <- function(lambda, sumx=100, n=15)
{
sumx* log(lambda) + -n*lambda
}
This function is used to find the loglikelihood when you have lamda
lambdavals <- seq(.1, 10, by = .1)
llvals <- loglike(lambdavals)
This is a way to create a vector with different lamda values to test
plot(lambdavals, llvals, type = "l", xlab = expression(lambda),
ylab = "log likelihood")
Here is a plot of a bunch of different lamda values being plotted on the loglikelihood function
Question 3
attach(swiss)
names(swiss)
## [1] "Fertility" "Agriculture" "Examination"
## [4] "Education" "Catholic" "Infant.Mortality"
mod1<-lm(Infant.Mortality ~ Fertility + Agriculture + Examination + Education + Catholic)
mod2<-lm(Infant.Mortality ~ Fertility + Examination + Education + Catholic)
I used names(swiss) to get all the variables to put into mod1. mod2 is the same as mod1 except without agriculture
A <- logLik(mod2)
B<- logLik(mod1)
teststat <- -2 * (as.numeric(A)-as.numeric(B))
p.val <- pchisq(teststat, df = 1, lower.tail = FALSE)
p.val
## [1] 0.6549756
The P value of the LRT is .6549 which is very high indicating Agriculture should not be removed
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
The P value of the ANOVA test is .6783 which is very high indicating Agriculture should not be removed
AIC(mod1)
## [1] 233.7217
AIC(mod2)
## [1] 231.9214
BIC(mod1)
## [1] 246.6727
BIC(mod2)
## [1] 243.0222
There is not a significant decrease in the AIC or BIC which suggests that it is best to keep Agriculutre in the model.