# 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.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")
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))
unique(belgium_data$trstep)
## [1] 0 7 8 5 6 4 9 3 1 10 2
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
## 14871 727
unique(belgium_data$wrkprty)
## [1] "No" "Yes" NA
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
unique(belgium_data$wrkprty)
## [1] "No" "Yes"
#Linear regression
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
trstep(hat) = 4.94 + 0.43 (worked in a political party) + error term
The intercept is at the score of 4.94. It suggests the expected value for trust in the European Parliament is approximately 4.94 for the category that people did not work in a political party. The correlation coefficient of 0.43 implies that the difference between people who have worked in a political party and people who have not worked in a political party is approximately a score of 0.43 for the predicted value of trust in the European Parliament. In other words, given that a person did not work in a political party, their predicted average score for trust in the European Parliament is 4.94, whereas given that a person did work in a political party, their predicted average score for trust in the European Parliament is 5.37.
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))
unique(bulgaria_data$stfdem)
## [1] 0 1 2 3 5 4 6 8 7 10 9
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
unique(bulgaria_data$native)
## [1] "Yes" "No" NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(native))
unique(bulgaria_data$native)
## [1] "Yes" "No"
#Model summary
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
The intercept at 2.80 indicates that the expected average satisfaction with democracy is 2.80 for respondents who were not born in Bulgaria.
finland_data <- ess %>%
filter(cntry == "FI") %>%
mutate(fltdpr = ifelse(fltdpr %in% c(7, 8, 9), NA, fltdpr))
unique(finland_data$fltdpr)
## [1] NA 1 2 3 4
finland_data <- finland_data %>% filter(!is.na(fltdpr))
unique(finland_data$fltdpr)
## [1] 1 2 3 4
finland_data <- finland_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(finland_data$educ_level)
##
## BA No BA
## 1601 4566
unique(finland_data$educ_level)
## [1] "No BA" "BA"
finland_data <- finland_data %>% filter(!is.na(educ_level))
unique(finland_data$educ_level)
## [1] "No BA" "BA"
#Linear regression
model3 <- lm(fltdpr ~ educ_level, data = finland_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept) educ_levelNo BA
## 1.1817614 0.0464471
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{fltdpr}} = 1.18 + 0.05(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \]
The intercept at the score of 1.18 suggests that the expected average value for feeling depressed in the past week is 1.18 for the category of those who have a BA degree. The correlation coefficient of 0.05 indicates the magnitude of the difference in the average value for feeling depressed in the past week between those who have a BA degree and those who do not have a BA degree. Hence, considering that a person has a BA degree, their predicted value for feeling depressed in the past week is 1.18. On the other hand, considering that a person does not have a BA degree, their predicted value for feeling depressed in the past week is 1.23.