rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 523827 28.0 1161934 62.1 NA 669428 35.8
## Vcells 964865 7.4 8388608 64.0 16384 1851710 14.2
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "knitr", "remotes") # 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"
##
## [[6]]
## [1] "knitr" "broom" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "remotes" "knitr" "broom" "modelsummary" "fst"
## [6] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "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),
wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
))
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
Belgium_data <- Belgium_data %>% filter(!is.na (wrkprty))
Belgium_data <- Belgium_data %>% filter(!is.na (trstep))
unique(Belgium_data$wrkprty)
## [1] "No" "Yes"
table(Belgium_data$wrkprty)
##
## No Yes
## 14871 727
unique(Belgium_data$trstep)
## [1] 0 7 8 5 6 4 9 3 1 10 2
table(Belgium_data$trstep)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1034 512 917 1303 1442 3589 2498 2528 1373 280 122
model1 <- lm(trstep ~ wrkprty, data = Belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 4.94 0.0187 264. 0
## 2 wrkprtyYes 0.431 0.0867 4.97 0.000000683
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 4.941 | 0.019 | 263.892 | 0 |
| wrkprtyYes | 0.431 | 0.087 | 4.968 | 0 |
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) 4.94056 0.01872 263.892 < 2e-16 ***
## wrkprtyYes 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
## Multiple R-squared: 0.00158, Adjusted R-squared: 0.001516
## F-statistic: 24.68 on 1 and 15596 DF, p-value: 6.832e-07
remotes::install_github("datalorax/equatiomatic")
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## xfun (0.40 -> 0.41 ) [CRAN]
## rlang (1.1.1 -> 1.1.2 ) [CRAN]
## htmltools (0.5.6 -> 0.5.7 ) [CRAN]
## parallelly (NA -> 1.36.0 ) [CRAN]
## listenv (NA -> 0.9.0 ) [CRAN]
## vctrs (0.6.3 -> 0.6.4 ) [CRAN]
## lifecycle (1.0.3 -> 1.0.4 ) [CRAN]
## globals (NA -> 0.16.2 ) [CRAN]
## future (NA -> 1.33.0 ) [CRAN]
## utf8 (1.2.3 -> 1.2.4 ) [CRAN]
## withr (2.5.0 -> 2.5.2 ) [CRAN]
## fansi (1.0.4 -> 1.0.5 ) [CRAN]
## stringi (1.7.12 -> 1.8.1 ) [CRAN]
## stringr (1.5.0 -> 1.5.1 ) [CRAN]
## dplyr (1.1.3 -> 1.1.4 ) [CRAN]
## evaluate (0.21 -> 0.23 ) [CRAN]
## httpuv (1.6.11 -> 1.6.12 ) [CRAN]
## furrr (NA -> 0.3.1 ) [CRAN]
## coda (NA -> 0.19-4 ) [CRAN]
## knitr (1.44 -> 1.45 ) [CRAN]
## shiny (1.7.5.1 -> 1.8.0 ) [CRAN]
## broom.mixed (NA -> 0.2.9.4) [CRAN]
## Installing 22 packages: xfun, rlang, htmltools, parallelly, listenv, vctrs, lifecycle, globals, future, utf8, withr, fansi, stringi, stringr, dplyr, evaluate, httpuv, furrr, coda, knitr, shiny, broom.mixed
##
## The downloaded binary packages are in
## /var/folders/38/pf42f0g52sz6c6j6kt1krrxc0000gn/T//Rtmppebjy9/downloaded_packages
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/38/pf42f0g52sz6c6j6kt1krrxc0000gn/T/Rtmppebjy9/remotes9a142af3fa8c/datalorax-equatiomatic-29ff168/DESCRIPTION’ ... OK
## * preparing ‘equatiomatic’:
## * checking DESCRIPTION meta-information ... OK
## * checking for LF line-endings in source and make files and shell scripts
## * checking for empty or unneeded directories
## * building ‘equatiomatic_0.3.1.tar.gz’
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \] ## Task 2 ## Q: What is the expected average satisfaction with democracy for respondents that were not born in Bulgaria?
Bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
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$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 2247 1589 1778 1969 1597 1742 640 400 206 86 84
Bulgaria_data <- Bulgaria_data %>% filter(!is.na (native))
Bulgaria_data <- Bulgaria_data %>% filter(!is.na (stfdem))
unique(Bulgaria_data$stfdem)
## [1] 0 1 2 3 5 4 6 8 7 10 9
table(Bulgaria_data$stfdem)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 2247 1588 1778 1968 1597 1742 640 400 206 86 84
unique(Bulgaria_data$native)
## [1] "Yes" "No"
table(Bulgaria_data$native)
##
## No Yes
## 97 12239
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
tidy(model2) |>
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(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
## 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
equatiomatic::extract_eq(model2, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \] ## A: 2.804 is the expected average satisfaction with democracy for respondents that were not born in Bulgaria.
##Task 3
sweden_data <- ess %>%
filter(cntry == "SE") %>%
mutate(
clsprty = case_when(
clsprty == 1 ~ "Yes",
clsprty == 2 ~ "No",
clsprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(clsprty)
),
trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(sweden_data$trstplt)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 866 782 1429 2178 2456 3873 2829 2335 934 201 119
sweden_data <- sweden_data %>% filter(!is.na(trstplt))
sweden_data <- sweden_data %>% filter(!is.na(educ_level))
unique(sweden_data$trstplt)
## [1] 3 7 2 5 8 6 4 1 0 9 10
table(sweden_data$trstplt)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 866 782 1429 2178 2456 3873 2829 2335 934 201 119
unique(sweden_data$educ_level)
## [1] "No BA" "BA"
table(sweden_data$educ_level)
##
## BA No BA
## 4710 13292
model3 <- lm(trstplt ~ educ_level, data = sweden_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept) educ_levelNo BA
## 5.133333 -0.696454
tidy(model3)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 5.13 0.0308 166. 0
## 2 educ_levelNo BA -0.696 0.0359 -19.4 5.21e-83
tidy(model3) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 5.133 | 0.031 | 166.426 | 0 |
| educ_levelNo BA | -0.696 | 0.036 | -19.402 | 0 |
summary(model3)
##
## Call:
## lm(formula = trstplt ~ educ_level, data = sweden_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.1333 -1.4369 0.5631 1.5631 5.5631
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.13333 0.03084 166.4 <2e-16 ***
## educ_levelNo BA -0.69645 0.03590 -19.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.117 on 18000 degrees of freedom
## Multiple R-squared: 0.02049, Adjusted R-squared: 0.02043
## F-statistic: 376.4 on 1 and 18000 DF, p-value: < 2.2e-16
equatiomatic::extract_eq(model3, use_coefs = TRUE)
\[ \operatorname{\widehat{trstplt}} = 5.13 - 0.7(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \]