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.
# 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.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ 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.
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))
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(wrkprty= ifelse(wrkprty %in% c(7, 8, 9), NA, wrkprty),
)
belgium_data <- belgium_data %>%
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
mutate(polintr = case_when(
polintr %in% c(1, 2) ~ "Interested",
polintr %in% c(3, 4) ~ "Not Interested",
polintr %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(polintr)
)) %>%
# Recode work for party
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
)) %>%
# Recode worked for civil org/association
mutate(workcivil = case_when(
wrkorg == 1 ~ "Yes",
wrkorg == 2 ~ "No",
wrkorg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkorg)
)) %>%
# Recode participation in protest
mutate(protest = case_when(
pbldmn == 1 ~ "Yes",
pbldmn == 2 ~ "No",
pbldmn %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pbldmn)
))
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
Regression equation: trstep(hat) = 7.5491077 - 0.9610318 (wrkprtyYes) + error term
Interpretation: The intercept is 7.5491077, therefore the expected value of trust in the parliament (trststep) is approximately 7.55 when looking at this category. Our coefficient is -0.9610318, therefore having worked in a political party (wrkprtyYes) is associated with a decrease of approximately -0.96 in the predicted value of trust in the parliament, in comparison to having not worked in a party (No).
This suggests a negative correlation between trust in the European parliament and whether or not one has worked for a political party in Belgium. So, if a person has not worked in a political party their predicted average trust in the European parliament is 7.55. If a person has worked in a political party then their predicted average trust in the European parliament is 6.59 (given 7.55 - 0.96).
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(
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_),
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(bulgaria_data$native)
##
## No Yes
## 104 13134
table(bulgaria_data$geo)
##
## Peri-Urban Rural Urban
## 326 7530 5379
model1 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(model1)
## # 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
Regression equation: stfdem(hat) = 2.8 + 0.12 (nativeYes)
Interpretation: The intercept is 2.8041237 and the coefficient is 0.1189909, so if a respondent was born in the country then their expected satisfaction with democracy is approximately 2.80. Therefore, the expected average satisfaction with democracy for respondents who were not born in Bulgaria is approximately 2.92 (given 2.80 + 0.12). This suggests a positive relationship between satisfaction with democracy and if a person was born in the country of Bulgaria.
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.
greece_data <- ess %>%
filter(cntry == "GR") %>%
mutate(trstsci = ifelse(trstsci %in% c(77, 88, 99), NA, trstsci),
)
greece_data <- greece_data %>%
mutate(
rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
religion_binary = ifelse(rlgblg == 1, "Yes", "No"),
)
table(greece_data$religion_binary)
##
## No Yes
## 960 11569
model1 <- lm(trstsci ~ religion_binary, data = greece_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) religion_binaryYes
## 7.39662447 -0.07367495
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstsci}} = 7.4 - 0.07(\operatorname{religion\_binary}_{\operatorname{Yes}}) \]
Regression equation: trstsci(hat) = 7.4 - 0.07 (religion_binaryYes)
Interpretation: Our intercept is 7.39662447, the expected value of trust in scientists (trstsci) is then approximately 7.4. Our coefficient is -0.07367495, belonging to a religion (religion_binaryYes) is then associated with a decrease of approximately -0.07 in the predicted value of trust in scientists, compared to not belonging to a religion (No).
Therefore, the regression equation suggests a negative correlation between trust in scientists and belonging to a religion. It tells us that the expected average trust in scientists for respondents who are not religious in Greece is approximately 7.33 (given 7.4 - 0.07). As well as that the expected average trust in scientists for respondents who are religious (religion_binaryYes) in Greece is approximately 7.4.