# 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")
#Task 1
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"
model_1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model_1)
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(model_1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \]
#The intercept is at a score of 4.94, which suggests that the expected value for Trust in the European Parliament (trstep) is approximately 4.94 for the group of individuals that did not work in a political party (wrkprty). The correlation coefficient of 0.43 indicates that the difference between those who have and have not worked in a political party is 0.43 for the predicted value of Trust in the European Parliament. This means that if a person did work for a political party, their predicted average score for trstep is 5.37 (4.94 + 0.43), while those who did not work in a political party have a predicted average score of 4.94 for trstep.
#Task 2
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_2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model_2)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(model_2)
## # 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_2) %>%
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 |
summary(model_2)
##
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9231 -1.9231 0.0769 2.0769 7.1959
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.8041 0.2265 12.382 <2e-16 ***
## nativeYes 0.1190 0.2274 0.523 0.601
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.231 on 12334 degrees of freedom
## Multiple R-squared: 2.22e-05, Adjusted R-squared: -5.887e-05
## F-statistic: 0.2739 on 1 and 12334 DF, p-value: 0.6007
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_2, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \] #The intercept of 2.8 represents the expected average satisfaction with democracy for respondents not born in Bulgaria.
#Task 3
#I used Germany as my country of interest and the outcome of interest was "interest in politics" (polintr). The predictor was education level (BA or No BA).
germany_data <- ess %>%
filter(cntry == "DE") %>%
mutate(polintr = ifelse(polintr %in% c(7,8,9), NA, polintr))
unique(germany_data$polintr)
## [1] 4 1 3 2 NA
germany_data <- germany_data %>% filter(!is.na(polintr))
unique(germany_data$polintr)
## [1] 4 1 3 2
germany_data <- germany_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlbb = 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(germany_data$educ_level)
##
## BA No BA
## 8843 25516
unique(germany_data$educ_level)
## [1] "No BA" "BA"
model_3 <- lm(polintr ~ educ_level, data = germany_data)
coefficients <- coef(model_3)
print(coefficients)
## (Intercept) educ_levelNo BA
## 1.9176750 0.4292446
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_3, use_coefs = TRUE)
\[ \operatorname{\widehat{polintr}} = 1.92 + 0.43(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \] #The intercept at 1.92 represents the expected average value of the interest in politics (polintr) for those who have a BA degree; the correlation coefficient of 0.43 indicates the difference in the average value of the interest in politics for those who have a BA degree and those who do not have a BA degree. If a person holds a BA degree, their expected value of being interested in politics is 1.92, while a person who does not hold a BA degree has an expected value of 2.35.