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)
## ── 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")
france_data <- read_fst("france_data.fst")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(
wrkprty = case_when(
wrkprty == 1 ~ "Yes",
wrkprty == 2 ~ "No",
wrkprty %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(wrkprty)
),
happy = case_when(
trstep == 1 ~ "No trust",
trstep == 2 ~ "No trust",
happy == 3 ~ "No trust",
trstep == 4 ~ "No trust",
trstep == 5 ~ "No trust",
trstep == 6 ~ "Trust",
trstep == 7 ~ "Trust",
trstep == 8 ~ "Trust",
trstep == 9 ~ "Trust",
trstep == 10 ~ "Trust",
trstep %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(trstep)
)
)
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(
stfdem = case_when(
stfdem %in% c(77, 88, 99) ~ NA_character_,
TRUE ~ as.character (stfdem)),
native = recode (brncntr,
'1' = "Yes",
'2' = "No",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_)
)
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(france_data$native)
## < table of extent 0 >
table(france_data$geo)
## < table of extent 0 >
france_data <- france_data %>%
mutate(
pdwrk = case_when(
pdwrk == 1 ~ 'Yes',
pdwrk == 0 ~ 'No',
TRUE ~ NA_character_
),
retired_status = case_when(
rtrd == 1 ~ "Yes",
rtrd == 0 ~ "No",
TRUE ~ NA_character_
),
happy = case_when(
happy == 1 ~ "Extremely unhappy",
happy == 2 ~ "Often unhappy",
happy == 3 ~ "Frequently unhappy",
happy == 4 ~ "Occasionally unhappy",
happy == 5 ~ "Not happy or unhappy",
happy == 6 ~ "Somewhat happy",
happy == 7 ~ "Occasionally happy",
happy == 8 ~ "Frequently happy",
happy == 9 ~ "Often happy",
happy == 10 ~ "Extremely happy",
happy %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(happy)
)
)
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
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))
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprty
## 5.5366318 -0.2943243
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 5.54 0.129 42.9 0
## 2 wrkprty -0.294 0.0652 -4.52 0.00000634
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 5.537 | 0.129 | 42.924 | 0 |
| wrkprty | -0.294 | 0.065 | -4.516 | 0 |
summary(model1)
##
## Call:
## lm(formula = trstep ~ wrkprty, data = belgium_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.242 -0.948 0.052 2.052 5.052
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.53663 0.12899 42.924 < 2e-16 ***
## wrkprty -0.29432 0.06517 -4.516 6.34e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.284 on 15612 degrees of freedom
## (1316 observations deleted due to missingness)
## Multiple R-squared: 0.001305, Adjusted R-squared: 0.001241
## F-statistic: 20.4 on 1 and 15612 DF, p-value: 6.34e-06
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(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{trstep}} = 5.54 - 0.29(\operatorname{wrkprty}) \] Looking at the results, if a person has worked in a political party, their predicted average trust in European Parliament is a decrease of -0.294.
model1 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 2.8041237 0.1189909
tidy(model1)
## # 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(model1) |>
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(model1)
##
## 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
## (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 satisfaction with democracy increases by 0.119 if the respondents are born in Bulgaria, hence the average satisfaction with democracy for respondents that were not born in Bulgaria is the opposite.
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
)
unique(france_data$happy)
## [1] 10 3 6 5 8 7 9 4 2 0 1 NA
france_data <- france_data %>% filter(!is.na(happy))
model1 <- lm(happy ~ rtrd, data = france_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) rtrd
## 7.2579668 -0.1836693
tidy(model1)
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 7.26 0.0156 464. 0
## 2 rtrd -0.184 0.0283 -6.49 8.68e-11
tidy(model1) |>
knitr::kable(digits = 3)
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 7.258 | 0.016 | 464.426 | 0 |
| rtrd | -0.184 | 0.028 | -6.492 | 0 |
summary(model1)
##
## Call:
## lm(formula = happy ~ rtrd, data = france_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.2580 -1.2580 0.7420 0.9257 2.9257
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.25797 0.01563 464.426 < 2e-16 ***
## rtrd -0.18367 0.02829 -6.492 8.68e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.796 on 19010 degrees of freedom
## Multiple R-squared: 0.002212, Adjusted R-squared: 0.00216
## F-statistic: 42.15 on 1 and 19010 DF, p-value: 8.682e-11
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
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(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{happy}} = 7.26 - 0.18(\operatorname{rtrd}) \] Respondents that are retired have a decrease in their happiness by -0.184.