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.
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom")
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"
ess <- read_fst("All-ESS-Data.fst")
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.
table(ess$trstep)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 46287 23673 35107 45532 47200 91420 52520 47268 31121 10370 7761 860 50411
## 99
## 1025
table(ess$wrkprty)
##
## 1 2 7 8 9
## 17531 411791 315 805 428
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(
wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
),
trstep = case_when(
trstep %in% c(77, 88, 99) ~ NA_character_,
TRUE ~ as.character(trstep)
)
)
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \] y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon
trstep(hat) = 4.94 + 0.43 (wrkprtyYes) + error term
Interpretation: - If a person has not worked in a political party (No), their predicted average trust in the European Parliament is 4.94 - If a person has worked in a political party (Yes), their predicted average trust in the European Parliament is 5.37 (given 4.94 + 0.43)
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?
table(ess$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 29121 18116 31973 45014 46758 80154 55790 67816 59837 23346 12989 755 18211
## 99
## 675
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_),
stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem)
)
table(bulgaria_data$native)
##
## No Yes
## 104 13134
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
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
Answer: The expected average satisfaction with democracy for respondents that were not born in Bulgaria is 2.80
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.
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(
ipsuces = ifelse(ipsuces %in% c(7, 8, 9), NA, ipsuces),
rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
religion_binary = ifelse(rlgblg == 1, "Yes", "No")
)
table(netherlands_data$ipsuces)
##
## 1 2 3 4 5 6
## 860 4063 5689 4180 2422 751
table(netherlands_data$religion_binary)
##
## No Yes
## 11289 7008
model3 <- lm(ipsuces ~ religion_binary, data = netherlands_data)
equatiomatic::extract_eq(model3, use_coefs = TRUE)
\[ \operatorname{\widehat{ipsuces}} = 3.25 + 0.16(\operatorname{religion\_binary}_{\operatorname{Yes}}) \] Equation: ipsuces(hat) = 3.25 + 0.16 (religion_binaryYes) + error term
Interpretation: - If a person does not belong to a religion (No), we predict that on average, their value of personal success and recognition of personal achievements by others is 3.25. - If a person does belong to a religion (Yes), we predict that on average, their value of personal success and recognition of personal achievements by others is 3.41 (given 3.25 + 0.16).