Setting Up
setwd("/Users/tenzinyingsel/Documents/SOC202")
getwd()
## [1] "/Users/tenzinyingsel/Documents/SOC202"
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "kableExtra", "flextable", "equatiomatic", "forcats", "RColorBrewer", "viridis", "knitr", "rmarkdown", "ggridges")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ 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.
##
## Loading required package: grid
##
## Loading required package: Matrix
##
##
## Attaching package: 'Matrix'
##
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
##
## Loading required package: survival
##
##
## Attaching package: 'survey'
##
##
## The following object is masked from 'package:graphics':
##
## dotchart
##
##
##
## Attaching package: 'MASS'
##
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
##
## Attaching package: 'flextable'
##
##
## The following objects are masked from 'package:kableExtra':
##
## as_image, footnote
##
##
## The following object is masked from 'package:purrr':
##
## compose
##
##
## Loading required package: viridisLite
## [[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] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "modelsummary" "fst" "infer" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[7]]
## [1] "MASS" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "modelsummary" "fst" "infer"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[8]]
## [1] "kableExtra" "MASS" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "modelsummary" "fst"
## [11] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[9]]
## [1] "flextable" "kableExtra" "MASS" "survey" "survival"
## [6] "Matrix" "grid" "effects" "carData" "modelsummary"
## [11] "fst" "infer" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[11]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[12]]
## [1] "RColorBrewer" "equatiomatic" "flextable" "kableExtra" "MASS"
## [6] "survey" "survival" "Matrix" "grid" "effects"
## [11] "carData" "modelsummary" "fst" "infer" "lubridate"
## [16] "forcats" "stringr" "dplyr" "purrr" "readr"
## [21] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [26] "graphics" "grDevices" "utils" "datasets" "methods"
## [31] "base"
##
## [[13]]
## [1] "viridis" "viridisLite" "RColorBrewer" "equatiomatic" "flextable"
## [6] "kableExtra" "MASS" "survey" "survival" "Matrix"
## [11] "grid" "effects" "carData" "modelsummary" "fst"
## [16] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [21] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [26] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [31] "datasets" "methods" "base"
##
## [[14]]
## [1] "knitr" "viridis" "viridisLite" "RColorBrewer" "equatiomatic"
## [6] "flextable" "kableExtra" "MASS" "survey" "survival"
## [11] "Matrix" "grid" "effects" "carData" "modelsummary"
## [16] "fst" "infer" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [26] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [31] "utils" "datasets" "methods" "base"
##
## [[15]]
## [1] "rmarkdown" "knitr" "viridis" "viridisLite" "RColorBrewer"
## [6] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [11] "survival" "Matrix" "grid" "effects" "carData"
## [16] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [21] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [26] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [31] "grDevices" "utils" "datasets" "methods" "base"
##
## [[16]]
## [1] "ggridges" "rmarkdown" "knitr" "viridis" "viridisLite"
## [6] "RColorBrewer" "equatiomatic" "flextable" "kableExtra" "MASS"
## [11] "survey" "survival" "Matrix" "grid" "effects"
## [16] "carData" "modelsummary" "fst" "infer" "lubridate"
## [21] "forcats" "stringr" "dplyr" "purrr" "readr"
## [26] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [31] "graphics" "grDevices" "utils" "datasets" "methods"
## [36] "base"
ess <- read_fst("All-ESS-Data.fst")
russianfederation_data <- ess %>%
filter(cntry == "RU")
df <- russianfederation_data
df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
df$year[df$essround == i] <- replacements[i]
}
Recoding Variables of Interest
df <- df %>%
# Education Level
mutate(
edulvla = edulvla %>%
na_if(55) %>%
na_if(88),
edulvlb = edulvlb %>%
na_if(5555) %>%
na_if(7777) %>%
na_if(8888),
educ_lvl = case_when(
essround < 5 & edulvla == 5 ~ "1",
essround >= 5 & edulvlb > 600 ~ "1",
TRUE ~ "0"
),
# Year Born
yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn),
gen = case_when(
yrbrn %in% 1900:1945 ~ "1",
yrbrn %in% 1946:1964 ~ "2",
yrbrn %in% 1965:1979 ~ "3",
yrbrn %in% 1980:1996 ~ "4",
yrbrn %in% 1997:2012 ~ "5",
TRUE ~ as.character(yrbrn)
),
gen = factor(gen, levels = c("1", "2", "3", "4", "5"),
labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials", "Gen Z")),
# Satisfaction with National Government
stfgov = case_when(
stfgov %in% c(77, 88, 99) ~ NA_real_,
TRUE ~ stfgov
)
)
Cleaning Data
df <- df %>% filter(!is.na(educ_lvl))
df <- df %>% filter(!is.na(gen))
df <- df %>% filter(!is.na(stfgov))
Checking Tables
table(df$gen)
##
## Interwar Baby Boomers Gen X Millennials Gen Z
## 2142 3637 2858 3106 119
table(df$educ_lvl)
##
## 0 1
## 7234 4628
table(df$stfgov)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 898 520 998 1562 1383 2452 1216 1281 898 308 346
Descriptive Statistics
d <- df %>%
mutate(
educ_lvl_num = as.numeric(educ_lvl),
gen_num = as.numeric(gen),
stfgov_num = as.numeric(stfgov)
)
table1 <- datasummary_skim(d %>% dplyr::select(educ_lvl_num, gen_num, stfgov_num), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
educ_lvl_num | 2 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 |
gen_num | 5 | 0 | 2.6 | 1.1 | 1.0 | 3.0 | 5.0 |
stfgov_num | 11 | 0 | 4.6 | 2.5 | 0.0 | 5.0 | 10.0 |
d <- d %>%
rename(
`Education Level` = educ_lvl_num,
`Generations` = gen_num,
`Satisfaction With National Government` = stfgov_num
)
table1_v2 <- datasummary_skim(d %>% dplyr::select(`Education Level`,`Generations`, `Satisfaction With National Government`), title = "Table 1. Descriptive Statistics of Main Variables", output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Education Level | 2 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 |
Generations | 5 | 0 | 2.6 | 1.1 | 1.0 | 3.0 | 5.0 |
Satisfaction With National Government | 11 | 0 | 4.6 | 2.5 | 0.0 | 5.0 | 10.0 |
Step 1: ANOVA Calculation
test_stat <- df %>%
specify(explanatory = gen,
response = stfgov) %>%
hypothesize(null = "independence") %>%
calculate(stat = "F")
print(test_stat$stat)
## [1] 9.305161
Step 2: Null Distribution
null_dist <- df %>%
specify(stfgov ~ gen) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "F")
null_dist
## Response: stfgov (numeric)
## Explanatory: gen (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.402
## 2 2 0.814
## 3 3 0.437
## 4 4 0.147
## 5 5 1.04
## 6 6 0.702
## 7 7 0.996
## 8 8 0.0371
## 9 9 0.738
## 10 10 1.46
## # ℹ 990 more rows
Step 3: P Value
p_val <- null_dist %>%
get_pvalue(obs_stat = test_stat, direction = "greater")
## 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_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
Step 4: Visualization
conf_int <- null_dist%>%
get_confidence_interval(level = 0.95, type = "percentile")
null_dist %>%
visualize(data, bins = 10, method = "simulation", dens_color = "black") +
shade_p_value(obs_stat = test_stat, direction = "greater") +
shade_confidence_interval(endpoints = conf_int)
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf
Regression Model
df$weight <- df$dweight * df$pweight
model1 <- lm(stfgov ~ gen, data = df, weights = weight)
model2 <- lm(stfgov ~ gen + educ_lvl, data = df, weights = weight)
model3 <- lm(stfgov ~ gen*educ_lvl, data = df, weights = weight)
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
title = "Table 2. Regression Models Predicting Satisfaction with National Government due to Generation Born and Level of Educational Attainment (Scale of 0 - 10)")
| Â (1) | Â Â (2) | Â Â (3) | |
|---|---|---|---|
| (Intercept) | 4.7 (0.1)*** | 4.7 (0.1)*** | 4.8 (0.1)*** |
| genBaby Boomers | −0.2 (0.1)** | −0.2 (0.1)** | −0.4 (0.1)*** |
| genGen X | −0.1 (0.1)+ | −0.1 (0.1)+ | −0.3 (0.1)*** |
| genMillennials | 0.0 (0.1) | 0.0 (0.1) | −0.1 (0.1) |
| genGen Z | 0.9 (0.2)*** | 0.9 (0.2)*** | 0.8 (0.2)*** |
| educ_lvl1 | 0.0 (0.0) | −0.4 (0.1)** | |
| genBaby Boomers × educ_lvl1 | 0.4 (0.2)** | ||
| genGen X × educ_lvl1 | 0.6 (0.2)*** | ||
| genMillennials × educ_lvl1 | 0.2 (0.2) | ||
| Num.Obs. | 11862 | 11862 | 11862 |
| R2 | 0.004 | 0.004 | 0.005 |
| R2 Adj. | 0.004 | 0.004 | 0.005 |
| AIC | 56948.8 | 56950.8 | 56940.7 |
| BIC | 56993.1 | 57002.4 | 57014.5 |
| Log.Lik. | −28468.389 | −28468.386 | −28460.364 |
| RMSE | 2.49 | 2.49 | 2.49 |
Visual
df_clean <- df %>%
filter(!is.na(gen) & !is.na(stfgov))
df_probs <- df_clean %>%
count(stfgov, gen) %>%
group_by(gen) %>%
mutate(prob = n / sum(n))
ggplot(df_probs, aes(x = as.factor(stfgov), y = prob, color = gen)) +
geom_point() +
geom_line(aes(group = gen)) +
labs(title = "Conditional Probabilities of Satisfaction with National Government",
subtitle = "by Generation",
x = "Satisfaction Scale",
y = "Probability") +
theme_minimal()