R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Setting up environment

# 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
## Loading required package: carData
## 
## 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")
france_data <- ess %>%
  filter(cntry == "FR") %>%
  select(agea, gndr,essround, cntry, imbgeco,imwbcnt,imdfetn, hinctnt, hincfel,hincsrc)
 


france_clean <- france_data%>%
   # Recoding
  mutate(
    agea = ifelse(agea < 999, agea, NA),
    gndr = ifelse(gndr %in% c(1:2), gndr, NA), 
    imbgeco= ifelse(imbgeco %in% c(77, 88,99), NA, imbgeco),
    imwbcnt= ifelse(imwbcnt %in% c(77, 88,99), NA, imwbcnt),
    imdfetn= ifelse(imdfetn %in% c(7,8,9), NA, imdfetn),
    hinctnt= ifelse(hinctnt %in% c(77, 88,99), NA, hinctnt),
    hincfel= ifelse(hincfel %in% c(7,8,9), NA, hincfel),
    hincsrc= ifelse(hincsrc%in% c(77, 88,99), NA, hincsrc),
    year = case_when(essround == 1 ~ 2002,
                         essround == 2 ~ 2004,
                         essround == 3 ~ 2006,
                         essround == 4 ~ 2008,
                         essround == 5 ~ 2010,
                         essround == 6 ~ 2012,
                         essround == 7 ~ 2014,
                         essround == 8 ~ 2016,
                         essround == 9 ~ 2018,
                         essround == 10 ~ 2020))
model1 <- lm(imwbcnt ~  hinctnt, data = france_clean)
model2 <- lm(imwbcnt ~  hinctnt + gndr + agea + year, data = france_clean)
model3 <- lm(imwbcnt ~  hinctnt + gndr + agea + year + hinctnt*gndr + agea*year + hinctnt*year
             ,data = france_clean)
modelsummary(list(model1,model2,model3))
 (1)   (2)   (3)
(Intercept) 3.547 −4.017 18.755
(0.124) (77.582) (347.500)
hinctnt 0.141 0.116 1.644
(0.018) (0.018) (35.492)
gndr −0.034 −0.478
(0.078) (0.247)
agea −0.026 −0.766
(0.002) (4.549)
year 0.004 −0.007
(0.039) (0.173)
hinctnt × gndr 0.067
(0.036)
agea × year 0.000
(0.002)
hinctnt × year −0.001
(0.018)
Num.Obs. 3220 3220 3220
R2 0.019 0.057 0.058
R2 Adj. 0.019 0.056 0.056
AIC 14309.7 14189.1 14191.5
BIC 14328.0 14225.6 14246.2
Log.Lik. −7151.873 −7088.547 −7086.750
RMSE 2.23 2.19 2.19