packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom") # 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.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"
setwd("/Users/luoyujian/Downloads")
library(fst)
ess <- read_fst("All-ESS-Data.fst")
#Task1:
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(wrkprty = ifelse(wrkprty %in% c(7, 8, 9), NA, wrkprty),
)
unique(belgium_data$wrkprty)
## [1] 2 1 NA
belgium_data <- belgium_data %>% filter(!is.na(trstplt))
belgium_data <- belgium_data %>%
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
mutate(
Trust_in_the_European_Parliament = recode(as.character(trstep),
'1' = "not_trust_at_all",
'2' = "not_trust",
'3' = "not_trust",
'4' = "not_trust",
'5' = "half_trust",
'6' = "trust",
'7' = "trust",
'8' = "trust",
'9' = "trust",
'10' = "complete_trust",
'77' = NA_character_,
'88' = NA_character_,
'99' = NA_character_),
Worked_in_political_party_or_action_group = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pbldmn)
)
)
table(belgium_data$wrkprty)
##
## 1 2
## 738 15354
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprty
## 5.6270440 0.9610318
#nterpretation:
#(Intercept) 7.55:
#The intercept represents the predicted value of the outcome variable (in this case, rust in the European Parliament, 0-10) when the explanatory variable is zero or, in the case of categorical variables, at their reference level. Put differently, when looking at the category that’s not “worked in a political party or action group last 12 months, the expected value of trstplt is approximately 7.55.
#wrkprtyYes Worked_in_political_party_or_action_group -0.9610318:
#This coefficient represents the difference in the predicted value of trust in politicians between the “Not_Worked_in_political_party_or_action_group” category and the reference category (“Worked_in_political_party_or_action_group”) for Trust_in_the_European_Parliament variable. Specifically, having “Worked_in_political_party_or_action_group” is associated with a decrease of approximately 0.961 in the predicted value of Trust in European Parliament, compared to not worked in political party or action group in the last 12 months.
#Task2:
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
)
unique(belgium_data$wrkprty)
## [1] 2 1 NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
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_),
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(bulgaria_data$native)
##
## No Yes
## 97 12239
table(bulgaria_data$geo)
##
## Peri-Urban Rural Urban
## 293 6879 5162
model2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
summary(model2)
##
## 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
## (2 observations deleted due to missingness)
## 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
#The expected average satisfaction is 2.80
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
)
unique(netherlands_data$trstlgl)
## [1] 8 5 4 3 6 7 1 0 2 10 9 NA
netherlands_data <- netherlands_data %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr)
),
rlgblg = ifelse(rlgblg %in% c(7, 8), NA, rlgblg),
religion_binary = ifelse(rlgblg == 1, "Yes", "No"),
minority = case_when(
blgetmg %in% c(7, 8, 9) ~ NA_character_,
blgetmg == 1 ~ "Ethnic Minority",
blgetmg == 2 ~ "Not Ethnic Minority",
TRUE ~ as.character(blgetmg)
),
religious = case_when(
rlgdgr %in% c(77, 88, 99) ~ NA_real_,
TRUE ~ rlgdgr
)
)
table(netherlands_data$gndr)
##
## Female Male
## 9931 8398
model3 <- lm(religious ~ blgetmg, data = netherlands_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept) blgetmg
## 6.239496 -0.811852
#nterpretation:
#(Intercept) 6.24:
#The intercept represents the predicted value of the outcome variable (in this case, minority) when the explanatory variable is zero or, in the case of categorical variables, at their reference level. Put differently, when looking at the category that’s not “minority", the expected value of trstplt is approximately 6.24
#wrkprtyYes Worked_in_political_party_or_action_group -0.812:
#This coefficient represents the difference in the predicted value of trust in politicians between the “not minority” category and the reference category (“Minority”) for religious. Specifically, having “minority” is associated with a decrease of approximately 0.812 in the predicted value of having religious or not, compared to not minority.