packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom","remotes") # add any you need here
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
## Warning: package 'remotes' was built under R version 4.3.2
## [[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"
##
## [[6]]
## [1] "remotes" "broom" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "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.
# cleaning
tsk1data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep))
unique(tsk1data$trstep)
## [1] 0 7 8 5 6 NA 4 9 3 1 10 2
tsk1data <- tsk1data %>% filter(!is.na(trstep))
tsk1data <- tsk1data %>%
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
))
table(tsk1data$wrkprty)
##
## No Yes
## 14871 727
unique(tsk1data$wrkprty)
## [1] "No" "Yes" NA
tsk1data <- tsk1data %>% filter(!is.na(wrkprty))
unique(tsk1data$wrkprty)
## [1] "No" "Yes"
tsk1mdl <- lm(trstep ~ wrkprty, data = tsk1data)
coefficients <- coef(tsk1mdl)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
Interpretation:
(Intercept) 4.94: This means that the value of not having worked in a political party and trusting in the European Union is ~4.94, which is close to 5, the median value between no trust at all and complete trust
wrkprtyYes 0.43: Having worked in a political party is associated with an increase of ~0.43 in the predicted value of trust in the european union, compared to having a not worked in a political party.
In summary: if you’ve worked in a political party, you’re more likely to trust the european union
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?
tsk2data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem))
unique(tsk2data$stfdem)
## [1] 0 1 NA 2 3 5 4 6 8 7 10 9
tsk2data <- tsk2data %>% filter(!is.na(stfdem))
unique(tsk2data$stfdem)
## [1] 0 1 2 3 5 4 6 8 7 10 9
tsk2data <- tsk2data %>%
mutate(native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(tsk2data$native)
##
## No Yes
## 97 12239
unique(tsk2data$native)
## [1] "Yes" "No" NA
tsk2data <- tsk2data %>% filter(!is.na(native))
unique(tsk2data$native)
## [1] "Yes" "No"
Model summary
tsk2mod1 <- lm(stfdem ~ native, data = tsk2data)
coefficients2 <- coef(tsk2mod1)
print(coefficients2)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(tsk2mod1)
## # 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
The expected average value for satisfaction with democracy of people not born in Bulgaria is ~2.804, as noted by the intercept. For people born in Bulgaria, it is ~0.119 higher, so around 2.923. Using the markers from ESS, both are are dissatsified.
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.
Research Question: How does the state of the economy in Bulgaria affect the levels of happiness? happy - how happy are you https://ess-search.nsd.no/en/variable/a5c95af9-6037-4e52-9156-2ceac708eb3f
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy))
unique(bulgaria_data$happy)
## [1] 6 0 10 5 7 9 8 4 1 3 2 NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(happy))
bulgaria_data <- bulgaria_data %>% filter(!is.na(happy))
unique(bulgaria_data$happy)
## [1] 6 0 10 5 7 9 8 4 1 3 2
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_))
table(bulgaria_data$geo)
##
## Peri-Urban Rural Urban
## 322 7447 5320
unique(bulgaria_data$geo)
## [1] "Rural" "Urban" "Peri-Urban" NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(geo))
unique(bulgaria_data$geo)
## [1] "Rural" "Urban" "Peri-Urban"
Model summary
bulgariamodel <- lm(happy ~ geo, data = bulgaria_data)
coefficients3 <- coef(bulgariamodel)
print(coefficients3)
## (Intercept) geoRural geoUrban
## 5.36024845 -0.04428229 0.51042824
This is a bit hard to interpret, as its a multinomial variable. However; we believe the intercept (~5.36), here, represents the expected value of being happy in a peri-urban location. People in rural areas’ expected value is 0.044 less than the intercept, while rural areas expected value is 0.51 more than the intercept.
tidy(bulgariamodel)
## # A tibble: 3 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 5.36 0.143 37.5 1.76e-292
## 2 geoRural -0.0443 0.146 -0.303 7.62e- 1
## 3 geoUrban 0.510 0.147 3.47 5.26e- 4
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(bulgariamodel, use_coefs = TRUE)
\[ \operatorname{\widehat{happy}} = 5.36 - 0.04(\operatorname{geo}_{\operatorname{Rural}}) + 0.51(\operatorname{geo}_{\operatorname{Urban}}) \] y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon happy(hat) = 5.36 - 0.04 (Rural) + 0.51 (Urban) + error term