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)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── 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.4 ✔ 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
## Warning: package 'infer' was built under R version 4.3.2
## Warning: package 'fst' was built under R version 4.3.2
## Warning: package 'modelsummary' was built under R version 4.3.2
## Warning: package 'broom' 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"
ess <- read.fst("All-ESS-Data.fst")
## Warning: package 'fstcore' was built under R version 4.3.2
Task 1
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77,88,99), NA, trstep))
belgium_data <- belgium_data %>%
mutate(wrkprty = ifelse(wrkprty %in% c(7,8,9), NA, wrkprty))
table(belgium_data$wrkprty)
##
## 1 2
## 738 15354
table(belgium_data$trstep)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1139 577 997 1419 1562 3850 2688 2759 1493 314 132
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprty
## 5.8022231 -0.4308338
summary(model1)
##
## Call:
## lm(formula = trstep ~ wrkprty, data = belgium_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3714 -0.9406 0.0594 2.0594 5.0594
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.80222 0.17038 34.054 < 2e-16 ***
## wrkprty -0.43083 0.08672 -4.968 6.83e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.283 on 15596 degrees of freedom
## (1853 observations deleted due to missingness)
## Multiple R-squared: 0.00158, Adjusted R-squared: 0.001516
## F-statistic: 24.68 on 1 and 15596 DF, p-value: 6.832e-07
Regression equation: trstep(hat) = 5.80 - 0.43(wrkprty)
This equation means that if someone in Belgium works in a Political partry, the avegerage trust they have in European parliament is 5.80. As people in Belgium do not work in a political party, the chance, their predicted average is 5.37 (5.80 - 0.43).
Task 2
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),)
bulgaria_data <- bulgaria_data %>%
mutate(brncntr = ifelse(brncntr %in% c(7, 8, 9), NA, brncntr),)
table(bulgaria_data$brncntr)
##
## 1 2
## 13134 104
model2 <- lm(stfdem ~ brncntr, data = bulgaria_data)
summary(model2)
##
## Call:
## lm(formula = stfdem ~ brncntr, 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) 3.0421 0.2300 13.224 <2e-16 ***
## brncntr -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
## (904 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 with democracy from people not born in Bulgaria in Bulgaria is 2.92 (3.04 - 0.12).
Task 3
switzerland_data <- ess %>%
filter(cntry == "CH") %>%
mutate(trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl))
table(switzerland_data$trstprl)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 273 149 404 936 1254 3423 2488 3352 2710 690 398
switzerland_data <- switzerland_data %>%
mutate(rlgblg = ifelse(rlgblg %in% c (7, 8), NA, rlgblg))
table(switzerland_data$rlgblg)
##
## 1 2
## 10855 6008
model3 <- lm(trstprl ~ rlgblg, data = switzerland_data)
summary(model3)
##
## Call:
## lm(formula = trstprl ~ rlgblg, data = switzerland_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.066 -1.066 -0.066 1.172 4.172
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.30354 0.04712 133.774 < 2e-16 ***
## rlgblg -0.23759 0.03271 -7.263 3.97e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.985 on 16017 degrees of freedom
## (906 observations deleted due to missingness)
## Multiple R-squared: 0.003282, Adjusted R-squared: 0.00322
## F-statistic: 52.75 on 1 and 16017 DF, p-value: 3.971e-13
#quatiomatic::extract_eq(model3, use_coefs = TRUE)
In Switzerland, the average trust in parliament, if a respondant belongs to a religious group is 6.03, and this average decreases by 0.24 if the participant does not belong to a religious group. If a preson does not belong to a religious group, their average support in parliament will be 5.79.