# 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 = case_when(
      trstep %in% c(77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(trstep)),
    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, belgium_data)
coefficients1 <- coef(model1)
print(coefficients1)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338
equatiomatic::extract_eq(model1, use_coefs = TRUE)

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

Regression equation: trstep = 4.9405554 + 0.4308338 x_wrkprty

Interpretation:

The intercept of 4.94405554 represents the expected value of the variable, trstep (Trust in politicians) given that the explanatory variable is zero, or when the variable wrkprty (Worked in a political party) is “No”.

The slope of 0.4308338 represents the change in the predicted value for trstep given a one unit increase in wrkprty, which in this case, is if the person has worked in a political party.

As a result, we can say that the difference in trust in politicians given whether or not someone has worked at a political party is 0.4308338. If a person has not worked in a political party, the prediccted trust in politicians is 4.94, while if they have worked in a political party, the average trust is 4.94 + 0.43, or 5.37488934.

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 = case_when(
      stfdem %in% c(77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(stfdem)),
    native = recode(brncntr,
                    `1` = "Yes",
                    `2` = "No",
                    `7` = NA_character_,
                    `8` = NA_character_,
                    `9` = NA_character_)
  )

model2 <- lm(stfdem ~ native, bulgaria_data)
coefficients2 <- coef(model2)
print(coefficients2)
## (Intercept)   nativeYes 
##   2.8041237   0.1189909
summary(model2)
## 
## Call:
## lm(formula = stfdem ~ native, 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 ***
## nativeYes     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 expected average satisfaction in democracy for people not born in Bulgaria is 2.8041.

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.

My country of interest is Sweden, and I’m interested in seeing how satisfied people are with the national government (stfgov) given whether or not they are an ethnic minority (minority).

sweden_data <- ess %>% 
  filter(cntry == "SE")

sweden_df <- sweden_data %>% 
  mutate(
    minority = case_when(
      blgetmg %in% c(7, 8, 9) ~ NA_character_,
      blgetmg == 1 ~ "Ethnic Minority",
      blgetmg == 2 ~ "Not Ethnic Minority",
      TRUE ~ as.character(blgetmg)),
    stfgov = case_when(
      stfgov %in% c(77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(stfgov))
  )

model3 <- lm(stfgov ~ minority, sweden_df)
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{stfgov}} = 5.23 - 0.04(\operatorname{minority}_{\operatorname{Not\ Ethnic\ Minority}}) \] Interpretation:

The intercept of 5.23 represents the expected value of the variable, stfgov (Satisfaction with the government) given that the explanatory variable is zero, or when the variable minority is “Ethnic Minority”.

The slope of -0.04 represents the change in the predicted value for stfgov given a one unit increase in minority, which in this case, is if the person is not an ethnic minority.

As a result, we can say that the difference in satisfaction in the national government given whether or not someone is an ethnic minority is -0.04. If a person is an ethnic minority, their satisfaction with the government is 5.23, while if they have worked in a political party, the average trust is 5.23 -0.04 , or 5.19.