packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", "equatiomatic", "infer",
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") 

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.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: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "equatiomatic" "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "infer"        "equatiomatic" "RColorBrewer" "modelsummary" "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[7]]
##  [1] "fst"          "infer"        "equatiomatic" "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[8]]
##  [1] "viridis"      "viridisLite"  "fst"          "infer"        "equatiomatic"
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "infer"       
##  [6] "equatiomatic" "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "infer"        "equatiomatic" "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "infer"        "equatiomatic" "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[12]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "infer"        "equatiomatic" "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[13]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "infer"        "equatiomatic" "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "infer"        "equatiomatic"
## [11] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"
set.seed(69)


setwd("C:/Users/sduan/Desktop/SOC252 resources/")

ess <- read_fst("All-ESS-Data.fst")

##Data Cleaning

ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  ess$year[ess$essround == i] <- replacements[i]
}
gb_2008 <- ess %>%
  filter(cntry == "GB", year == 2008)

gb_2008_clean <- gb_2008 %>%
  mutate(hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
         edulvla = ifelse(edulvla %in% c(55, 0, 77, 88, 99), NA, edulvla),
         uemp5yr = ifelse(uemp5yr %in% c(6, 7, 8, 9), NA, uemp5yr),
         sblazy = ifelse(sblazy %in% c(7, 8, 9), NA, sblazy),
         uentrjb = ifelse(uentrjb %in% c(7, 8, 9), NA, uentrjb))

gb_2008_clean$edulvla <- recode(gb_2008_clean$edulvla, '5' = 4)

gb_linear <- na.omit(gb_2008_clean %>%
  select(hinctnta, edulvla, sblazy, uentrjb))

gb_binomial <- na.omit(gb_2008_clean %>%
  select(uemp5yr, sblazy, uentrjb))

##Data Summary

gb_skim <- na.omit(gb_2008_clean %>%
                     select(hinctnta, edulvla, uemp5yr, sblazy, uentrjb))

datasummary_skim(gb_skim)
Unique (#) Missing (%) Mean SD Min Median Max
hinctnta 10 0 4.6 3.0 1.0 4.0 10.0
edulvla 4 0 2.7 1.2 1.0 3.0 4.0
uemp5yr 2 0 1.6 0.5 1.0 2.0 2.0
sblazy 5 0 2.5 1.0 1.0 2.0 5.0
uentrjb 5 0 2.9 1.1 1.0 3.0 5.0

##Comparing UK to ESS Baseline

yrbrn_gb <- gb_2008_clean %>%
  filter(yrbrn > 1908 & yrbrn < 1994) %>%
  select(yrbrn, sblazy) %>%
  group_by(yrbrn) %>%
  summarize(mean_sblazy = mean(sblazy, na.rm = TRUE))

theme_set(theme_bw())

ggplot(data = yrbrn_gb, aes(x = yrbrn, y = mean_sblazy)) +
  geom_line(color = "pink") +
     ylim(1, 5)

ess_2008 <- ess %>%
  filter(year == 2008)

yrbrn_ess <- ess_2008 %>%
     filter(yrbrn > 1908 & yrbrn < 1994) %>%
     mutate(sblazy = ifelse(sblazy %in% c(7, 8, 9), NA, sblazy)) %>%
     select(yrbrn, sblazy) %>%
  group_by(yrbrn) %>%
  summarize(mean_sblazy = mean(sblazy, na.rm = TRUE))


ggplot(data = yrbrn_ess, aes(x = yrbrn, y = mean_sblazy)) +
     geom_line(color = "pink") +
     ylim(1, 5)

##Model 1 (sblazy ~ hinctnta)

gb_linear$hinctnta <- as.factor(gb_linear$hinctnta)
gb_linear$sblazy <- as.factor(gb_linear$sblazy)

chisq_sh <- gb_linear %>%
  specify(sblazy ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_sh <- gb_linear %>%
  specify(sblazy ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_sh <- nulldist_sh %>%
  get_p_value(obs_stat = chisq_sh, direction = "greater")

nulldist_sh %>% visualize() +
  shade_p_value(chisq_sh, direction = "greater", color = "pink")

print(p_sh)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.426
check1 <- xtabs(~sblazy + hinctnta, data = gb_linear)
cprop(check1)
##        hinctnta
## sblazy  1     2     3     4     5     6     7     8     9     10    All  
##   1      16.8  17.9  13.8  14.0  15.5  15.6  18.8  12.0  17.5  14.9  15.8
##   2      51.6  53.8  52.7  53.9  51.0  52.6  48.4  47.3  47.5  42.5  50.1
##   3      14.0  12.0  12.8  18.0  17.4  17.9  14.6  17.4  14.4  18.0  15.5
##   4      15.2  15.0  18.7  11.8  12.9  13.3  16.9  21.2  19.4  23.2  16.9
##   5       2.4   1.3   2.0   2.2   3.2   0.6   1.4   2.2   1.2   1.3   1.8
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0

##Model 2 (sblazy ~ edulvla)

gb_linear$edulvla <- as.factor(gb_linear$edulvla)
gb_linear$sblazy <- as.factor(gb_linear$sblazy)

chisq_se <- gb_linear %>%
  specify(sblazy ~ edulvla) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_se <- gb_linear %>%
  specify(sblazy ~ edulvla) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_se <- nulldist_se %>%
  get_p_value(obs_stat = chisq_se, 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.
nulldist_se %>% visualize() +
  shade_p_value(chisq_se, direction = "greater", color = "pink")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

print(p_se)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
check2 <- xtabs(~sblazy + edulvla, data = gb_linear)
cprop(check2)
##        edulvla
## sblazy  1     2     3     4     All  
##   1      18.2  15.1  15.5  14.8  15.8
##   2      53.8  57.8  52.7  44.4  50.1
##   3      12.4  15.1  12.8  17.9  15.5
##   4      12.8  11.5  18.1  20.9  16.9
##   5       2.7   0.5   0.9   2.0   1.8
##   Total 100.0 100.0 100.0 100.0 100.0

##Model 3 (sblazy ~ uemp5yr)

gb_binomial$uemp5yr <- as.factor(gb_binomial$uemp5yr)
gb_binomial$sblazy <- as.factor(gb_binomial$sblazy)

chisq_su <- gb_binomial %>%
  specify(sblazy ~ uemp5yr) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_su <- gb_binomial %>%
  specify(sblazy ~ uemp5yr) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_su <- nulldist_su %>%
  get_p_value(obs_stat = chisq_su, direction = "greater")

nulldist_su %>% visualize() +
  shade_p_value(chisq_su, direction = "greater", color = "pink")

print(p_su)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.211
check3 <- xtabs(~sblazy + uemp5yr, data = gb_binomial)
cprop(check3)
##        uemp5yr
## sblazy  1     2     All  
##   1      16.9  10.7  13.5
##   2      44.0  45.7  45.0
##   3      18.2  19.6  19.0
##   4      20.0  21.4  20.8
##   5       0.9   2.5   1.8
##   Total 100.0 100.0 100.0

##Model 4 (uentrjb ~ hinctnta)

gb_linear$hinctnta <- as.factor(gb_linear$hinctnta)
gb_linear$uentrjb <- as.factor(gb_linear$uentrjb)

chisq_uh <- gb_linear %>%
  specify(uentrjb ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_uh <- gb_linear %>%
  specify(uentrjb ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_uh <- nulldist_uh %>%
  get_p_value(obs_stat = chisq_uh, 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.
nulldist_uh %>% visualize() +
  shade_p_value(chisq_uh, direction = "greater", color = "pink")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

print(p_uh)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
check4 <- xtabs(~uentrjb + hinctnta, data = gb_linear)
cprop(check4)
##        hinctnta
## uentrjb 1     2     3     4     5     6     7     8     9     10    All  
##   1      11.6  10.3  11.3   7.3   4.5   8.7   8.0   8.2  12.5   3.1   8.6
##   2      46.4  46.2  38.4  36.5  40.6  40.5  32.9  31.5  36.2  25.9  37.7
##   3      22.4  18.8  25.6  20.8  23.2  17.9  27.7  23.4  18.8  28.5  22.9
##   4      18.4  22.6  21.2  30.9  29.7  31.2  29.1  35.3  31.2  39.9  28.6
##   5       1.2   2.1   3.4   4.5   1.9   1.7   2.3   1.6   1.2   2.6   2.3
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0

##Model Five (uentrjb ~ edulvla)

gb_linear$edulvla <- as.factor(gb_linear$edulvla)
gb_linear$uentrjb <- as.factor(gb_linear$uentrjb)

chisq_ue <- gb_linear %>%
  specify(uentrjb ~ edulvla) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_ue <- gb_linear %>%
  specify(uentrjb ~ edulvla) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_ue <- nulldist_ue %>%
  get_p_value(obs_stat = chisq_ue, 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.
nulldist_ue %>% visualize() +
  shade_p_value(chisq_ue, direction = "greater", color = "pink")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

print(p_ue)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
check5 <- xtabs(~uentrjb + edulvla, data = gb_linear)
cprop(check5)
##        edulvla
## uentrjb 1     2     3     4     All  
##   1      13.5   8.2   7.1   6.5   8.6
##   2      47.0  40.5  45.6  29.5  37.7
##   3      19.0  24.1  19.5  25.3  22.9
##   4      16.8  25.8  27.4  36.3  28.6
##   5       3.7   1.4   0.4   2.3   2.3
##   Total 100.0 100.0 100.0 100.0 100.0

##Model Six (uentrjb ~ uemp5yr)

gb_binomial$uemp5yr <- as.factor(gb_binomial$uemp5yr)
gb_binomial$uentrjb <- as.factor(gb_binomial$uentrjb)

chisq_uu <- gb_binomial %>%
  specify(uentrjb ~ uemp5yr) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
 
nulldist_uu <- gb_binomial %>%
  specify(uentrjb ~ uemp5yr) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")

p_uu <- nulldist_uu %>%
  get_p_value(obs_stat = chisq_uu, direction = "greater")

nulldist_uu %>% visualize() +
  shade_p_value(chisq_uu, direction = "greater", color = "pink")

print(p_uu)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.796
check6 <- xtabs(~uentrjb + uemp5yr, data = gb_binomial)
cprop(check6)
##        uemp5yr
## uentrjb 1     2     All  
##   1       8.4   8.2   8.3
##   2      34.7  30.0  32.1
##   3      21.3  21.4  21.4
##   4      32.0  37.1  34.9
##   5       3.6   3.2   3.4
##   Total 100.0 100.0 100.0

Model Summary

gb_linear$hinctnta <- as.numeric(gb_linear$hinctnta)
gb_linear$edulvla <- as.numeric(gb_linear$edulvla)
gb_binomial$uemp5yr <- as.factor(gb_binomial$uemp5yr)
gb_binomial$sblazy <- as.factor(gb_binomial$sblazy)
gb_linear$sblazy <- as.numeric(gb_linear$sblazy)
gb_linear$uentrjb <- as.numeric(gb_linear$uentrjb)
gb_binomial$uentrjb <- as.factor(gb_binomial$uentrjb)

sblazy_hinctnta <- lm(sblazy ~ hinctnta, data = gb_linear)
sblazy_edulvla <- lm(sblazy ~ edulvla, data = gb_linear)
sblazy_uemp5yr <- glm(sblazy~uemp5yr, family = binomial, data = gb_binomial)
uentrjb_hinctnta <- lm(uentrjb ~ hinctnta, data = gb_linear)
uentrjb_edulvla <- lm(uentrjb ~ edulvla, data = gb_linear)
uentrjb_uemp5yr <- glm(uentrjb~uemp5yr, family = binomial, data = gb_binomial)

modelsummary(list(sblazy_hinctnta, sblazy_edulvla, sblazy_uemp5yr, uentrjb_hinctnta, uentrjb_edulvla, uentrjb_uemp5yr))
 (1)   (2)   (3)   (4)   (5)   (6)
(Intercept) 2.288 2.150 1.594 2.520 2.356 2.383
(0.046) (0.054) (0.178) (0.046) (0.055) (0.240)
hinctnta 0.019 0.049
(0.007) (0.008)
edulvla 0.085 0.153
(0.018) (0.018)
uemp5yr2 0.527 0.030
(0.263) (0.324)
Num.Obs. 1978 1978 505 1978 1978 505
R2 0.003 0.012 0.021 0.035
R2 Adj. 0.003 0.011 0.020 0.035
AIC 5606.6 5590.0 5673.6 5644.7
BIC 5623.4 5606.8 5690.4 5661.5
Log.Lik. −2800.324 −2792.001 −2833.814 −2819.372
RMSE 1.00 0.99 0.34 1.01 1.01 0.28
extract_eq(sblazy_edulvla, use_coefs = TRUE)

\[ \operatorname{\widehat{sblazy}} = 2.15 + 0.09(\operatorname{edulvla}) \]