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.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: package 'infer' was built under R version 4.3.2
## Warning: package 'remotes' was built under R version 4.3.2
## [[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")
BE_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep  = ifelse(trstep  %in% c(77, 88, 99), NA, trstep), 
  )
unique(BE_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
BE_data <- BE_data %>% filter(!is.na(wrkprty))
BE_data <- BE_data %>%
  mutate(
    wrkprty = case_when(
      wrkprty == 1 ~ "Yes",
      wrkprty == 2 ~ "No",
      wrkprty == 7 ~ NA_character_,
      wrkprty == 8 ~ NA_character_,
      wrkprty == 9 ~ NA_character_,
      TRUE ~ as.character(wrkprty)
  ),
)
model1 <- lm(trstep ~ wrkprty, data = BE_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic     p.value
##   <chr>          <dbl>     <dbl>     <dbl>       <dbl>
## 1 (Intercept)    4.94     0.0187    264.   0          
## 2 wrkprtyYes     0.431    0.0867      4.97 0.000000683
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 represents the predicted value of the dependent variable (trust in the European Parliament) when the explanatory variable (in this case, whether working in a political party) is zero or for its reference category (in this case, not working in a political party).In other words, for individuals who do not work for political parties, the average score predicting their level of trust in the European Parliament is about 4.94.The 0.43 means that individuals who work for political parties have on average 0.43 points more trust in the European Parliament than those who do not.

Task 2

BG_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem  = ifelse(stfdem   %in% c(77, 88, 99), NA, stfdem), 
  )
unique(BE_data$stfdem)
##  [1] 99  6  8  7  2 88  5  9  4  1 10  3  0 77
BG_data <- BG_data %>%
  mutate(
    brncntr = case_when(
      brncntr == 1 ~ "Yes",
      brncntr == 2 ~ "No",
      brncntr == 7 ~ NA_character_,
      brncntr == 8 ~ NA_character_,
      brncntr == 9 ~ NA_character_,
      TRUE ~ as.character(brncntr)
  ),
)
model2 <- lm(stfdem ~ brncntr, data = BG_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)  brncntrYes 
##   2.8041237   0.1189909
tidy(model2)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    2.80      0.226    12.4   5.30e-35
## 2 brncntrYes     0.119     0.227     0.523 6.01e- 1

The expected average satisfaction with democracy for respondents that were not in Bulgaria is 2.8041237-0.1189909=2.6851328 ## Task 3 Relationship with gender and wrclmch - How worried about climate change in Czechia

CZ_data <- ess %>%
  filter(cntry == "CZ") %>%
  mutate(wrclmch  = ifelse(wrclmch  %in% c(6,7,8,9), NA, wrclmch), 
  )
unique(CZ_data$wrclmch)
## [1] NA  2  5  3  1  4
CZ_data <- CZ_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 7 ~ NA_character_,
      gndr == 8 ~ NA_character_,
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
  ),
)
model3 <- lm(wrclmch ~ gndr, data = CZ_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept)    gndrMale 
##   3.0862348  -0.2897148
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{wrclmch}} = 3.09 - 0.29(\operatorname{gndr}_{\operatorname{Male}}) \] 3.09means that the average Czech people are expected to vote 3.09 for worried about climate change, and males are tend to vote 0.29 value higher than females.