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("/Users/jasminevvvvvvv/Desktop/UT/SOC202/Homework/All-ESS-Data.fst")

Task 1

belgium_data <- ess %>%
  filter(cntry == "BE") %>%
 
   mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
    wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  )) 

belgium_data <- belgium_data %>% filter(!is.na (wrkprty))
belgium_data <- belgium_data %>% filter(!is.na (trstep))

unique(belgium_data$wrkprty)
## [1] "No"  "Yes"
unique(belgium_data$trstep)
##  [1]  0  7  8  5  6  4  9  3  1 10  2
model1 <- lm(trstplt ~ wrkprty, data = belgium_data)

coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.3694439   0.5934172
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    4.37     0.0387    113.   0       
## 2 wrkprtyYes     0.593    0.179       3.31 0.000926
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 4.369 0.039 112.983 0.000
wrkprtyYes 0.593 0.179 3.313 0.001
summary(model1)
## 
## Call:
## lm(formula = trstplt ~ wrkprty, data = belgium_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.963 -1.369  0.631  1.631 94.631 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.36944    0.03867 112.983  < 2e-16 ***
## wrkprtyYes   0.59342    0.17914   3.313 0.000926 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.716 on 15596 degrees of freedom
## Multiple R-squared:  0.0007031,  Adjusted R-squared:  0.0006391 
## F-statistic: 10.97 on 1 and 15596 DF,  p-value: 0.0009262
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.43   -> 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
## 
## The downloaded binary packages are in
##  /var/folders/wp/nzrghzgx1n147zc2bwsdntc40000gn/T//Rtmps5l5vq/downloaded_packages
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/wp/nzrghzgx1n147zc2bwsdntc40000gn/T/Rtmps5l5vq/remotes27fd25eb85/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’
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplt}} = 4.37 + 0.59(\operatorname{wrkprty}_{\operatorname{Yes}}) \]

Task 2

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_)
         )

model2 <- lm(stfdem ~ native, data = bulgaria_data)

tidy(model2)
## # 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(model2) |>
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(model2)
## 
## 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 expected average satisfaction with democracy for respondents that were not born in Bulgaria is 2.8041.

Task 3

norway_data <- ess %>%
  filter(cntry == "NO") %>%
  mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),  
         native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
         )

model3 <- lm(stfdem ~ native, data = norway_data)

coefficients <- coef(model3)
print(coefficients)
## (Intercept)   nativeYes 
##    8.897742   -1.305407
tidy(model3)
## # A tibble: 2 × 5
##   term        estimate std.error statistic      p.value
##   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
## 1 (Intercept)     8.90     0.217     40.9  0           
## 2 nativeYes      -1.31     0.228     -5.72 0.0000000111
tidy(model3) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 8.898 0.217 40.925 0
nativeYes -1.305 0.228 -5.715 0
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{stfdem}} = 8.9 - 1.31(\operatorname{native}_{\operatorname{Yes}}) \]