--- title: "likelihood_ratio" author: "Prof. Eric A. Suess" format: html: self-contained: true --- ## Examples of using the generalized likelihood ratio test. Using the `mtcars` dataset, we will compare the full model to the reduced model. The full model includes four of the variables in the dataset. The reduced model includes only the variables `disp` and `carb`. The null model includes only the intercept. We will use the `lrtest()` function from the `lmtest` package to perform the likelihood ratio test. # Likelihood Ratio Test for Linear Regression ```{r} #| messages = FALSE, warning = FALSE library(lmtest) #fit full model model_full <- lm(mpg ~ disp + carb + hp + cyl, data = mtcars) model_full #fit reduced model model_reduced <- lm(mpg ~ disp + carb, data = mtcars) model_reduced #fit nullmodel model_null <- lm(mpg ~ 1, data = mtcars) model_null #perform likelihood ratio test for differences in models lrtest(model_full, model_reduced) lrtest(model_full, model_null) lrtest(model_reduced, model_null) ``` Check the calculations. ```{r} -2*log( exp(-78.60301) / exp(-77.55790) ) -2*( - 78.60301 - (- 77.55790)) 2*( 78.60301 + (- 77.55790)) pchisq(2.04522, df = 2, lower.tail = FALSE) ``` # Likelihood ratio test for logistic regression Using the `infert` dataset, we will compare the full model to the reduced model. The full model includes two of the variables in the dataset. The reduced model includes only the variable `induced`. The null model includes only the intercept. We will use the `lrtest()` function from the `lmtest` package to perform the likelihood ratio test. ```{r} infert model_full <- glm(case ~ induced + spontaneous, family=binomial, data=infert) model_full model_reduced <- glm(case ~ induced, family=binomial, data=infert) model_reduced model_null <- glm(case ~ 1, family=binomial, data=infert) model_null lrtest (model_full, model_reduced) lrtest (model_full, model_null) lrtest (model_reduced, model_null) ```