Homework 8

# 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
## [[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

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), 
  )
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
belgium_data <- belgium_data %>% filter(!is.na(trstep))

unique(belgium_data$wrkprty)
## [1]  2  1  9  7  8 NA
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  
  # Recode wrkprty
 mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  )) 
  
table(belgium_data$wrkprty)
## 
##    No   Yes 
## 15354   738
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   7.5491077  -0.9610318

Interpretation:

(Intercept) 7.55:

The intercept represents the predicted value of the outcome variable (in this case, Trust in the European Parliament, 0-10) when the explanatory variable is zero or, in the case of categorical variables, at their reference level.

Put differently, when looking at the category that’s NOT “Yes” in wrkprty (thus, holding a “No”), the expected value of trstep is approximately 7.55.

wrkprty Yes -0.96:

This coefficient represents the difference in the predicted value of trust in politicians between the “Yes” category and the reference category (“No”) for the wrkprty variable.

Specifically, having “Yes” is associated with a decrease of approximately 0.96 in the predicted value of trust in the European Parliament, compared to having a “No”.

In summary:

If a person has a “No” (assuming that’s the reference category), their predicted average trust in politicians is 7.55. If a person does not have a “Yes”, their predicted average trust in the European Parliament is 7.55 (given 7.55 - 0.96)

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), 
  )
unique(bulgaria_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))

unique(bulgaria_data$brncntr)
## [1] 1 2 7
bulgaria_data <- bulgaria_data %>% filter(!is.na(brncntr))
bulgaria_data <- bulgaria_data %>%
  mutate(
    native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )
table(bulgaria_data$native)
## 
##    No   Yes 
##    97 12239
model2 <- lm(stfdem ~ brncntr, data = bulgaria_data)
summary(model2)
## 
## Call:
## lm(formula = stfdem ~ brncntr, data = bulgaria_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9232 -1.9232  0.0768  2.0768  7.2107 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.0571     0.1747  17.496   <2e-16 ***
## brncntr      -0.1339     0.1721  -0.778    0.436    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.23 on 12336 degrees of freedom
## Multiple R-squared:  4.91e-05,   Adjusted R-squared:  -3.196e-05 
## F-statistic: 0.6057 on 1 and 12336 DF,  p-value: 0.4364

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.

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}} = 7.55 - 0.96(\operatorname{wrkprty}_{\operatorname{Yes}}) \]