Learning objectives

By the end of the lecture, you will be able to …

  • XXX
  • YYY

Packages

Load the standard packages and new package modelbased.

library(tidyverse)
library(haven) # not core tidyverse
library(gssr)
library(gssrdoc)
library(summarytools)
library(gtsummary)
library(modelbased)
library(modelsummary)

Load your data

# Load all gss
gss22 <- gss_get_yr(2022)

Variables

  • realrinc R’s income in constant $
  • lifenow R’s rating of life overall now from 0-10
  • childs Number of children
  • mntlhlth Days of poor mental health past 30 days
  • age Age of respondent
  • educ Respondents highest edu credit
  • hrs1 How many hours did you work last week
  • sex Respondents sex
  • marital R’s marital status

Make a df with only the (pretty) categorical and continuous variables we’ll analyze.

# Categorical Variables
my_cat <- gss22 |>
  select(id, sex, marital) |>
  zap_missing() |>
  mutate(
    sex = as_factor(sex),
    marital = as_factor(marital)) |>
  droplevels()

# Continuous Variables
my_con <- gss22 |>
  select(id, age, educ, hrs1, realrinc, lifenow, childs, mntlhlth) |>
  mutate(across(c(age, educ, hrs1, realrinc, lifenow, childs, mntlhlth),
                as.numeric))

# Combine the two dataframes
my_data <- full_join(my_cat, my_con, by = c("id")) |>
  drop_na() |>
  mutate(
    marital = recode(
      marital,
      "married"       = "Married",
      "never married" = "Never married",
      "widowed"       = "Div/Wid/Sep",
      "divorced"      = "Div/Wid/Sep",
      "separated"     = "Div/Wid/Sep"),
    marital = factor(
      marital,
      levels = c("Never married", "Div/Wid/Sep", "Married")),
    income10k = realrinc / 10000
  )

# show pretty labels in regression table
coef_labels <- c(
  age = "Age",
  sexfemale = "Female",
  "maritalDiv/Wid/Sep" = "Div/Wid/Sep (ref. = never married)",
  maritalMarried = "Married",
  childs = "Children",
  educ = "Education (years)",
  hrs1 = "Work hours",
  income10k = "Income ($10k)",
  mntlhlth = "Poor mental health days",
  lifenow = "Life satisfaction",
  "educ:income10k" = "Education × Income")

Building Models

Most analysts will run several models predicting a single outcome (Model 1, Model 2, etc.)


Why?


  • Elaborate on an apparent relationship between two variables
  • Explore alternative (or competing) explanations
  • Test whether apparent associations “hold up” under more stringent conditions


Heads Up!

Must use a consistent sample size in order to compare across models.

Several relationships may be present when a third variable (Z) is included in the analysis of two-variables.


Confounders The relationship between X and Y is spurious—both predicted by Z

Covariates Other variables in the model that predict Y, but mostly unrelated to the relationship between X and Y

Moderators The relationship between X and Y depends on Z (interactions) Does a third variable change the relationship between X and Y?

Mediators Explains the relationship between X and Y (X🡪Z🡪Y) Does a third variable help explain the relationship between X and Y?

Mediators

Conditions for mediation

IV🡪DV relationship is significant > Regression 1: regress DV IV

IV🡪MV relationship is significant > Regression 2: regress MV IV

MV🡪DV relationship is significant when IV is controlled > Regression 3: regress DV IV MV


Heads Up!

The IV🡪DV coefficient must be closer to zero when MV is controlled than when it is not (Regression 1 & 3).

Does income mediate the education–life satisfaction association?
Total Effect Path A Direct + Indirect
* p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) 6.425*** -2.246*** 6.629***
(0.217) (0.409) (0.215)
educ 0.097*** 0.357*** 0.064***
(0.015) (0.027) (0.015)
income10k 0.091***
(0.013)
Num.Obs. 1628 1628 1628
R2 0.026 0.094 0.055

Mediation in a Paper

Does income help explain the association between education and life satisfaction, net of controls?


models_med <- list(
  "Model 1: Education"  = lm(lifenow ~ educ, data = my_data),
  "Model 2: + Controls" = lm(lifenow ~ educ + age + sex + marital + childs,
                             data = my_data),
  "Model 3: + Income"   = lm(lifenow ~ educ + age + sex + marital + childs
                             + income10k, data = my_data)
)

modelsummary(
  models_med,
  statistic = "std.error",
  stars = c("*" = .05, "**" = .01, "***" = .001),
  coef_rename = coef_labels,
  gof_map = c("nobs", "adj.r.squared")
  )

OLS Regression of Life Satisfaction: Income as a Mediator

Model 1: Education

Model 2: + Controls

Model 3: + Income

(Intercept)

6.425***

5.790***

5.971***

(0.217)

(0.239)

(0.241)

Education (years)

0.097***

0.070***

0.050***

(0.015)

(0.014)

(0.015)

Age

0.017***

0.016***

(0.003)

(0.003)

Female

0.022

0.080

(0.078)

(0.079)

Div/Wid/Sep (ref. = never married)

0.017

-0.012

(0.122)

(0.121)

Married

0.765***

0.686***

(0.099)

(0.100)

Children

-0.041

-0.038

(0.028)

(0.028)

Income ($10k)

0.060***

(0.013)

Num.Obs.

1628

1628

1628

R2 Adj.

0.026

0.099

0.109

* p < 0.05, ** p < 0.01, *** p < 0.001

Moderation in a Paper

Does the association between education and life satisfaction depend on income?

models_mod <- list(
  "Model 1: Education"     = lm(lifenow ~ educ, data = my_data),
  "Model 2: + Income"      = lm(lifenow ~ educ + income10k + age + sex + marital + childs,
                                data = my_data),
  "Model 3: + Interaction" = lm(lifenow ~ educ * income10k + age + sex + marital + childs,
                                data = my_data)
)

modelsummary(
  models_mod,
  statistic = "std.error",
  stars = c("*" = .05, "**" = .01, "***" = .001),
  coef_rename = coef_labels,
  gof_map = c("nobs", "adj.r.squared")
)

OLS Regression of Life Satisfaction: Education × Income Interaction

Model 1: Education

Model 2: + Income

Model 3: + Interaction

(Intercept)

6.425***

5.971***

6.217***

(0.217)

(0.241)

(0.301)

Education (years)

0.097***

0.050***

0.034

(0.015)

(0.015)

(0.019)

Income ($10k)

0.060***

-0.041

(0.013)

(0.075)

Age

0.016***

0.016***

(0.003)

(0.003)

Female

0.080

0.079

(0.079)

(0.079)

Div/Wid/Sep (ref. = never married)

-0.012

-0.010

(0.121)

(0.121)

Married

0.686***

0.695***

(0.100)

(0.100)

Children

-0.038

-0.040

(0.028)

(0.028)

Education × Income

0.006

(0.005)

Num.Obs.

1628

1628

1628

R2 Adj.

0.026

0.109

0.110

* p < 0.05, ** p < 0.01, *** p < 0.001

Moderation vs Mediation

Mediation Moderation
Question Does income help explain the relationship between education and life satisfaction? Does the relationship between education and life satisfaction depend on income?
Concept Income explains why education is associated with life satisfaction. Income changes how strongly education is associated with life satisfaction.
Model lifenow ~ educ + controls
income10k ~ educ + controls
lifenow ~ educ + income10k + controls
lifenow ~ educ * income10k + controls
Look at Does the educ coefficient shrink when income10k is added? Is the educ:income10k coefficient significant?

Comparing between models

Theory-Driven Blocks

What predicts life satisfaction?

models_blocks <- list(
  "Model 1: Demographic"   = lm(lifenow ~ age + sex + educ, data = my_data),
  "Model 2: + Family"      = lm(lifenow ~ age + sex + educ + marital + childs,
                                data = my_data),
  "Model 3: + Economic"    = lm(lifenow ~ age + sex + educ + marital + childs
                                + hrs1 + income10k, data = my_data),
  "Model 4: + Mental health" = lm(lifenow ~ age + sex + educ + marital + childs
                                + hrs1 + income10k + mntlhlth,
                                data = my_data)
)

modelsummary(
  models_blocks,
  title = "Table 4. OLS Regression of Life Satisfaction: Nested Blocks of Predictors",
  statistic = "std.error",
  stars = c("*" = .05, "**" = .01, "***" = .001),
  coef_rename = coef_labels,
  gof_map = c("nobs", "adj.r.squared")
  )

OLS Regression of Life Satisfaction: Nested Blocks of Predictors

Model 1: Demographic

Model 2: + Family

Model 3: + Economic

Model 4: + Mental health

(Intercept)

5.687***

5.790***

6.177***

6.780***

(0.242)

(0.239)

(0.269)

(0.256)

Age

0.020***

0.017***

0.015***

0.008**

(0.003)

(0.003)

(0.003)

(0.003)

Female

-0.080

0.022

0.063

0.190*

(0.079)

(0.078)

(0.080)

(0.075)

Education (years)

0.091***

0.070***

0.051***

0.048***

(0.014)

(0.014)

(0.015)

(0.014)

Div/Wid/Sep (ref. = never married)

0.017

0.006

0.009

(0.122)

(0.122)

(0.114)

Married

0.765***

0.689***

0.628***

(0.099)

(0.100)

(0.094)

Children

-0.041

-0.038

-0.039

(0.028)

(0.028)

(0.027)

Work hours

-0.005

-0.004

(0.003)

(0.003)

Income ($10k)

0.064***

0.054***

(0.013)

(0.013)

Poor mental health days

-0.068***

(0.005)

Num.Obs.

1628

1628

1628

1628

R2 Adj.

0.053

0.099

0.110

0.212

* p < 0.05, ** p < 0.01, *** p < 0.001

Standardized regression coefficients

Survey weights

Logistic Regression

Average Marginal Effects

Average marginal effect (AME) | Average partial effect; average difference between two AAPs |


AME | estimate_contrasts(..., estimate = "population") | avg_comparisons(variables = "sex") | margins, dydx(sex) |