Participation_4

# 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.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ 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")
sweden_data <- ess %>%
  filter(cntry == "SE") %>%
  
  mutate(
    vote = case_when(
    vote == 1 ~ 1,              # Keep 1 as numeric
    vote == 2 ~ 2,              # Recode 2 as numeric 0
    vote %in% c(3, 7, 8, 9) ~ NA_real_,  # Set 7, 8, 9 as NA keeping the type numeric
    TRUE ~ vote                 # Keep all other values as they are
  ),
    gincdif = case_when(
    gincdif == 1 ~ 1,
    gincdif == 2 ~ 1,
    gincdif == 3 ~ 0,
    gincdif == 4 ~ 0, 
    gincdif == 5 ~ 0,
    gincdif %in% c(3, 7, 8, 9) ~ NA_real_,  # Set 7, 8, 9 as NA keeping the type numeric
    TRUE ~ gincdif                 # Keep all other values as they are
  ))
sweden_data <- sweden_data %>%
  mutate(
    # Recoding age variable, setting 999 to NA
    age = ifelse(agea == 999, NA, agea),
    # Recoding cohort variable, setting years before 1900 and after 2000 to NA
    cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn),
    # Recoding generational cohorts based on year of birth
    gen = case_when(
      yrbrn %in% 1900:1945 ~ "1",
      yrbrn %in% 1946:1964 ~ "2",
      yrbrn %in% 1965:1979 ~ "3",
      yrbrn %in% 1980:1996 ~ "4",
      TRUE ~ as.character(cohort)  # Keeping other values as character if they do not fit the ranges
    ),
    # Converting cohort to a factor with labels
    gen = factor(gen,
                 levels = c("1", "2", "3", "4"),
                 labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials"))
  )

table(sweden_data$age)

 15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 
163 242 261 247 243 250 244 224 240 257 245 274 258 211 243 272 312 243 253 290 
 35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54 
281 263 296 313 263 299 301 311 299 260 274 259 298 254 259 297 270 290 305 300 
 55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74 
297 350 278 286 289 328 272 279 299 290 285 315 295 290 258 249 272 253 237 204 
 75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94 
213 189 177 183 149 127 133 116  93 104  86  58  52  46  41  66  18   9  11   6 
 95  96  97  99 114 
  4   3   1   1   1 
table(sweden_data$cohort)

1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 
 102  133  129  120  163  150  181  175  190  212  236  201  272  309  297  335 
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 
 346  355  327  364  283  316  327  297  262  309  268  279  301  274  288  300 
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 
 276  287  328  318  305  343  329  286  256  289  276  247  282  297  287  262 
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 
 251  235  253  246  253  250  222  262  225  254  253  227  221  191  200  179 
1994 1995 1996 1997 1998 1999 2000 
 132  116  125  110   73   60   36 
table(sweden_data$gen)

    Interwar Baby Boomers        Gen X  Millennials 
        4021         5787         4263         3609 
table(sweden_data$gincdif)

    0     1 
 6240 11659 
table(sweden_data$vote)

    1     2 
15348  1433 
sweden_data <- sweden_data %>%
  mutate(
    current_union_member = case_when(
      mbtru == 1 ~ "Yes",
      mbtru %in% c(7, 8, 9) ~ NA_character_,
      TRUE ~ "No"
  ))

table(sweden_data$current_union_member)

  No  Yes 
8955 9192 
sweden_data <- sweden_data %>%
  mutate(polintr = case_when(
    polintr %in% c(1, 2) ~ "Interested",
    polintr %in% c(3, 4) ~ "Not Interested",
    polintr %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(polintr)
  )) %>%
  # Recode work for party
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  )) %>%
  # Recode worked for civil org/association
  mutate(workcivil = case_when(
    wrkorg == 1 ~ "Yes",
    wrkorg == 2 ~ "No",
    wrkorg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkorg)
  )) %>%
  # Recode participation in protest
  mutate(protest = case_when(
    pbldmn == 1 ~ "Yes",
    pbldmn == 2 ~ "No",
    pbldmn %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(pbldmn)
  )) 

table(sweden_data$wrkprty)

   No   Yes 
15173   748 
table(sweden_data$polintr)

    Interested Not Interested 
         11257           6938 
table(sweden_data$workcivil)

   No   Yes 
11050  4867 
table(sweden_data$protest)

   No   Yes 
14687  1226 
sweden_data <- sweden_data %>%
  mutate(
    # Recoding gender
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
    ))
sweden_data <- sweden_data %>%
  mutate(
    # Recoding individual's education for rounds 1-4
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    # Re-coding individual's education for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ))
sweden_data <- sweden_data %>%
  mutate(
educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ))

table(sweden_data$educ_level)

   BA No BA 
 4666 13550 
sweden_data$gincdif <- as.numeric(sweden_data$gincdif)
model1 <- lm(gincdif ~ mbtru, data = sweden_data)
model2 <- lm(gincdif ~ mbtru + educ_level, data = sweden_data)
model3 <- lm(gincdif ~ mbtru + educ_level + mbtru*educ_level, data = sweden_data)
modelsummary(
  list(model1, model2, model3))
 (1)   (2)   (3)
(Intercept) 0.757 0.686 0.691
(0.008) (0.009) (0.015)
mbtru −0.061 −0.070 −0.073
(0.004) (0.004) (0.009)
educ_levelNo BA 0.115 0.109
(0.008) (0.018)
mbtru × educ_levelNo BA 0.004
(0.010)
Num.Obs. 17899 17899 17899
R2 0.013 0.024 0.024
R2 Adj. 0.013 0.023 0.023
AIC 24039.4 23841.8 23843.7
BIC 24062.7 23873.0 23882.7
Log.Lik. −12016.680 −11916.923 −11916.846
RMSE 0.47 0.47 0.47
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)
mbtru −0.1 (0.0)*** −0.1 (0.0)*** −0.1 (0.0)***
educ_levelNo BA 0.1 (0.0)*** 0.1 (0.0)***
mbtru × educ_levelNo BA 0.0 (0.0)
Num.Obs. 17899 17899 17899
R2 0.013 0.024 0.024
R2 Adj. 0.013 0.023 0.023
AIC 24039.4 23841.8 23843.7
BIC 24062.7 23873.0 23882.7
Log.Lik. −12016.680 −11916.923 −11916.846
RMSE 0.47 0.47 0.47
# We use the "effect" function to call the interaction term from model 3. We assign the results to "effect_plot"
interaction_plot <- effect("mbtru*educ_level", model3, na.rm=TRUE)

# We plot here the interaction term

plot(interaction_plot,
     main="Interaction effect Union Membership x gender",
     xlab="Union Membership",
     ylab="Government should reduce income inequality")