Task 1

packages <- c("tidyverse", "infer", "fst")
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.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.2     ✔ 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"
library(fst)
ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep), ## Example of a trust variable
  )
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)
  ))
model <- lm(trstep ~ wrkprty, data = belgium_data)
summary(model)
## 
## Call:
## lm(formula = trstep ~ wrkprty, data = belgium_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.3714 -0.9406  0.0594  2.0594  5.0594 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.94056    0.01872 263.892  < 2e-16 ***
## wrkprtyYes   0.43083    0.08672   4.968 6.83e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.283 on 15596 degrees of freedom
##   (1853 observations deleted due to missingness)
## Multiple R-squared:  0.00158,    Adjusted R-squared:  0.001516 
## F-statistic: 24.68 on 1 and 15596 DF,  p-value: 6.832e-07

\(\hat{trstep}= 4.94056+ 0.43083wrkprtyYes\)
So when Worked in political party or action group last 12 months=no, the mean of Trust in the European Parliament is 4.94056.
So when Worked in political party or action group last 12 months=Yes, the mean of Trust in the European Parliament is 4.94056+0.43083.

Task 2

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

bulgaria_data <- bulgaria_data %>%
  mutate(brncntr = case_when(
    brncntr == 1 ~ "Yes",
    brncntr == 2 ~ "No",
    brncntr %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(brncntr)
  ))
model <- lm(stfdem ~ brncntr, data = bulgaria_data)
summary(model)
## 
## Call:
## lm(formula = stfdem ~ brncntr, 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 ***
## brncntrYes    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
##   (904 observations deleted due to missingness)
## 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

Mean satisfaction with democracy is 2.8041 for people with brncntr= no. (not born in this country).

Task 3

library(tidyverse)
data <- ess %>%
  filter(cntry == "CH") 

data<- data %>%
  mutate(
    pdwrk = case_when(
      pdwrk == 1 ~ 'Yes',
      pdwrk == 0 ~ 'No', 
      TRUE ~ NA_character_ ),trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt))
model33<- lm(trstplt ~pdwrk, data = data)
summary(model33)
## 
## Call:
## lm(formula = trstplt ~ pdwrk, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1543 -1.0523 -0.0523  0.9477  4.9477 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.15428    0.02481 207.712   <2e-16 ***
## pdwrkYes    -0.10203    0.03172  -3.217   0.0013 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.98 on 16410 degrees of freedom
##   (513 observations deleted due to missingness)
## Multiple R-squared:  0.0006302,  Adjusted R-squared:  0.0005693 
## F-statistic: 10.35 on 1 and 16410 DF,  p-value: 0.001298
library(broom)
tidy(model33)
## # A tibble: 2 × 5
##   term        estimate std.error statistic p.value
##   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
## 1 (Intercept)    5.15     0.0248    208.   0      
## 2 pdwrkYes      -0.102    0.0317     -3.22 0.00130
#install.packages("remotes")
#remotes::install_github("datalorax/equatiomatic")
equatiomatic::extract_eq(model33, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplt}} = 5.15 - 0.1(\operatorname{pdwrk}_{\operatorname{Yes}}) \]

trstplt is trust in politicians and pdwrk is a categorical variable with 1 and 0 level.
when the work is not paid, the average trust in politicians is 5.15428. When the work is paid, the averagetrust in politicians is 0.1020268 less than when the work is not paid .