Setting up environment
setwd("~/SOC252_Assignment")
# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "sandwich")
# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[6]]
## [1] "sandwich" "effects" "carData" "modelsummary" "fst"
## [6] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
#Loading Data
ess <- read_fst("All-ESS-Data.fst")
Subsetting for Germany + Cleaning Missing data & Recoding
Germany_data <- ess %>%
filter(cntry == "DE")%>%
dplyr::select(
stflife, eisced, # Focal Variables
health, rlgdgr, hinctnta
) %>%
mutate(
life_satisfaction = ifelse(stflife %in% c(77, 88, 99), NA, stflife),
eisced = ifelse(eisced %in% c(0, 55, 77, 88, 99), NA, eisced),
edu_level = case_when(
eisced %in% 1:5 ~ "No BA",
eisced %in% 6:7 ~ "BA",
),
bad_health = ifelse(health %in% c(7, 8, 9), NA, health),
religiosity = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr),
income = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
)
Germany_data <- na.omit(Germany_data)
Checking for Unique Values
sapply(lapply(Germany_data, unique), length)
## stflife eisced health rlgdgr
## 11 7 5 11
## hinctnta life_satisfaction edu_level bad_health
## 10 11 2 5
## religiosity income
## 11 10
4-Step Hypothesis Testing using the infer Package
#Step 1 - Calculating sample test statistic
test_stat <- Germany_data |>
specify(explanatory = edu_level,
response = life_satisfaction) |>
hypothesize(null = "independence") |>
calculate(
stat = "t",
order = c("No BA", "BA")
)
test_stat$stat
## t
## -15.92345
# Step 2 - Simulating the null distribution
null_distribution <- Germany_data %>%
specify(explanatory = edu_level,
response = life_satisfaction) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "t",
order = c("No BA", "BA")
)
# Step 3 - Calculating the p-value of your sample
p_value <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_value
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
# Step 4 - Visualizing sample distribution with respect to null distribution
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")

null_distribution
## Response: life_satisfaction (numeric)
## Explanatory: edu_level (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.363
## 2 2 0.130
## 3 3 -0.163
## 4 4 0.574
## 5 5 0.694
## 6 6 0.592
## 7 7 1.22
## 8 8 0.434
## 9 9 0.394
## 10 10 1.06
## # ℹ 990 more rows
Comparing Models
model1 <- lm(life_satisfaction ~ edu_level, data = Germany_data)
model2 <- lm(life_satisfaction ~ edu_level + religiosity, data = Germany_data)
model3 <- lm(life_satisfaction ~ edu_level + religiosity + bad_health, data = Germany_data)
model4 <- lm(life_satisfaction ~ edu_level + religiosity + bad_health + income, data = Germany_data)
#modelsummary::modelsummary(list(model1, model2, model3, model4))
modelsummary(
list(model1, model2, model3, model4),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
|
|
(1)
|
(2)
|
(3)
|
(4)
|
|
edu_levelNo BA
|
−0.5 (0.0)***
|
−0.5 (0.0)***
|
−0.3 (0.0)***
|
0.0 (0.0)
|
|
religiosity
|
|
0.1 (0.0)***
|
0.1 (0.0)***
|
0.1 (0.0)***
|
|
bad_health
|
|
|
−0.7 (0.0)***
|
−0.6 (0.0)***
|
|
income
|
|
|
|
0.2 (0.0)***
|
|
Num.Obs.
|
21035
|
21035
|
21035
|
21035
|
|
R2
|
0.010
|
0.028
|
0.108
|
0.148
|
|
R2 Adj.
|
0.010
|
0.028
|
0.108
|
0.148
|
|
AIC
|
91899.8
|
91513.4
|
89706.2
|
88748.4
|
|
BIC
|
91923.6
|
91545.2
|
89746.0
|
88796.1
|
|
Log.Lik.
|
−45946.880
|
−45752.701
|
−44848.109
|
−44368.211
|
|
RMSE
|
2.15
|
2.13
|
2.04
|
1.99
|
Checking is assumptions of the Frequentist Approach are met
# set graphical parameters
par(mfrow = c(2,2)) # plot 2 X 2 matrix, by row
# plot diagnostics plots
plot(model4, pch = 19, col = scales::alpha("grey", .8), cex = .5)

models <- list(
"Model 1" = lm(life_satisfaction ~ edu_level, data = Germany_data),
"Model 2" = lm(life_satisfaction ~ edu_level + religiosity, data = Germany_data),
"Model 3" = lm(life_satisfaction ~ edu_level + religiosity + health, data = Germany_data),
"Model 4" = lm(life_satisfaction ~ edu_level + religiosity + health + income, data = Germany_data)
)
modelsummary(models)
|
|
Model 1
|
Model 2
|
Model 3
|
Model 4
|
|
(Intercept)
|
7.620
|
7.257
|
8.796
|
7.436
|
|
|
(0.029)
|
(0.034)
|
(0.048)
|
(0.064)
|
|
edu_levelNo BA
|
−0.489
|
−0.498
|
−0.326
|
−0.044
|
|
|
(0.034)
|
(0.034)
|
(0.032)
|
(0.033)
|
|
religiosity
|
|
0.096
|
0.093
|
0.089
|
|
|
|
(0.005)
|
(0.005)
|
(0.005)
|
|
health
|
|
|
−0.699
|
−0.607
|
|
|
|
|
(0.016)
|
(0.016)
|
|
income
|
|
|
|
0.165
|
|
|
|
|
|
(0.005)
|
|
Num.Obs.
|
21035
|
21035
|
21035
|
21035
|
|
R2
|
0.010
|
0.028
|
0.108
|
0.148
|
|
R2 Adj.
|
0.010
|
0.028
|
0.108
|
0.148
|
|
AIC
|
91899.8
|
91513.4
|
89706.2
|
88748.4
|
|
BIC
|
91923.6
|
91545.2
|
89746.0
|
88796.1
|
|
Log.Lik.
|
−45946.880
|
−45752.701
|
−44848.109
|
−44368.211
|
|
RMSE
|
2.15
|
2.13
|
2.04
|
1.99
|