# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "forcats", "RColorBrewer", 
              "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "equatiomatic") 
# Install packages if they aren't installed already
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
## Warning: 程辑包'effects'是用R版本4.3.2 来建造的
## 载入需要的程辑包:carData
## Warning: 程辑包'carData'是用R版本4.3.2 来建造的
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## Warning: 程辑包'survey'是用R版本4.3.2 来建造的
## 载入需要的程辑包:grid
## 载入需要的程辑包:Matrix
## 
## 载入程辑包:'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 载入需要的程辑包:survival
## 
## 载入程辑包:'survey'
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## 
## 载入程辑包:'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 载入需要的程辑包:viridisLite
## Warning: 程辑包'knitr'是用R版本4.3.2 来建造的
## 
## 载入程辑包:'flextable'
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## [[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] "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"        
## 
## [[9]]
##  [1] "RColorBrewer" "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"        
## 
## [[10]]
##  [1] "viridis"      "viridisLite"  "RColorBrewer" "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] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "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"        
## 
## [[12]]
##  [1] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "RColorBrewer"
##  [6] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
## [11] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [16] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [21] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"        
## 
## [[13]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "RColorBrewer" "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] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "RColorBrewer" "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"        
## 
## [[15]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "knitr"        "viridis"     
##  [6] "viridisLite"  "RColorBrewer" "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"        
## 
## [[16]]
##  [1] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "knitr"       
##  [6] "viridis"      "viridisLite"  "RColorBrewer" "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"        
## 
## [[17]]
##  [1] "equatiomatic" "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "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"
# read data
ess <- read_fst("All-ESS-Data.fst")
#read france data
france_data <- read_fst("france_data.fst")
clean_data <- france_data %>%
  mutate(stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
         edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
      lrscale = case_when( 
      lrscale %in% 0:3 ~ "Left",                       # Recode values 0 to 3 as "Left"
      lrscale %in% 7:10 ~ "Right",                     # Recode values 7 to 10 as "Right"
      lrscale %in% 4:6 ~ "Moderate",                   # Recode values 4, 5, 6 as "Moderate"
      lrscale %in% c(77, 88, 99) ~ NA_character_       # Recode values 77, 88, 99 as NA
    ),
     gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
     ),  
    geo = case_when(
    domicil %in% c(1, 2) ~ "urban", #urban
    domicil %in% c(3, 4, 5) ~ "rural", #rural
    TRUE ~ NA_character_
     ))
clean_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  clean_data$year[clean_data$essround == i] <- replacements[i]
}
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]
}
test_stat <- clean_data %>%
  specify(explanatory = educ_level, 
          response = stfgov) %>% 
  hypothesize(null = "independence") %>%
  calculate(stat = "t") 
## Warning: Removed 280 rows containing missing values.
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "BA" - "No BA", or divided in the order "BA" / "No BA" for ratio-based
## statistics. To specify this order yourself, supply `order = c("BA", "No BA")`
## to the calculate() function.
print(test_stat$stat)
##        t 
## 9.370928
null_distribution <- clean_data %>%
  specify(explanatory = educ_level,
          response = stfgov) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "t")
## Warning: Removed 280 rows containing missing values.
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "BA" - "No BA", or divided in the order "BA" / "No BA" for ratio-based
## statistics. To specify this order yourself, supply `order = c("BA", "No BA")`
## to the calculate() function.
p_val <- 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_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")

null_distribution
## Response: stfgov (numeric)
## Explanatory: educ_level (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  0.837 
##  2         2  0.950 
##  3         3 -1.05  
##  4         4  1.14  
##  5         5  0.204 
##  6         6 -0.841 
##  7         7 -0.384 
##  8         8 -1.02  
##  9         9 -0.0134
## 10        10 -0.0916
## # ℹ 990 more rows
model1 <- lm(stfgov ~ educ_level, data = clean_data)
model2 <- lm(stfgov ~ educ_level + lrscale, data = clean_data)
model3 <- lm(stfgov ~ educ_level + lrscale + geo, data = clean_data)
model4 <- lm(stfgov ~ educ_level + lrscale + geo + geo*educ_level, data = clean_data)
model5 <- lm(stfgov ~ educ_level + lrscale + geo + geo*educ_level + lrscale*educ_level, data = clean_data)
model6 <- lm(stfgov ~ educ_level + lrscale + geo + geo*educ_level + lrscale*educ_level + geo*lrscale, data = clean_data)
model7 <- lm(stfgov ~ educ_level + lrscale + lrscale*educ_level, data = clean_data)

modelsummary(list(model1, model2, model3, model4, model5, model6, model7))
 (1)   (2)   (3)   (4)   (5)   (6)   (7)
(Intercept) 4.018 3.503 3.417 3.451 3.255 3.252 3.316
(0.035) (0.043) (0.046) (0.054) (0.069) (0.074) (0.060)
educ_levelNo BA -0.360 -0.424 -0.392 -0.434 -0.159 -0.158 -0.159
(0.040) (0.040) (0.040) (0.052) (0.080) (0.081) (0.071)
lrscaleModerate 0.717 0.725 0.724 1.002 0.981 0.993
(0.040) (0.040) (0.040) (0.079) (0.089) (0.079)
lrscaleRight 0.957 0.967 0.966 1.265 1.324 1.256
(0.047) (0.047) (0.047) (0.096) (0.107) (0.096)
geourban 0.179 0.104 0.122 0.129
(0.036) (0.070) (0.070) (0.088)
educ_levelNo BA × geourban 0.103 0.081 0.083
(0.082) (0.082) (0.082)
educ_levelNo BA × lrscaleModerate -0.380 -0.372 -0.378
(0.092) (0.093) (0.092)
educ_levelNo BA × lrscaleRight -0.405 -0.429 -0.405
(0.110) (0.112) (0.110)
lrscaleModerate × geourban 0.050
(0.087)
lrscaleRight × geourban -0.138
(0.102)
Num.Obs. 18758 17585 17580 17580 17580 17580 17585
R2 0.004 0.030 0.032 0.032 0.033 0.033 0.031
R2 Adj. 0.004 0.030 0.031 0.031 0.032 0.033 0.031
AIC 83810.2 77905.6 77857.8 77858.2 77841.7 77841.5 77889.2
BIC 83833.8 77944.5 77904.4 77912.6 77911.7 77927.0 77943.7
Log.Lik. -41902.119 -38947.804 -38922.880 -38922.082 -38911.844 -38909.732 -38937.619
RMSE 2.26 2.22 2.21 2.21 2.21 2.21 2.22
modelsummary(
  list(model1, model2, model3, model4, model5, model6, model7),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{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)   (5)   (6)   (7)
educ_levelNo BA -0.4 (0.0)*** -0.4 (0.0)*** -0.4 (0.0)*** -0.4 (0.1)*** -0.2 (0.1)* -0.2 (0.1)+ -0.2 (0.1)*
lrscaleModerate 0.7 (0.0)*** 0.7 (0.0)*** 0.7 (0.0)*** 1.0 (0.1)*** 1.0 (0.1)*** 1.0 (0.1)***
lrscaleRight 1.0 (0.0)*** 1.0 (0.0)*** 1.0 (0.0)*** 1.3 (0.1)*** 1.3 (0.1)*** 1.3 (0.1)***
geourban 0.2 (0.0)*** 0.1 (0.1) 0.1 (0.1)+ 0.1 (0.1)
educ_levelNo BA × geourban 0.1 (0.1) 0.1 (0.1) 0.1 (0.1)
educ_levelNo BA × lrscaleModerate -0.4 (0.1)*** -0.4 (0.1)*** -0.4 (0.1)***
educ_levelNo BA × lrscaleRight -0.4 (0.1)*** -0.4 (0.1)*** -0.4 (0.1)***
lrscaleModerate × geourban 0.0 (0.1)
lrscaleRight × geourban -0.1 (0.1)
Num.Obs. 18758 17585 17580 17580 17580 17580 17585
R2 0.004 0.030 0.032 0.032 0.033 0.033 0.031
R2 Adj. 0.004 0.030 0.031 0.031 0.032 0.033 0.031
AIC 83810.2 77905.6 77857.8 77858.2 77841.7 77841.5 77889.2
BIC 83833.8 77944.5 77904.4 77912.6 77911.7 77927.0 77943.7
Log.Lik. -41902.119 -38947.804 -38922.880 -38922.082 -38911.844 -38909.732 -38937.619
RMSE 2.26 2.22 2.21 2.21 2.21 2.21 2.22
extract_eq(model7, use_coefs = TRUE)

\[ \operatorname{\widehat{stfgov}} = 3.32 - 0.16(\operatorname{educ\_level}_{\operatorname{No\ BA}}) + 0.99(\operatorname{lrscale}_{\operatorname{Moderate}}) + 1.26(\operatorname{lrscale}_{\operatorname{Right}}) - 0.38(\operatorname{educ\_level}_{\operatorname{No\ BA}} \times \operatorname{lrscale}_{\operatorname{Moderate}}) - 0.4(\operatorname{educ\_level}_{\operatorname{No\ BA}} \times \operatorname{lrscale}_{\operatorname{Right}}) \]