General Linear Models:
Statistical Inference

Introduction

  • In our previous lectures, we discussed correlation and estimation of regression coefficients.

    • Correlation is unitless.

    • Regression coefficients retain the units of x and y for interpretations.

  • In this lecture, we introduce statistical inference for regression:

    • Confidence intervals for \beta_i

    • Overall test for a significant regression line

    • Test for individual predictors

R Set Up

library(tidyverse)
library(ssstats)
library(broom)
training <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-1-training.csv")
wellness <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-2-wellness.csv")
reports <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-3-reports.csv")
break_room <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-4-break_room.csv")
bakery_sales <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-5-bakery_sales.csv")
cafe_sales <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/7-6-cafe_sales.csv")

Regression Assumptions

  • Recall the general linear model,

y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_k x_k + \varepsilon

  • Like in ANOVA, we assume

\varepsilon \sim N(0, \sigma^2)

Regression Assumptions (R)

  • We will use the reg_check() function from library(ssstats).

  • Generic syntax:

model_name |> reg_check() 

Example 1

  • Recall the training dataset,

    • Is there a relationship between weekly practice hours (x) and exam score (y)?
practice_hours exam_score
6.7 68.7
1.4 56.6
1.4 63.0

Example 1

  • Is there a relationship between weekly practice hours (x) and exam score (y)?
m1 <- glm(exam_score ~ practice_hours,
          data = training)
m1 |> coefficients()
   (Intercept) practice_hours 
     57.253240       3.108387 

Example 1

  • Checking assumptions,
m1 |> reg_check()

Example 2

  • Recall the reports dataset,

    • Is there a relationship between officer height (x) and report accuracy (y)?
height_cm report_accuracy
198 75.5
129 82.7
89 78.0

Example 2

  • Is there a relationship between officer height (x) and report accuracy (y)?

  • Constructing the model,

m2 <- glm(report_accuracy ~ height_cm, 
          data = reports)
m2 |> coefficients()
 (Intercept)    height_cm 
80.430106777  0.009226122 

Example 2

  • Checking assumptions,
m2 |> reg_check()

Statistical Inference in Linear Regression

  • Once we confirm that our residuals follow a normal distribution, we begin to make inference.

    • Omnibus test for the model.

    • Individual predictor tests.

      • Confidence intervals for \beta_i.
  • If the residuals do not follow a normal distribution with mean 0 and some common variance, \sigma^2, we do not follow our modeling up with formal hypothesis tests.

Confidence Intervals for \beta_i

  • Like all statistics, we can put a confidence interval on \beta_i.

    • When reporting regression results, I always include the corresponding CI.

\hat{\beta}_i \pm t_{\alpha/2, n-k-1} \text{ SE}_{\hat{\beta}_i}

Confidence Intervals for \beta_i (R)

  • We can use the tidy() function to get regression results with confidence intervals,
model_name |> tidy(conf.int = TRUE) 

Example 1

  • Returning to our first example,

    • Is there a relationship between weekly practice hours (x) and exam score (y)?
m1 |> tidy(conf.int = TRUE) 
term estimate conf.low conf.high
(Intercept) 57.253240 52.988825 61.51765
practice_hours 3.108388 2.418945 3.79783

Example 3

  • Recall the bakery_sales dataset,

    • How are advertising spending (x_1), foot traffic (x_2), temperature (x_3), and discounts (x_4) related to daily bakery sales (y)?
advertising_dollars foot_traffic temperature_c discount_percent sales_dollars
12.72 136 21.7 6.5 553.84
70.45 145 28.1 13.9 838.67
82.13 144 18.8 13.7 842.29

Example 3

  • Recall the bakery_sales dataset,

    • How are advertising spending (x_1), foot traffic (x_2), temperature (x_3), and discounts (x_4) related to daily bakery sales (y)?
m3 |> tidy(conf.int = TRUE) 
term estimate conf.low conf.high
(Intercept) 142.694320 27.014973 258.373666
advertising_dollars 1.734312 1.159990 2.308634
foot_traffic 1.383370 1.206676 1.560065
temperature_c 8.607033 4.743606 12.470459
discount_percent 11.922056 8.979698 14.864413

Hypothesis Testing: Significant Regression Line

  • We can use the likelihood ratio test to test for a “significant regression” line.

    • A significant regression line means that there is a non-zero slope among all slopes.
  • Hypotheses

    • H_0: \ \beta_1 = ... = \beta_k = 0
    • H_1: at least one is different
  • Test Statistic and p-Value

    • \chi^2_0 (from R)
    • p = \text{P}[\chi^2_{k} \ge \chi^2_0]

Hypothesis Testing: Significant Regression Line (R)

  • We will use the anova() function to test for a significant model.
full <- glm(y ~ x_1 + x_2 + ... + x_k, data = dataset_name)
reduced <- glm(y ~ 1, data = dataset_name)
anova(reduced, full, test = "Chisq")

Example 1

  • Revisiting our model from Example 1,

\hat{\text{exam}} = 57.25 + 3.11 \text{ practice}

full <- glm(exam_score ~ practice_hours, data = training)
reduced <- glm(exam_score ~ 1, data = training)
anova(reduced, full, test = "Chisq")
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
39 4123.858 NA NA NA
38 1349.925 1 2773.933 0

Example 1

  • Hypotheses:

    • H_0: \beta_1 = 0
    • H_1: \beta_1 \ne 0
  • Test Statistic and p-Value:

    • \chi^2_0 = 2773.9
    • p < 0.001
  • Rejection Region:

    • Reject H_0 if p < \alpha; \alpha = 0.05
  • Conclusion and Interpretation:

    • Reject H_0. There is sufficient evidence to suggest that the slope is non-zero.

Example 3

  • Revisiting our model from Example 3,

\hat{\text{exam}} = 57.25 + 3.11 \text{ practice}

full <- glm(sales_dollars ~ advertising_dollars + foot_traffic + temperature_c + discount_percent, data = bakery_sales)
reduced <- glm(sales_dollars ~ 1, data = bakery_sales)
anova(reduced, full, test = "Chisq")
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
39 796611.34 NA NA NA
35 84293.17 4 712318.2 0

Example 3

  • Hypotheses:

    • H_0: \beta_1 = \beta_2 = \beta_3 = \beta_4 = 0
    • H_1: at least one \beta_i \ne 0
  • Test Statistic and p-Value:

    • \chi^2_0 = 712318
    • p < 0.001
  • Rejection Region:

    • Reject H_0 if p < \alpha; \alpha = 0.05
  • Conclusion and Interpretation:

    • Reject H_0. There is sufficient evidence to suggest that at least one of the slopes is non-zero.

Hypothesis Testing: Individual Predictors

  • We can use the t-test to test for “significant predictors”

    • A significant predictor means that there is a non-zero slope between the outcome and this specific x_i.
  • Hypotheses

    • H_0: \ \beta_i = 0
    • H_1: \ \beta_i \ne 0
  • Test Statistic

t_0 = \frac{\hat{\beta}_i}{\text{SE}_{\beta_i}} \sim t_1

Hypothesis Testing: Individual Predictors (R)

  • We again turn to tidy() for output,
model_name |> tidy(conf.int = TRUE)

Example 1

  • Returning to our first example,
m1 |> tidy(conf.int = TRUE)
term estimate std.error statistic p.value conf.low conf.high
(Intercept) 57.253240 2.175762 26.314113 0 52.988825 61.51765
practice_hours 3.108388 0.351763 8.836595 0 2.418945 3.79783
  • Practice time is a significant predictor of exam score (p < 0.001).

Example 3

  • Returning to our third example,
m3 |> tidy(conf.int = TRUE)
term estimate std.error statistic p.value conf.low conf.high
(Intercept) 142.694320 59.0211593 2.417681 0.0209643 27.014973 258.373666
advertising_dollars 1.734312 0.2930268 5.918610 0.0000010 1.159990 2.308634
foot_traffic 1.383370 0.0901519 15.344879 0.0000000 1.206676 1.560065
temperature_c 8.607033 1.9711721 4.366454 0.0001068 4.743606 12.470459
discount_percent 11.922056 1.5012304 7.941523 0.0000000 8.979698 14.864413
  • All predictors are significant (all p < 0.001).

Example 4

  • A cafe owner in Savanna Central brings you a spreadsheet: “We’ve been tracking everything! Can you help us figure out which of these are related to sales?”

  • We have collected data from 40 days to answer the following question:

    • Which of the five explanatory variables are associated with daily cafe sales after accounting for the other variables?
advertising_dollars foot_traffic temperature_c music_volume_db window_display_hours sales_dollars
16.46 239 25.3 66.7 3.5 639.90
16.97 398 17.5 52.7 2.0 1018.52
28.16 123 18.6 53.9 10.1 438.26

Example 4

  • Constructing the model,

    • Which of the five explanatory variables are associated with daily cafe sales after accounting for the other variables?
m4 <- glm(sales_dollars ~ window_display_hours + music_volume_db + temperature_c + foot_traffic + advertising_dollars,
          data = cafe_sales)
m4 |> tidy(conf.int = TRUE)
term estimate std.error statistic p.value conf.low conf.high
(Intercept) 214.8508016 92.5198490 2.3222131 0.0263440 33.515230 396.186373
window_display_hours 0.6401574 2.9259721 0.2187845 0.8281259 -5.094642 6.374957
music_volume_db 0.1185153 1.2434609 0.0953108 0.9246275 -2.318623 2.555654
temperature_c -3.9037541 2.5040629 -1.5589681 0.1282647 -8.811627 1.004119
foot_traffic 1.8625319 0.1215631 15.3215187 0.0000000 1.624273 2.100791
advertising_dollars 2.9437998 0.3958345 7.4369464 0.0000000 2.167978 3.719621

Example 4

  • Is this a significant regression model?
full <- glm(sales_dollars ~ window_display_hours + music_volume_db + temperature_c + foot_traffic + advertising_dollars, data = cafe_sales)
reduced <- glm(sales_dollars ~ 1, data = cafe_sales)
anova(reduced, full, test = "Chisq")
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
39 1378745.9 NA NA NA
34 133178.7 5 1245567 0

Example 4

  • Hypotheses:

    • H_0: \beta_1 = \beta_2 = \beta_3 = \beta_4 = \beta_5 =0
    • H_1: at least one \beta_i \ne 0
  • Test Statistic and p-Value:

    • \chi^2_0 = 1245567
    • p < 0.001
  • Rejection Region:

    • Reject H_0 if p < \alpha; \alpha = 0.05
  • Conclusion and Interpretation:

    • Reject H_0. There is sufficient evidence to suggest that at least one of the slopes is non-zero.

Example 4

  • Looking at individual predictors,
m4 <- glm(sales_dollars ~ window_display_hours + music_volume_db + temperature_c + foot_traffic + advertising_dollars,
          data = cafe_sales)
m4 |> tidy(conf.int = TRUE)
term estimate std.error statistic p.value conf.low conf.high
(Intercept) 214.8508016 92.5198490 2.3222131 0.0263440 33.515230 396.186373
window_display_hours 0.6401574 2.9259721 0.2187845 0.8281259 -5.094642 6.374957
music_volume_db 0.1185153 1.2434609 0.0953108 0.9246275 -2.318623 2.555654
temperature_c -3.9037541 2.5040629 -1.5589681 0.1282647 -8.811627 1.004119
foot_traffic 1.8625319 0.1215631 15.3215187 0.0000000 1.624273 2.100791
advertising_dollars 2.9437998 0.3958345 7.4369464 0.0000000 2.167978 3.719621

Example 4

  • Looking at the individual predictors,

    • Window display is not a significant predictor (p = 0.828)

    • Music volume is not a significant predictor (p = 0.925)

    • Temperature is not a significant predictor (p = 0.128)

    • Foot traffic is a significant predictor (p < 0.001)

    • Advertising dollars is a significant predictor (p < 0.001)

Wrap Up

  • We have covered the basics of linear regression.

  • In Statistics for Data Science II, you will learn more about statistical modeling.

    • A deeper dive into ordinary least squares.

      • Additional predictor types.

      • Model fit and diagnostics.

      • Data visualization.

    • Logistic regression.

    • Poisson and negative binomial regressions.