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. ## Setting up environment
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.3 ✔ 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. ##Outcome:
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstplt = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
)
unique(belgium_data$trstpep)
## NULL
belgium_data <- belgium_data %>% filter(!is.na(trstep))
##Explanatory
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
##Regression
model_BE <- lm(trstep ~ wrkprty, data = belgium_data)
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)
coefficients <- coef(model_BE)
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 European Parliament, 0-10) when the explanatory variable (political party) 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, does not work in a political part), the expected value of trstep is approximately 7.55.
wrkprtyYes -0.96:
This coefficient represents the difference in the predicted value of trust in European Parliament between the “work in a political party (wrkprtyYes)” category and the reference category (“does not work in a political party (wrkprtyNo)”) for the ‘wrkprty’ variable.
Specifically, having “work in a political party (wrkprtyYes)” is associated with a decrease of approximately 0.96 in the predicted value of trust in European Parliament, compared to having a “wrkprtyNo”.
In summary:
If a person does not work in a political party (wrkprtyNo), their predicted average trust in work in European Parliament is 7.55. If a person work in a political party (wrkprtyYes), their predicted average trust in European Parliament is 6.59 (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? ##Outcome:
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))
##Explanatory
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
##Regression
model_BG <- lm(stfdem ~ native, data = bulgaria_data)
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)
coefficients <- coef(model_BG)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(model_BG)
## # 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(model_BG) |>
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 |
The expected average satisfaction with democracy for respondents that were not born in Bulgaria (reference category) will be 2.804 which is the intercept estimate.
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. ##Outcome:
uk_data <- ess %>%
filter(cntry == "GB") %>%
mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
)
unique(uk_data$trstplt)
## [1] 7 6 1 4 3 2 0 5 8 10 9 NA
uk_data <- uk_data %>% filter(!is.na(trstplt))
##Explanatory
uk_data <- uk_data %>%
mutate( gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr)
))
table(uk_data$gndr)
##
## Female Male
## 11345 9317
model_uk <- lm(trstplt ~ gndr, data = uk_data)
# model 1 refers to the name where you will store the output
# then it goes lm(outcome ~ explanatory, data = dataset_name)
coefficients <- coef(model_uk)
print(coefficients)
## (Intercept) gndrMale
## 3.511591009 -0.006063479
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(model_uk, use_coefs = TRUE)
\[ \operatorname{\widehat{trstplt}} = 3.51 - 0.01(\operatorname{gndr}_{\operatorname{Male}}) \] Interpretation: trstplt(hat) = 3.51 - 0.01 (Male) + error term intercept = 3.51 The intercept term (3.51) represents the predicted amount of trust in politicians when all other variables in the model are zero. In this context, it means the predicted trust in politicians when the individual is not identified as male (thus, female) gndrMale = -0.01 This indicates the expected change in the amount of trust in politicians for each one-unit increase in the “Male” variable. Since “Male” is binary, this suggests that being male is associated with a decrease of 0.01 units in the predicted trust in politicians compared to being not male (thus, female).
summary: this regression equation suggests that the baseline trust in politicians for female is 3.51. Being identified as male is associated with a decrease of 0.01 units in the predicted trust in politicians. (3.51-0.01 = 3.5) ## End