# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "remotes")
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.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
## Warning: package 'remotes' 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"
##
## [[6]]
## [1] "remotes" "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"
ess <- read_fst("All-ESS-Data.fst")
The intercept is 4.94 for trstep as the outcome and wrkprty as the explanatory. That means if the answer is “No” to worked in a political party the expected value of trstep is approximately 4.94.
The coefficient between “Yes” and “No” is 0.43. This means that if someone selected the answer “Yes” to worked in a political party the expected value of trstep increases by 0.43 to approximately 5.37.
Therefore, the regression formula is as follows:
y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon
trstep(hat) = 4.94 + 0.43 (wrkprtyYes) + error term
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
wrkprty = ifelse(wrkprty %in% c(7, 8, 9), NA, wrkprty)
)
belgium_data <- belgium_data %>%
mutate(
wrkprty = case_when(
wrkprty == 1 ~ 'Yes',
wrkprty == 2 ~ 'No',
TRUE ~ NA_character_)
)
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$wrkprty)
## [1] "No" "Yes" NA
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
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
table(belgium_data$wrkprty)
##
## No Yes
## 14871 727
task1model <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(task1model)
print(coefficients)
## (Intercept) wrkprtyYes
## 4.9405554 0.4308338
tidy(task1model)
## # 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(task1model) |>
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(task1model)
##
## 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")
## 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(task1model, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 4.94 + 0.43(\operatorname{wrkprty}_{\operatorname{Yes}}) \]
The average satisfaction with democracy for respondents that were not born in Bulgaria is 2.80 (the intercept).
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
brncntr = ifelse(brncntr %in% c(7, 8, 9), NA, brncntr)
)
bulgaria_data <- bulgaria_data %>%
mutate(
native = recode(brncntr,
`1` = "Yes",
`2` = "No")
)
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$native)
## [1] "Yes" "No" NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(native))
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
table(bulgaria_data$native)
##
## No Yes
## 97 12239
task2model <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(task2model)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(task2model)
## # 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(task2model) |>
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(task2model)
##
## 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(task2model, use_coefs = TRUE)
\[ \operatorname{\widehat{stfdem}} = 2.8 + 0.12(\operatorname{native}_{\operatorname{Yes}}) \]
I used Poland as my country of interest and ability to take an active role in a political group was the outcome of interest, with gender as predictor.
The regression equation is as follows:
actrolg(hat) = 3.34 - 0.84 (gndrWomen) + error term
This means that the men’s answer was on average 3.34 (with 0 being ‘not at all able’ and 10 being ‘completely able’), and the women’s answer was on average 2.5. Gender does seem to have a part in whether a person has the ability to take an active role in a political group.
poland_data <- ess %>%
filter(cntry == "PL") %>%
mutate(actrolg = ifelse(actrolg %in% c(77, 88, 99), NA, actrolg),
gndr = case_when(
gndr == 1 ~ "Men",
gndr == 2 ~ "Women",
gndr == 9 ~ NA_character_,
TRUE ~ as.character(gndr))
)
unique(poland_data$actrolg)
## [1] NA 1 0 6 2 4 3 8 9 5 7 10
poland_data <- poland_data %>% filter(!is.na(actrolg))
unique(poland_data$gndr)
## [1] "Women" "Men"
poland_data <- poland_data %>% filter(!is.na(gndr))
table(poland_data$actrolg)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 398 223 210 185 103 172 65 80 62 23 37
table(poland_data$gndr)
##
## Men Women
## 721 837
task3model <- lm(actrolg ~ gndr, data = poland_data)
coefficients <- coef(task3model)
print(coefficients)
## (Intercept) gndrWomen
## 3.3356449 -0.8410213
tidy(task3model)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.34 0.100 33.3 2.75e-184
## 2 gndrWomen -0.841 0.137 -6.16 9.27e- 10
tidy(task3model) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 3.336 | 0.100 | 33.331 | 0 |
| gndrWomen | -0.841 | 0.137 | -6.160 | 0 |
summary(task3model)
##
## Call:
## lm(formula = actrolg ~ gndr, data = poland_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.3356 -2.4946 -0.4946 1.6644 7.5054
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.3356 0.1001 33.33 < 2e-16 ***
## gndrWomen -0.8410 0.1365 -6.16 9.27e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.687 on 1556 degrees of freedom
## Multiple R-squared: 0.0238, Adjusted R-squared: 0.02318
## F-statistic: 37.94 on 1 and 1556 DF, p-value: 9.266e-10
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(task3model, use_coefs = TRUE)
\[ \operatorname{\widehat{actrolg}} = 3.34 - 0.84(\operatorname{gndr}_{\operatorname{Women}}) \]