Homework 8

packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom") # add any you need here

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), ## Example of a trust variable
  )
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
table(belgium_data$trstep)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 1139  577  997 1419 1562 3850 2688 2759 1493  314  132
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)
  ))
unique(belgium_data$wrkprty)
## [1] "No"  "Yes" NA
table(belgium_data$wrkprty)
## 
##    No   Yes 
## 15354   738
model2 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338

Regression equation: y = 4.94 + 0.43x

Intercept: 4.94 The intercept value of 4.94 represents the predicted value of the outcome variable, which is trust in the European parliament (trstep) on a scale of 1-10, when the value of the explanatory variable is 0, meaning the individual did not work in a political party/action group in the last 12 months.

wrkprtyYes: 0.43 This coefficient value of 0.43 represents the difference in the predicted value of trust in the European parliament between the “wrkprtyYes” category and the reference category “wrkprtyNo” for the wrkprty variable. Specifically, having worked in a political party/ action group is associated with an increase of approximately 0.43 in the predicted value of trust in the European Parliament, compared to not working in a party.

Summary: If a person has not worked in a political/action party, their predicted average trust in the European Parliament is 4.94. If a person has worked in a political/action party in the last 12 months, their predicted average trust in the European Parliament is 5.37 (given 4.94 + 0.43).

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)
  ))
unique(bulgaria_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
unique(bulgaria_data$brncntr)
## [1] "Yes" "No"  NA
table(bulgaria_data$stfdem)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 2247 1589 1778 1969 1597 1742  640  400  206   86   84
table(bulgaria_data$brncntr)
## 
##    No   Yes 
##   104 13134
model3 <- lm(stfdem ~ brncntr, data = bulgaria_data)
summary(model3)
## 
## 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

The average satisfaction with democracy for those not born in Bulgaria is 2.80.

Task 3

uk_data <- ess %>%
  filter(cntry == "GB") %>%
  mutate(polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr), 
  )
uk_data <- uk_data %>%
  mutate(brncntr = case_when(
    brncntr == 1 ~ "Yes",
    brncntr == 2 ~ "No",
    brncntr %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(brncntr)
  ))
unique(uk_data$polintr)
## [1]  1  3  4  2 NA
unique(uk_data$brncntr)
## [1] "Yes" "No"  NA
table(uk_data$polintr)
## 
##    1    2    3    4 
## 2809 8701 5634 3819
table(uk_data$brncntr)
## 
##    No   Yes 
##  2409 18564
model4 <- lm(polintr ~ brncntr, data = uk_data)
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(model4, use_coefs = TRUE)

\[ \operatorname{\widehat{polintr}} = 2.49 + 0.02(\operatorname{brncntr}_{\operatorname{Yes}}) \]

The average of interest in politics for those not born in the UK is 2.49, because 2.49 is the intercept. For those born in the UK, the value increases by 0.02.