By the end of the lecture, you will be able to …
Download and open code-along-07.qmd
Load the standard packages and new package modelbased.
year Survey yeartvhours Hours per day watching tvrealrinc R’s income in constant $age Age of respondenteduc Respondents highest edu credithrs1 How many hours did you work last weekchilds How many children have you ever had?polviews Think of self as liberal or conservativesex Respondents sexmarital R’s marital statusMake a df with only the (pretty) categorical and continuous variables we’ll analyze.
# Categorical Variables
my_cat <- gss_all |>
select(id, year, polviews, sex, marital) |>
zap_missing() |>
mutate(
polviews = as_factor(polviews),
sex = as_factor(sex),
marital = as_factor(marital)) |>
droplevels()
# Continuous Variables
my_con <- gss_all |>
select(id, year, tvhours, age, educ, hrs1, realrinc, childs) |>
mutate(
age = as.numeric(age),
educ = as.numeric(educ),
hrs1 = as.numeric(hrs1),
realrinc = as.numeric(realrinc),
childs = as.numeric(childs))
# Combine the two dataframes
my_data <- full_join(my_cat, my_con, by = c("year", "id")) # match by 2 varsHave the average number of children changed over time?
# A tibble: 2 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 17.2 0.811 21.2 0 15.7 18.8
2 year -0.008 0 -18.9 0 -0.008 -0.007
The better a least-squares line is, the better it can predict an outcome with the least amount of error
\(r^2\) is a proportional reduction of error (PRE) measure
\(PRE = \frac{E1 - E2}{E1}\)
It tells us how well the regression model fits the data
\(R^2 = \frac{\sum E1^2 - \sum E2^2}{\sum E1^2}\)
E1
Errors of prediction made when IV is ignored
Equivalent to the sum of errors we would have if we guessed that every Y was equal to the mean of Y
aka Sum of Squares Total (SST)
E2
Errors of prediction made based on IV
Equivalent to the sum of errors we would have if we used our least squares line to predict Y
aka Sum of Squared Errors (SSE)
Use get_regression_points() to see E2.
# A tibble: 5 × 5
ID year childs childs_hat residual
<int> <dbl> <dbl> <dbl> <dbl>
1 41065 2002 1 1.88 -0.885
2 64339 2018 0 1.76 -1.76
3 32162 1994 2 1.95 0.053
4 47250 2006 3 1.86 1.14
5 51810 2008 0 1.84 -1.84
# A tibble: 5 × 7
ID year childs mean_pred childs_hat E1 E2
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2065 1973 2 1.91 2.11 0.0906 -0.108
2 47076 2006 1 1.91 1.86 -0.909 -0.855
3 12952 1982 2 1.91 2.04 0.0906 -0.039
4 32720 1996 0 1.91 1.93 -1.91 -1.93
5 60392 2016 1 1.91 1.78 -0.909 -0.778
\(R^2\) ranges from 0 to 1.0
1.0 = Our independent variable accounts for all the variation that we find in the dependent variable (it predicts the dependent variable perfectly).
0.0 = Our independent variable does not improve our ability to predict the dependent variable.
In the social sciences, a high \(R^2\) is rare!
Use get_regression_summaries() to see \(R^2\).
# A tibble: 1 × 9
r_squared adj_r_squared mse rmse sigma statistic p_value df nobs
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.005 0.005 3.07 1.75 1.75 357. 0 1 75407
\(R^2\) shows the amount of variance of the DV explained by the IV(s).
An \(R^2\) of \(0.005\) means that knowing the survey year reduces our error in predicting average number of children by .5% compared with not using survey year at all.
The survey year explains .5% of variation in average number of children.
Are the trends explained by increases in education?
# A tibble: 2 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 17.2 0.811 21.2 0 15.7 18.8
2 year -0.008 0 -18.9 0 -0.008 -0.007
# A tibble: 3 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 5.26 0.813 6.47 0 3.66 6.85
2 year -0.001 0 -2.00 0.045 -0.002 0
3 educ -0.131 0.002 -64.5 0 -0.134 -0.127
# A tibble: 1 × 9
r_squared adj_r_squared mse rmse sigma statistic p_value df nobs
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.005 0.005 3.07 1.75 1.75 357. 0 1 75407
tbl_regression()OLS Regression predicting number of children by survey year and years of education
| Characteristic | Beta | 95% CI | p-value |
|---|---|---|---|
| (Intercept) | 5.3 | 3.7, 6.9 | <0.001 |
| GSS year for this respondent | 0.00 | 0.00, 0.00 | 0.045 |
| educ | -0.13 | -0.13, -0.13 | <0.001 |
| Abbreviation: CI = Confidence Interval | |||
tbl_regression()OLS Regression predicting number of children by survey year and years of education
| Characteristic | Beta | SE | p-value |
|---|---|---|---|
| (Intercept) | 5.3 | 0.813 | <0.001 |
| GSS year for this respondent | 0.00 | 0.000 | 0.045 |
| educ | -0.13 | 0.002 | <0.001 |
| Abbreviations: CI = Confidence Interval, SE = Standard Error | |||
tbl_regression()OLS Regression predicting number of children by survey year and years of education
| Characteristic | Beta1 | SE |
|---|---|---|
| (Intercept) | 5.3*** | 0.813 |
| GSS year for this respondent | 0.00* | 0.000 |
| educ | -0.13*** | 0.002 |
| R² | 0.057 | |
| No. Obs. | 75,166 | |
| 1 p<0.05; p<0.01; p<0.001 | ||
Are weekly hours worked related to education and gender?
# A tibble: 3 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 38.6 0.32 121. 0 38.0 39.2
2 educ 0.42 0.022 18.7 0 0.376 0.464
3 sex-female -6.58 0.132 -49.9 0 -6.83 -6.32
tbl_regression()OLS Regression predicting weekly work hours by years of education and gender
| Characteristic | Beta1 | SE |
|---|---|---|
| (Intercept) | 39*** | 0.320 |
| educ | 0.42*** | 0.022 |
| sex | ||
| male | — | — |
| female | -6.6*** | 0.132 |
| R² | 0.061 | |
| No. Obs. | 43,167 | |
| 1 p<0.05; p<0.01; p<0.001 | ||
tbl_regression()OLS Regression predicting weekly work hours by years of education and gender
| Characteristic | Beta1 | SE |
|---|---|---|
| (Intercept) | 39*** | 0.320 |
| educ | 0.42*** | 0.022 |
| Women | -6.6*** | 0.132 |
| R² | 0.061 | |
| No. Obs. | 43,167 | |
| 1 p<0.05; p<0.01; p<0.001 | ||
Are weekly hours worked related to education and marital status?
# A tibble: 6 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 36.5 0.33 110. 0 35.9 37.1
2 educ 0.378 0.023 16.4 0 0.333 0.424
3 marital-widowed -5.94 0.385 -15.4 0 -6.70 -5.19
4 marital-divorced 0.605 0.198 3.06 0.002 0.218 0.993
5 marital-separated -0.278 0.379 -0.733 0.464 -1.02 0.465
6 marital-never married -1.76 0.163 -10.8 0 -2.08 -1.44
Regression Predicting Weekly Work Hours by Years of Education and Marital Status
model |>
tbl_regression(
intercept = TRUE,
label = list(
marital = "Marital status (ref. = married)")) |>
remove_row_type(type = c("reference")) |>
modify_column_hide(conf.low) |>
modify_column_unhide(
column = std.error) |>
remove_abbreviation() |>
add_significance_stars() |>
add_glance_table(
include = c("r.squared", "nobs"))| Characteristic | Beta1 | SE |
|---|---|---|
| (Intercept) | 37*** | 0.330 |
| educ | 0.38*** | 0.023 |
| Marital status (ref. = married) | ||
| widowed | -5.9*** | 0.385 |
| divorced | 0.61** | 0.198 |
| separated | -0.28 | 0.379 |
| never married | -1.8*** | 0.163 |
| R² | 0.015 | |
| No. Obs. | 43,178 | |
| 1 p<0.05; p<0.01; p<0.001 | ||
Are daily TV hours related to education and political identity?
my_data <- my_data |>
mutate(pol_group = case_when(
polviews %in% c("extremely liberal", "liberal", "slightly liberal") ~ "Liberal",
polviews == "moderate, middle of the road" ~ "Moderate",
polviews %in% c("slightly conservative", "conservative", "extremely conservative") ~ "Conservative",
TRUE ~ NA_character_))
model <- lm(tvhours ~ educ + pol_group, data = my_data)
moderndive::get_regression_table(model)# A tibble: 4 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 5.00 0.056 89.1 0 4.89 5.12
2 educ -0.159 0.004 -40.4 0 -0.166 -0.151
3 pol_group-Liberal 0.157 0.031 5.15 0 0.097 0.217
4 pol_group-Moderate 0.192 0.028 6.76 0 0.136 0.247
# Change to factor and change order
my_data$pol_group <- factor(
my_data$pol_group,
levels = c("Moderate", "Liberal", "Conservative"))
# OR ....
# change to factor and explicitly state reference category
my_data$pol_group <- relevel(factor(my_data$pol_group),
ref = "Moderate")
table(my_data$pol_group)
Moderate Liberal Conservative
25140 18533 22205
# A tibble: 4 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept 5.20 0.054 96.4 0 5.09 5.30
2 educ -0.159 0.004 -40.4 0 -0.166 -0.151
3 pol_group-Liberal -0.035 0.03 -1.15 0.25 -0.093 0.024
4 pol_group-Conservative -0.192 0.028 -6.76 0 -0.247 -0.136
| term | estimate | std_error | statistic | p_value | lower_ci | upper_ci |
|---|---|---|---|---|---|---|
| intercept | 5.162 | 0.059 | 88.013 | 0.00 | 5.047 | 5.277 |
| educ | -0.159 | 0.004 | -40.421 | 0.00 | -0.166 | -0.151 |
| relevel(pol_group, ref = “Liberal”)-Moderate | 0.035 | 0.030 | 1.150 | 0.25 | -0.024 | 0.093 |
| relevel(pol_group, ref = “Liberal”)-Conservative | -0.157 | 0.031 | -5.148 | 0.00 | -0.217 | -0.097 |
1. Run and interpret a regression predicting realrinc with hrs1 and educ
# A tibble: 3 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept -33065. 802. -41.2 0 -34637. -31494.
2 hrs1 481. 10.5 45.6 0 460. 501.
3 educ 2746. 50.4 54.5 0 2647. 2845.
2. Add childs to the regression. Interpret the output.
# A tibble: 4 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept -39248. 851. -46.1 0 -40917. -37580.
2 hrs1 481. 10.5 45.8 0 460. 501.
3 educ 2959. 51.2 57.8 0 2858. 3059.
4 childs 1960. 93.9 20.9 0 1776. 2144.
3. Add marital to the regression. Interpret the output.
# A tibble: 8 × 7
term estimate std_error statistic p_value lower_ci upper_ci
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 intercept -33122. 886. -37.4 0 -34859. -31385.
2 hrs1 468. 10.5 44.7 0 448. 489.
3 educ 2888. 51.0 56.6 0 2788. 2988.
4 childs 1021. 105. 9.76 0 816. 1226.
5 marital-widowed -4122. 826. -4.99 0 -5740. -2503.
6 marital-divorced -4029. 420. -9.60 0 -4851. -3206.
7 marital-separated -5769. 816. -7.07 0 -7368. -4171.
8 marital-never married -8792. 394. -22.3 0 -9564. -8020.