# 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")

Homework 8 (2.5%) due Nov. 20

Submit on Quercus using assignment tab – not on the discussion board. Make sure to provide both your markdown file and R pubs link (submit as comment if you cannot submit both files at once). If you do not submit both, you will be penalized 1 pt. out of the 2.5 total.

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)
  ))

table(belgium_data$wrkprty)
## 
##    No   Yes 
## 15354   738
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
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338

The intercept, 4.94, is the predicted value of trust in the European parliament when worked for party is at “No”, the reference level.

The coefficient wrkprtyYes, 0.43, is the difference in value between wrkprty “Yes” and wrkprty “No” with regards to trstep. Consenquently, this means that for workers of a party, they are 0.43 higher than those who haven’t worked for a party with regards to trust in the European parliament.

If wrkprtyYes, intercept = 4.94 + 0.43 If wrkprtyNo, intercept = 4.94

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_)
  )

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$native)
## 
##    No   Yes 
##   104 13134
model2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)   nativeYes 
##   2.8041237   0.1189909
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
tidy(model2) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 2.804 0.226 12.382 0.000
nativeYes 0.119 0.227 0.523 0.601
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 stfdem of respondents not born in Bulgaria is 2.8041237. The P-value is at 0, thus the observed differences are not a luck-based occurence but rather signal a connection between the two.

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.

finland_data <- ess %>%
  filter(cntry == "FI") %>%
  mutate(
    rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
    religion_binary = ifelse(rlgblg == 1, "Yes", "No"),
  )
finland_data <- finland_data %>%
  mutate(
    atchctr = case_when(
    atchctr %in% c(77, 88, 99) ~ NA_real_,
    TRUE ~ atchctr
  ))

table(finland_data$religion_binary)
## 
##    No   Yes 
##  7377 10113
table(finland_data$atchctr)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##    9   10   31   55   45  134  170  492 1109 1467 1721
model3 <- lm(atchctr ~ religion_binary, data = finland_data)
coefficients <- coef(model3)
print(coefficients)
##        (Intercept) religion_binaryYes 
##          8.2100910          0.6379743
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(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{atchctr}} = 8.21 + 0.64(\operatorname{religion\_binary}_{\operatorname{Yes}}) \] the intercept of atchctr, attachment to the country of Finland, is at the predicted value of 8.21 if one is not religious. This value rises by 0.64 if one is religious. We can draw from this observation and create a theory describing how religious tolerance in Finnish society creates strong ties between religious communities and the state.