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"
setwd("C:/Users/jpcha/Desktop/SOC202")

library(fst)

ess <- read_fst ("All-ESS-Data.fst")

Task 1:

Extracting coefficients…

Filtering to Belgium.

trstep = Trust in European Parliament.

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

Task 2:

bulgaria_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
  )
unique(bulgaria_data$stfdem)
##  [1]  0  1 NA  2  3  5  4  6  8  7 10  9
buglaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
model2 <- lm(stfdem ~ brncntr, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)     wrkprty 
##   5.5366318  -0.2943243
tidy(model2)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    3.06      0.175    17.5   1.01e-67
## 2 brncntr       -0.134     0.172    -0.778 4.36e- 1

The expected average of satisfaction with democracy for respondents that were not born in Bulgaria is %17.49.

Task 3:

spain_data <- ess %>%
  filter(cntry == "ES") %>%
  mutate(clsprty = ifelse(clsprty %in% c(7, 8, 9), NA, clsprty),
  )
unique(spain_data$clsprty)
## [1]  2  1 NA
spain_data <- spain_data %>% filter(!is.na(clsprty))
model3 <- lm(clsprty ~ gndr, data = belgium_data)
coefficients <- coef(model3)
print(coefficients)
## (Intercept)        gndr 
##  1.45645958  0.05473956
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(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{clsprty}} = 1.46 + 0.05(\operatorname{gndr}) \] ## The End.