Loading fst

packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom","remotes") # add any you need here

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
## ── 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 'infer' was built under R version 4.3.2
## Warning: package 'fst' was built under R version 4.3.2
## 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"
install.packages("fst")
## Warning: package 'fst' is in use and will not be installed
library(fst)

setwd("C:/Users/aiden/Desktop/SOC202 RStudio")
getwd()
## [1] "C:/Users/aiden/Desktop/SOC202 RStudio"
ess <- read_fst("All-ESS-Data.fst")

TASK 1

Extract the coefficients and intercept using trust in the European Parliament (as the outcome) and worked in a political party (wrkprty, as the explanatory), for the country of Belgium. Write the regression equation and interpret.

belgium <- ess %>%
  filter(cntry == "BE") %>%
    mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep))
unique(belgium$trstep)
##  [1]  0  7  8  5  6 NA  4  9  3  1 10  2
belgium <- belgium %>% filter(!is.na(trstep))
belgium <- belgium %>%
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  ))
table(belgium$wrkprty)
## 
##    No   Yes 
## 14871   727
unique(belgium$wrkprty)
## [1] "No"  "Yes" NA
belgium <- belgium %>% filter(!is.na(wrkprty))
unique(belgium$wrkprty)
## [1] "No"  "Yes"
tsk1mdl <- lm(trstep ~ wrkprty, data = belgium)

coefficients <- coef(tsk1mdl)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338

Interpretation:

(Intercept) 4.94: This means that the value of not having worked in a political party and trusting in the European Union is ~4.94, which is close to 5, the median value between no trust at all and complete trust

wrkprtyYes 0.43: Having worked in a political party is associated with an increase of ~0.43 in the predicted value of trust in the european union, compared to having a not worked in a political party.

In summary: if you’ve worked in a political party, you’re more likely to trust the european union

TASK 2

Produce a model summary output using either tidy() or summary() (your choice). Use stfdem (satisfaction with democracy) as the outcome and born in country as a predictor (which we recoded/renamed as ‘native’ in the tutorial), for the country of Bulgaria. What is the expected average satisfaction with democracy for respondents that were not born in Bulgaria?

bulgaria <- ess %>%
  filter(cntry == "BG") %>%
    mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem))

unique(bulgaria$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
bulgaria <- bulgaria %>% filter(!is.na(stfdem))
unique(bulgaria$stfdem)
##  [1]  0  1  2  3  5  4  6  8  7 10  9
bulgaria <- bulgaria %>%
  mutate(native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_))
table(bulgaria$native)
## 
##    No   Yes 
##    97 12239
unique(bulgaria$native)
## [1] "Yes" "No"  NA
bulgaria <- bulgaria %>% filter(!is.na(native))
unique(bulgaria$native)
## [1] "Yes" "No"
bulgariamdl <- lm(stfdem ~ native, data = bulgaria)
coefficients2 <- coef(bulgariamdl)
print(coefficients2)
## (Intercept)   nativeYes 
##   2.8041237   0.1189909
tidy(bulgariamdl)
## # 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

The expected average value for satisfaction with democracy of people not born in Bulgaria is ~2.804, as noted by the intercept. For people born in Bulgaria, it is ~0.119 higher, so around 2.923. Using the markers from ESS, both are are dissatsified.

TASK 3

Based on your own outcome and country of interest, as well as one of the predictors recoded in the tutorial, produce an equatiomatic regression equation. Interpret the regression equation.

Research Question: How does the state of the economy in Bulgaria affect the levels of happiness? happy: How happy are you (https://ess-search.nsd.no/en/variable/a5c95af9-6037-4e52-9156-2ceac708eb3f)

bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
    mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy))

unique(bulgaria_data$happy)
##  [1]  6  0 10  5  7  9  8  4  1  3  2 NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(happy))
bulgaria_data <- bulgaria_data %>% filter(!is.na(happy))
unique(bulgaria_data$happy)
##  [1]  6  0 10  5  7  9  8  4  1  3  2
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_))
table(bulgaria_data$geo)
## 
## Peri-Urban      Rural      Urban 
##        322       7447       5320
unique(bulgaria_data$geo)
## [1] "Rural"      "Urban"      "Peri-Urban" NA
bulgaria_data <- bulgaria_data %>% filter(!is.na(geo))
unique(bulgaria_data$geo)
## [1] "Rural"      "Urban"      "Peri-Urban"
bulgariamodel <- lm(happy ~ geo, data = bulgaria_data)
coefficients3 <- coef(bulgariamodel)
print(coefficients3)
## (Intercept)    geoRural    geoUrban 
##  5.36024845 -0.04428229  0.51042824

This is difficult to interpret since it’s a multinomial variable. However, we think that the intercept (~5.36) represents the expected value of being happy in a peri-urban location. The expected value for those in rural areas is 0.044 less than the intercept, while the expected value for rural areas is 0.51 more than the intercept.

tidy(bulgariamodel)
## # A tibble: 3 × 5
##   term        estimate std.error statistic   p.value
##   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)   5.36       0.143    37.5   1.76e-292
## 2 geoRural     -0.0443     0.146    -0.303 7.62e-  1
## 3 geoUrban      0.510      0.147     3.47  5.26e-  4
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.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
## Warning: packages 'stringr', 'dplyr' are in use and will not be installed
## Installing packages into 'C:/Users/aiden/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'xfun' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'xfun'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\xfun\libs\x64\xfun.dll to
## C:\Users\aiden\AppData\Local\R\win-library\4.3\xfun\libs\x64\xfun.dll:
## Permission denied
## Warning: restored 'xfun'
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\aiden\AppData\Local\R\win-library\4.3\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
## package 'htmltools' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'htmltools'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\htmltools\libs\x64\htmltools.dll
## to
## C:\Users\aiden\AppData\Local\R\win-library\4.3\htmltools\libs\x64\htmltools.dll:
## Permission denied
## Warning: restored 'htmltools'
## package 'parallelly' successfully unpacked and MD5 sums checked
## package 'listenv' successfully unpacked and MD5 sums checked
## package 'vctrs' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vctrs'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\vctrs\libs\x64\vctrs.dll
## to C:\Users\aiden\AppData\Local\R\win-library\4.3\vctrs\libs\x64\vctrs.dll:
## Permission denied
## Warning: restored 'vctrs'
## package 'lifecycle' successfully unpacked and MD5 sums checked
## package 'globals' successfully unpacked and MD5 sums checked
## package 'future' successfully unpacked and MD5 sums checked
## package 'utf8' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'utf8'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\utf8\libs\x64\utf8.dll to
## C:\Users\aiden\AppData\Local\R\win-library\4.3\utf8\libs\x64\utf8.dll:
## Permission denied
## Warning: restored 'utf8'
## package 'withr' successfully unpacked and MD5 sums checked
## package 'fansi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'fansi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\fansi\libs\x64\fansi.dll
## to C:\Users\aiden\AppData\Local\R\win-library\4.3\fansi\libs\x64\fansi.dll:
## Permission denied
## Warning: restored 'fansi'
## package 'stringi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'stringi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\stringi\libs\x64\stringi.dll
## to C:\Users\aiden\AppData\Local\R\win-library\4.3\stringi\libs\x64\stringi.dll:
## Permission denied
## Warning: restored 'stringi'
## package 'evaluate' successfully unpacked and MD5 sums checked
## package 'httpuv' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'httpuv'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aiden\AppData\Local\R\win-library\4.3\00LOCK\httpuv\libs\x64\httpuv.dll
## to C:\Users\aiden\AppData\Local\R\win-library\4.3\httpuv\libs\x64\httpuv.dll:
## Permission denied
## Warning: restored 'httpuv'
## package 'furrr' successfully unpacked and MD5 sums checked
## package 'coda' successfully unpacked and MD5 sums checked
## package 'knitr' successfully unpacked and MD5 sums checked
## package 'shiny' successfully unpacked and MD5 sums checked
## package 'broom.mixed' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aiden\AppData\Local\Temp\Rtmp42eJ7m\downloaded_packages
## Running `R CMD build`...
## * checking for file 'C:\Users\aiden\AppData\Local\Temp\Rtmp42eJ7m\remotes31b47277437\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'
## Installing package into 'C:/Users/aiden/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
equatiomatic::extract_eq(bulgariamodel, use_coefs = TRUE)

\[ \operatorname{\widehat{happy}} = 5.36 - 0.04(\operatorname{geo}_{\operatorname{Rural}}) + 0.51(\operatorname{geo}_{\operatorname{Urban}}) \]

y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon happy(hat) = 5.36 - 0.04 (Rural) + 0.51 (Urban) + error term