Install packages

packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "remotes")
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
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"        
## 
## [[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"

Import ESS data

ess <- read_fst("All-ESS-Data.fst")

Task 1

Filter and recode outcome of interest

belg_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep))
unique(belg_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
belg_data <- belg_data %>% filter(!is.na(trstep))

Create political covariate

belg_data <- belg_data %>%
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  )
  )

Linear modelling

model1 <- lm(trstep ~ wrkprty, data = belg_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338
tidy(model1) |>
  knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 4.941 0.019 263.892 0
wrkprtyYes 0.431 0.087 4.968 0
summary(model1)
## 
## Call:
## lm(formula = trstep ~ wrkprty, data = belg_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
##   (1332 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
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \]

Intercept of 4.94 indicates the predicted value of trstep in the case that wrkprty is “No.”

wrkprty of +0.43 indicates the difference between the predicted value of trstep between category 1 (Yes) and category 2 (No) of wrkprty. There is an increase of 0.43 when “Yes” is the response to wrkprty compared to when “No” is the response to wrkprty.

Conclusion: If a person has not worked in a political group or action group in the past 12 months, they have a predicted average trust of 4.94 in the European parliament (below average). But if they did work in a political group or action group in the past 12 months, their predicted average trust in the European parliament is then above average at (4.94 + 0.43 =) 5.37.

Task 2

Filter and recode outcome of interest

bulg_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem))
unique(bulg_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
bulg_data <- bulg_data %>% filter(!is.na(stfdem))

Recode predictor

bulg_data <- bulg_data %>%
  mutate(brncntr = case_when(
    brncntr == 1 ~ "Yes",
    brncntr == 2 ~ "No",
    brncntr %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(brncntr)
  )
  )

Linear modelling

model2 <- lm(stfdem ~ brncntr, data = bulg_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)  brncntrYes 
##   2.8041237   0.1189909

Produce model summary output

summary(model2)
## 
## Call:
## lm(formula = stfdem ~ brncntr, data = bulg_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
##   (2 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

Intercept of 2.8 indicates the predicted value of stfdem in the case that brncntr “No.”

brncntr of +0.12 indicates the difference between the predicted value of stfdem between category 1 (Yes) and category 2 (No) of brncntr. There is an increase of 0.12 when “Yes” is the response to brncntr compared to when “No” is the response to brncntr.

This means that the expected average satisfaction with democracy for respondents that were born in Bulgaria is (2.8 + 0.12) = 2.92. Still significantly below average, but not as dissatisfied as non-Bulgarian born respondents are likely to be.

Task 3

Filter and recode my potential country and outcome of interest (changed to fairelcc)

rus_data <- ess %>%
  filter(cntry == "RU") %>%
  mutate(fairelcc = ifelse(fairelcc %in% c(77, 88, 99), NA, fairelcc))
unique(rus_data$fairelcc)
##  [1] NA  3  5  2  8  1  7  0  6  9  4 10
rus_data <- rus_data %>% filter(!is.na(fairelcc))

Choose one of the predictors in the tutorial (pbldmn)

rus_data <- rus_data %>%
  mutate(pbldmn = case_when(
    pbldmn == 1 ~ "Yes",
    pbldmn == 2 ~ "No",
    pbldmn %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(pbldmn)
  )
  )

Linear modelling

model3 <- lm(fairelcc ~ pbldmn, data = rus_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept)   pbldmnYes 
##   4.3706356  -0.8448624
summary(model3)
## 
## Call:
## lm(formula = fairelcc ~ pbldmn, data = rus_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3706 -2.3706 -0.3706  1.6294  6.4742 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.37064    0.06036  72.407  < 2e-16 ***
## pbldmnYes   -0.84486    0.29590  -2.855  0.00434 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.853 on 2329 degrees of freedom
##   (18 observations deleted due to missingness)
## Multiple R-squared:  0.003488,   Adjusted R-squared:  0.00306 
## F-statistic: 8.152 on 1 and 2329 DF,  p-value: 0.004339

Producing an equatiomatic regression equation

equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{fairelcc}} = 4.37 - 0.84(\operatorname{pbldmn}_{\operatorname{Yes}}) \] Interpretation: When pbldmn (Taken part in lawful public demonstration last 12 months) is “No”, the predicted average outcome for fairelcc (In country national elections are free and fair) is 4.37. However, when pbldmn is “Yes”, the predicted average outcome decreases to (4.37 - 0.84 =) 3.53. This means that people who have not taken part in a lawful public demonstration within the last 12 months are more likely, on average, to believe that the statement “In country [Russia] national elections are free and fair” is true for Russia than those who have. However, both “Yes” and “No” responses fall below 5, so both parties, on average, believe that the statement is closer to not applying to [Russia] than applying to it (more false than true that elections are free and fair in Russia).

END