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

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the 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
## [[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"
# for writing an equation output with equatiomatic
options(repos = c(CRAN = "https://cran.r-project.org"))

install.packages("remotes")
## Installing package into 'C:/Users/for_m/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'remotes' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\for_m\AppData\Local\Temp\RtmpeiTVGQ\downloaded_packages
library(remotes)
## Warning: package 'remotes' was built under R version 4.3.2
remotes::install_github("datalorax/equatiomatic")
## Skipping install of 'equatiomatic' from a github remote, the SHA1 (39e8c3ea) has not changed since last install.
##   Use `force = TRUE` to force installation
ess <- read_fst("All-ESS-Data.fst")

Task 1

Extract the coefficients and intercept using trust in the European Parliament (as the outcome) and worked in a political party (wrkprty, as the explanatory), for the country of Belgium. Write the regression equation and interpret.

belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep)
  )
  
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)
  ))
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
# extract the coefficients and intercept
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338

The intercept, 4.94, represents the predicted value of trust in the European parliament (the outcome variable) when the explanatory variable is at their reference level, which is “No” for worked for a party.

The number 0.43, represents the difference in the predicted value of trust in the European parliament between the “Yes” category for “worked for a party” and the reference category (“NO”) for the “worked for a party” variable. This means that the workers of a party are 0.43 higher on the 0-10 scale for the variable “trust in the European parliament” than those who haven’t worked for a party.

In summary: If a person haven’t worked for a party, their predicted average trust in the European parliament is 4.94. If a person has worked for a party, their predicted average trust in the European parliament is 5.37 (given 4.9405554 + 0.4308338 = 5.3713892).

The regression equation

equatiomatic::extract_eq(model1, use_coefs = TRUE)

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

Task 2

Produce a model summary output using either tidy() or summary() (your choice). Use stfdem (satisfaction with democracy) as the outcome and born in country as a predictor (which we recoded/renamed as ‘native’ in the tutorial), for the country of Bulgaria. What is the expected average satisfaction with democracy for respondents that were not born in Bulgaria?

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

bulgaria_data <- bulgaria_data %>%
  mutate(
    native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )
model2 <- lm(stfdem ~ native, data = bulgaria_data)
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 nativeYes      0.119     0.227     0.523 6.01e- 1

The expected average satisfaction with democracy for respondents that were not born in Bulgaria is 2.80 (the intercept).

Task 3

Based on your own outcome and country of interest, as well as one of the predictors recoded in the tutorial, produce an equatiomatic regression equation. Interpret the regression equation.

#recde the predictor
germany_data <- ess %>%
  filter(cntry == "DE") %>%
  mutate(
  geo = recode(as.character(domicil), 
                 '1' = "Urban", 
                 '2' = "Peri-Urban", 
                 '3' = "Rural", 
                 '4' = "Rural", 
                 '5' = "Rural",
                 '7' = NA_character_,
                 '8' = NA_character_,
                 '9' = NA_character_)
  )

#recode the outcome variable
germany_data <- germany_data %>%
  mutate(
    stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco)
  )
model3 <- lm(stfeco ~ geo, data = germany_data)
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{stfeco}} = 5.31 - 0.19(\operatorname{geo}_{\operatorname{Rural}}) - 0.12(\operatorname{geo}_{\operatorname{Urban}}) \]

Interpretation: the intercept of stfeco, satisfaction with the current economy in Germany, is at the predicted value of 5.31 if one lives in a peri-urban area. This value decreases to 5.12 if one lives in a rural area (this comes from 5.31-0.19). If one lives in an urban area, their satisfaction with the current economy is 5.19 (this comes from 5.31-0.12).