packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom")

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
## [[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] "broom"        "modelsummary" "fst"          "infer"        "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"
ess <- read_fst("All-ESS-Data.fst")

Task 1

Belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
  )

unique(Belgium_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
Belgium_data <- Belgium_data %>% filter(!is.na(trstep))
unique(Belgium_data$trstep)
##  [1]  0  7  8  5  6  4  9  3  1 10  2
Belgium_data <- Belgium_data %>%
  mutate(
    wrkprty = case_when(
      wrkprty == 1 ~ "Yes",
      wrkprty == 2 ~ "No",
      wrkprty %in% c(7, 8, 9) ~ NA_character_,
      TRUE ~ as.character(wrkprty))
    )

table(Belgium_data$wrkprty)
## 
##    No   Yes 
## 14871   727
model1 <- lm(trstep ~ wrkprty, data = Belgium_data)

coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338
remotes::install_github("datalorax/equatiomatic")
## Skipping install of 'equatiomatic' from a github remote, the SHA1 (29ff168f) has not changed since last install.
##   Use `force = TRUE` to force installation
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \] The intercept of the equation is 4.94, represent the predicted value of the outcome variable (Trust in the European Parliament, 0-10) when categorical variables are at their reference level. When looking at the category “Yes” in wrkprty the expected value of trstep is approximately 4.94. The 0.43 represents the difference in the predicted value of Trust in the European Parliament between the “Yes” category and the reference category (“No”) for the wrkprty variable. If a person response is “No”, their predicted average Trust in the European Parliament is 3.73. If a person response is “Yes”, their predicted average trust in politicians is 5.36.

Task 2

Belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
  )

unique(Belgium_data$stfdem)
##  [1] NA  6  8  7  2  5  9  4  1 10  3  0
Belgium_data <- Belgium_data %>% filter(!is.na(stfdem))
unique(Belgium_data$stfdem)
##  [1]  6  8  7  2  5  9  4  1 10  3  0
Belgium_data <- Belgium_data %>%
  mutate(
    native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )

table(Belgium_data$native)
## 
##    No   Yes 
##  2038 15147
model2 <- lm(stfdem ~ native, data = Belgium_data)

coefficients <- coef(model2)
print(coefficients)
## (Intercept)   nativeYes 
##   6.0775270  -0.7120421
tidy(model2)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    6.08     0.0471     129.  0       
## 2 nativeYes     -0.712    0.0502     -14.2 1.85e-45
tidy(model2) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 6.078 0.047 129.028 0
nativeYes -0.712 0.050 -14.192 0

The expected average satisfaction with democracy for respondents that were not born in Bulgaria would be 6.078.

Task 3

iceland_data <- ess %>%
  filter(cntry == "IS") %>%
  mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
  )

unique(iceland_data$happy)
##  [1] NA 10  9  6  4  7  8  2  5  1  3  0
iceland_data <- iceland_data %>% filter(!is.na(happy))
unique(iceland_data$happy)
##  [1] 10  9  6  4  7  8  2  5  1  3  0
iceland_data <- iceland_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr))
    )

table(iceland_data$gndr)
## 
## Female   Male 
##   2011   1924
model3 <- lm(happy ~ gndr, data = iceland_data)

coefficients <- coef(model3)
print(coefficients)
## (Intercept)    gndrMale 
##   8.2729985  -0.1082376
remotes::install_github("datalorax/equatiomatic")
## Skipping install of 'equatiomatic' from a github remote, the SHA1 (29ff168f) has not changed since last install.
##   Use `force = TRUE` to force installation
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{happy}} = 8.27 - 0.11(\operatorname{gndr}_{\operatorname{Male}}) \] The intercept of the equation is 8.27, represent the predicted value of the outcome variable (How happy you are, 0-10) when categorical variables are at their reference level. When looking at the category “Male” in gndr the expected value of happy is approximately 8.27. The negative 0.11 represents the difference in the predicted value of How happy you are between the “Male” category and the reference category (“Femlae”) for the gndr variable. If the person is “female” , their predicted average happiness is 8.27. If the person “Male”, their predicted average happiness is 8.16.