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.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ 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] "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"
remotes::install_github("datalorax/equatiomatic", force = TRUE)
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/kd/m_bm088s23x_61gn79n1c0kh0000gn/T/RtmpAkGbE0/remotes26b12432a784/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’
ess <- read_fst("All-ESS-Data.fst")
belgium_data <- ess %>%
filter(cntry == "BE") %>%
mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt), ## Example of a trust variable
)
unique(belgium_data$trstplt)
## [1] 8 6 7 4 NA 5 3 9 2 1 0 10
unique(belgium_data$wrkprty)
## [1] 2 1 9 8 7 NA
belgium_data <- belgium_data %>% filter(!is.na(trstplt))
belgium_data <- belgium_data %>% filter(!is.na(wrkprty))
model1 <- lm(trstplt ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) wrkprty
## 4.9630918 -0.3984755
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{trstplt}} = 4.96 - 0.4(\operatorname{wrkprty}) \]
Interpretation:
Intercept(4.96): Means that for Belgium, if a person has not worked in a political party before, then the expected value for trstplt is about 4.96.
wrkprty(-0.4): Means that if a person has worked in a political party before, then the expected value for trstplt is about 4.96-0.4 = 4.56
bulgaria_data <- ess %>%
filter(cntry == "BG") %>%
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(bulgaria_data$native)
##
## No Yes
## 104 13134
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))
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 |
Expected average satisfaction with democracy for respondents that were not born in Bulgaria: 2.8041237
gb_data <- ess %>%
filter(cntry == "GB") %>%
mutate(imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
native = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_)
)
table(gb_data$imwbcnt)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1281 989 1654 2034 2135 4946 2035 2324 1801 705 712
table(gb_data$native)
##
## No Yes
## 2409 18564
model1 <- lm(imwbcnt ~ native, data = gb_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) nativeYes
## 6.318977 -1.655959
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{imwbcnt}} = 6.32 - 1.66(\operatorname{native}_{\operatorname{Yes}}) \]
Interpretation:
Intercept(6.32): Means that if a person was not born in the UK, then the expected value for imwbcnt is about 6.32
imwbcnt(-1.66): Means that if a person was born in the UK, then the expected value for imwbcnt is about 6.32-1.66 = 4.66