Task 1

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

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.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ 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"        
## 
## [[6]]
##  [1] "remotes"      "broom"        "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")
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
          wrkprty = case_when(
           wrkprty == 1 ~ "Yes",
           wrkprty == 2 ~ "No",
           wrkprty %in% c(7, 8, 9) ~ NA_character_,
           TRUE ~ as.character(wrkprty))
  )
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
unique(belgium_data$wrkprty)
## [1] "No"  "Yes" NA
belgium_data <- belgium_data %>% filter(!is.na(trstep))
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6  4  9  3  1 10  2
unique(belgium_data$wrkprty)
## [1] "No"  "Yes"
model_1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model_1)
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(model_1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \] Intercept 4.94 explains that if the person worked in a political party, approximately 4.94 people would trust the European Parliament. As the slope for the equation is 0.43, it accounts that there will be an increase of 0.43 for people who trust in the European Parliament as a person worked in a political party increased by one.

Task 2

bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), 
         native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )
unique(bulgaria_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
unique(bulgaria_data$native)
## [1] "Yes" "No"  NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
bulgaria_data <- bulgaria_data %>% filter(!is.na(native))
unique(bulgaria_data$stfdem)
##  [1]  0  1  2  3  5  4  6  8  7 10  9
unique(bulgaria_data$native)
## [1] "Yes" "No"
model_2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model_2)
print(coefficients)
## (Intercept)   nativeYes 
##   2.8041237   0.1189909
summary(model_2)
## 
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9231 -1.9231  0.0769  2.0769  7.1959 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.8041     0.2265  12.382   <2e-16 ***
## nativeYes     0.1190     0.2274   0.523    0.601    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.231 on 12334 degrees of freedom
## Multiple R-squared:  2.22e-05,   Adjusted R-squared:  -5.887e-05 
## F-statistic: 0.2739 on 1 and 12334 DF,  p-value: 0.6007
equatiomatic::extract_eq(model_2, use_coefs = TRUE)

\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \] From the Bulgaria dataset, we can infer that the expected average satisfaction with democracy for respondents who were not born in Bulgaria is approximately 2.8 as the intercept for the equation is 2.8.

Task 3

swiss_data <- ess %>%
  filter(cntry == "DE") %>%
  mutate(trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
         born = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )
unique(swiss_data$trstlgl)
##  [1]  6  7  9  8  4 10  5  3 NA  2  0  1
unique(swiss_data$born)
## [1] "Yes" "No"  NA
swiss_data <- swiss_data %>% filter(!is.na(trstlgl))
swiss_data <- swiss_data %>% filter(!is.na(born))
unique(swiss_data$trstlgl)
##  [1]  6  7  9  8  4 10  5  3  2  0  1
unique(swiss_data$born)
## [1] "Yes" "No"
model_3 <- lm(trstlgl ~ born, data = swiss_data)
coefficients <- coef(model_3)
print(coefficients)
## (Intercept)     bornYes 
##   6.0922406  -0.3092999
equatiomatic::extract_eq(model_3, use_coefs = TRUE)

\[ \operatorname{\widehat{trstlgl}} = 6.09 - 0.31(\operatorname{born}_{\operatorname{Yes}}) \] From the Switzerland dataset, we can infer that intercept 6.09 explains that if the person is born in Switzerland, approximately 6.09 people would trust Switzerland’s legal system. As the slope for the equation is -0.31, it accounts that there will be a decrease of 0.31 for people who trust the legal system as a person was born in Switzerland.