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/Assignment 3")

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

##Cleaning Variables

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))

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

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

##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")

print(p_sh)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.482

##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")
## 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

##Model 3

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")

print(p_su)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.187

##Summarizing Models

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)


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)


modelsummary(list(sblazy_hinctnta, sblazy_edulvla, sblazy_uemp5yr))
 (1)   (2)   (3)
(Intercept) 2.291 2.156 1.573
(0.046) (0.054) (0.176)
hinctnta 0.018
(0.007)
edulvla 0.083
(0.018)
uemp5yr2 0.551
(0.261)
Num.Obs. 1990 1990 508
R2 0.003 0.011
R2 Adj. 0.002 0.010
AIC 5637.6 5621.7
BIC 5654.4 5638.5
Log.Lik. −2815.785 −2807.872
RMSE 1.00 0.99 0.34
extract_eq(sblazy_edulvla, use_coefs = TRUE)

\[ \operatorname{\widehat{sblazy}} = 2.16 + 0.08(\operatorname{edulvla}) \]

extract_eq(sblazy_hinctnta, use_coefs = TRUE)

\[ \operatorname{\widehat{sblazy}} = 2.29 + 0.02(\operatorname{hinctnta}) \]