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.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"
library(fst)
ess <- read_fst("All-ESS-Data.fst")
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 <- belgium_data %>%
mutate(wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
))
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
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(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \] Interpretation: The y-intercept is 4.94; this represents the predicted value of the outcome variable (Trust in the European Parliament) when the explanatory variable (wrkprty) is at their reference level, in this case, “Yes.” The coefficient (0.43) represents the difference in the predicted value of trust in the European Parliament between the “No” category and the reference category “Yes” for the wrkprty variable. If a person has worked in a political party in the past year, a “Yes” (the reference category), their predicted average trust in politicians is 4.94. If a person has not worked in a political party in the past year, a “No,” their predicted average trust in parliament is 5.37 (given 4.94 + 0.43).
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))
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
model2 <- lm(stfdem ~ brncntr, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept) brncntr
## 3.0571188 -0.1339063
tidy(model2) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 3.057 | 0.175 | 17.496 | 0.000 |
| brncntr | -0.134 | 0.172 | -0.778 | 0.436 |
summary(model2)
##
## Call:
## lm(formula = stfdem ~ brncntr, data = bulgaria_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9232 -1.9232 0.0768 2.0768 7.2107
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0571 0.1747 17.496 <2e-16 ***
## brncntr -0.1339 0.1721 -0.778 0.436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.23 on 12336 degrees of freedom
## Multiple R-squared: 4.91e-05, Adjusted R-squared: -3.196e-05
## F-statistic: 0.6057 on 1 and 12336 DF, p-value: 0.4364
Interpretation: The expected average satisfaction with democracy for respondents not born in Bulgaria was 2.93, given by (3.06 - 0.13). The intercept (3.06) represents the predicted value of the outcome variable (stfdem) when the categorical variable (brncntr) is at the reference level (Yes, in this case). The expected average satisfaction with democracy for respondents born in Bulgaria was 3.06. The coefficient (-0.13) represents the difference in the predicted value of trust in politicians between the “No” category and the reference category (“Yes”) for the brncntr variable.
serbia_data <- ess %>%
filter(cntry == "RS") %>%
mutate(trstep = ifelse(trstun %in% c(77, 88, 99), NA, trstun),
)
unique(serbia_data$trstun)
## [1] 8 3 6 4 0 7 5 88 2 10 1 9 77 99
serbia_data <- serbia_data %>% filter(!is.na(trstun))
serbia_data <- serbia_data %>%
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)
))
model3 <- lm(trstun ~ polintr, data = serbia_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept) polintrNot Interested
## 7.873827 3.674246
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{trstun}} = 7.87 + 3.67(\operatorname{polintr}_{\operatorname{Not\ Interested}}) \] Interpretation: The y-intercept was found to be 7.87 which represents when the explanatory variable (polintr “Not Interested”) is equal to zero. The coefficient for (polintr “Not Interested”) is 3.67 which is the change in estimated value of Trust in the United Nations (trstun) for each unit change in (polintr “Not Interested”). The estimated average value of trstun for “Interested” is 7.87 the average for “Not Interested” is 11.54.