library(tidyverse)
library(tidymodels)
library(pROC)
library(knitr)
library(kableExtra)
# set default theme in ggplot2
::theme_set(ggplot2::theme_bw()) ggplot2
Logistic Regression: Inference
Announcements
Lab 07 due TODAY at 11:59pm
Team Feedback (email from TEAMMATES) due TODAY at 11:59pm (check email)
HW 04 due April 10 at 11:59pm
Next project milestone: Draft and peer review in Friday’s lab
Exam 02 - April 17 (same format as Exam 01)
- Exam 02 practice + lecture recordings available
Statistics experience due April 22
Questions from this week’s content?
Topics
Test of significance for a subset of predictors
Inference for a single predictor
Computational setup
Risk of coronary heart disease
This data set is from an ongoing cardiovascular study on residents of the town of Framingham, Massachusetts. We want to examine the relationship between various health characteristics and the risk of having heart disease.
high_risk
:- 1: High risk of having heart disease in next 10 years
- 0: Not high risk of having heart disease in next 10 years
age
: Age at exam time (in years)totChol
: Total cholesterol (in mg/dL)currentSmoker
: 0 = nonsmoker, 1 = smokereducation
: 1 = Some High School, 2 = High School or GED, 3 = Some College or Vocational School, 4 = College
Modeling risk of coronary heart disease
Using age
, totChol
, and currentSmoker
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
Drop-in-deviance test
Drop-in-deviance test
We will use a drop-in-deviance test (Likelihood Ratio Test) to test
the overall statistical significance of a logistic regression model
the statistical significance of a subset of coefficients in the model
Deviance
The deviance is a measure of the degree to which the predicted values are different from the observed values (compares the current model to a “saturated” model)
In logistic regression,
Note:
distribution
Test for overall significance
We can test the overall significance for a logistic regression model, i.e., whether there is at least one predictor with a non-zero coefficient
. . .
The drop-in-deviance test for overall significance compares the fit of a model with no predictors to the current model.
Drop-in-deviance test statistic
Let
where
Drop-in-deviance test statistic
. . .
When
is large, , ( follows a Chi-square distribution with degrees of freedom)2The p-value is calculated as
Large values of
(small p-values) indicate at least one is non-zero
Heart disease model: drop-in-deviance test
. . .
Fit the null model (we’ve already fit the alternative model)
<- glm(high_risk ~ 1, data = heart_disease, family = "binomial") null_model
term | estimate | std.error | statistic | p.value |
---|---|---|---|---|
(Intercept) | -1.72294 | 0.0436342 | -39.486 | 0 |
Heart disease model: drop-in-deviance test
Calculate the log-likelihood for the null and alternative models
<- glance(null_model)$logLik) (L_0
[1] -1737.735
<- glance(high_risk_fit)$logLik) (L_a
[1] -1612.406
. . .
Calculate the likelihood ratio test statistic
<- -2 * (L_0 - L_a)) (G
[1] 250.6572
. . .
Heart disease model: likelihood ratio test
Calculate the p-value
<- pchisq(G, df = 3, lower.tail = FALSE)) (p_value
[1] 4.717158e-54
. . .
Conclusion
The p-value is small, so we reject
Why use overall test?
Why do we use a test for overall significance instead of just looking at the test for individual coefficients?3
. . .
Suppose we have a model such that
. . .
About 5% of the p-values for individual coefficients will be below 0.05 by chance.
So we expect to see 5 small p-values if even no linear association actually exists.
Therefore, it is very likely we will see at least one small p-value by chance.
The overall test of significance does not have this problem. There is only a 5% chance we will get a p-value below 0.05, if a relationship truly does not exist.
Test a subset of coefficients
Testing a subset of coefficients
Suppose there are two models:
Reduced Model: includes predictors
Full Model: includes predictors
We can use a drop-in-deviance test to determine if any of the new predictors are useful
. . .
Drop-in-deviance test
. . .
The test statistic is
. . .
The p-value is calculated using a
Example: Include education
?
Should we include education
in the model?
Reduced model:
age
,totChol
,currentSmoker
Full model:
age
,totChol
,currentSmoker
,education
. . .
Example: Include education
?
<- glm(high_risk ~ age + totChol + currentSmoker,
reduced_model data = heart_disease, family = "binomial")
<- glm(high_risk ~ age + totChol + currentSmoker + education,
full_model data = heart_disease, family = "binomial")
. . .
Calculate deviances
<- -2 * glance(reduced_model)$logLik) (deviance_reduced
[1] 3224.812
<- -2 * glance(full_model)$logLik) (deviance_full
[1] 3217.6
. . .
Calculate test statistic
<- deviance_reduced - deviance_full) (G
[1] 7.212113
Example: Include education
?
Calculate p-value
pchisq(G, df = 3, lower.tail = FALSE)
[1] 0.06543567
. . .
What is your conclusion? Would you include education
in the model that already has age
, totChol
, currentSmoker
?
Drop-in-deviance test in R
Conduct the drop-in-deviance test using the anova()
function in R with option test = "Chisq"
anova(reduced_model, full_model, test = "Chisq") |>
tidy() |>
kable(digits = 3)
term | df.residual | residual.deviance | df | deviance | p.value |
---|---|---|---|---|---|
high_risk ~ age + totChol + currentSmoker | 4082 | 3224.812 | NA | NA | NA |
high_risk ~ age + totChol + currentSmoker + education | 4079 | 3217.600 | 3 | 7.212 | 0.065 |
Add interactions with currentSmoker
?
term | df.residual | residual.deviance | df | deviance | p.value |
---|---|---|---|---|---|
high_risk ~ age + totChol + currentSmoker | 4082 | 3224.812 | NA | NA | NA |
high_risk ~ age + totChol + currentSmoker + currentSmoker * age + currentSmoker * totChol | 4080 | 3222.377 | 2 | 2.435 | 0.296 |
Test for a single coefficient
Distribution of
When
How do we know the distribution of
Distribution of
When
The expected value of
. . .
where
Test for a single coefficient
Hypotheses:
. . .
(Wald) Test Statistic:
where
. . .
P-value:
Confidence interval for
We can calculate the C% confidence interval for
where
. . .
This is an interval for the change in the log-odds for every one unit increase in
Interpretation in terms of the odds
The change in odds for every one unit increase in
. . .
Interpretation: We are
Coefficient for age
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
. . .
Hypotheses:
Coefficient for age
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
Test statistic:
Coefficient for age
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
P-value:
. . .
2 * pnorm(14.34,lower.tail = FALSE)
[1] 1.230554e-46
Coefficient for age
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
Conclusion:
The p-value is very small, so we reject
CI for age
term | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|
(Intercept) | -6.673 | 0.378 | -17.647 | 0.000 | -7.423 | -5.940 |
age | 0.082 | 0.006 | 14.344 | 0.000 | 0.071 | 0.094 |
totChol | 0.002 | 0.001 | 1.940 | 0.052 | 0.000 | 0.004 |
currentSmoker1 | 0.443 | 0.094 | 4.733 | 0.000 | 0.260 | 0.627 |
Interpret the 95% confidence interval for age
in terms of the odds of being high risk for heart disease.
Overview of testing coefficients
Test a single coefficient
Drop-in-deviance test
Wald hypothesis test and confidence interval
. . .
Test a subset of coefficients
- Drop-in-deviance test
. . .
Can use AIC and BIC to compare models in both scenarios
Questions from this week’s content?
Recap
Introduced test of significance for a subset of predictors
Inference for a single predictor
References
Footnotes
Based on Wilk’s Theorem (Wilks 1935)↩︎
Example from Introduction to Statistical Learning↩︎
Based on Wilk’s Theorem (Wilks 1935)↩︎