set enviroment

# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects") # add any you need here

# 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
## Warning: 程辑包'effects'是用R版本4.3.2 来建造的
## 载入需要的程辑包:carData
## Warning: 程辑包'carData'是用R版本4.3.2 来建造的
## 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"
ess <- read_fst("All-ESS-Data.fst")

create year variable

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]}

read and clean france data

 France_data <- ess %>%
  filter(cntry == "FR") %>%
  mutate(stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
         stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco),
         trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
         netusoft = ifelse(netusoft %in% c(7, 8, 9), NA, netusoft),
         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"
    ),
    
     gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
    ))

simple model

model1 <- lm(stfgov ~ educ_level, data = France_data)
model2 <- lm(stfgov ~ educ_level + stfeco, data = France_data)
model3 <- lm(stfgov ~ educ_level + stfeco + educ_level*stfeco, data = France_data)
modelsummary(list(model1, model2, model3))
 (1)   (2)   (3)
(Intercept) 4.018 1.428 1.472
(0.035) (0.037) (0.061)
educ_levelNo BA -0.360 -0.046 -0.101
(0.040) (0.032) (0.067)
stfeco 0.668 0.656
(0.006) (0.014)
educ_levelNo BA × stfeco 0.014
(0.016)
Num.Obs. 18758 18588 18588
R2 0.004 0.375 0.375
R2 Adj. 0.004 0.375 0.375
AIC 83810.2 74338.2 74339.3
BIC 83833.8 74369.5 74378.5
Log.Lik. -41902.119 -37165.089 -37164.667
RMSE 2.26 1.79 1.79
modelsummary(
  list(model1, model2, model3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept") 
 (1)   (2)   (3)
educ_levelNo BA -0.4 (0.0)*** 0.0 (0.0) -0.1 (0.1)
stfeco 0.7 (0.0)*** 0.7 (0.0)***
educ_levelNo BA × stfeco 0.0 (0.0)
Num.Obs. 18758 18588 18588
R2 0.004 0.375 0.375
R2 Adj. 0.004 0.375 0.375
AIC 83810.2 74338.2 74339.3
BIC 83833.8 74369.5 74378.5
Log.Lik. -41902.119 -37165.089 -37164.667
RMSE 2.26 1.79 1.79