Setting up environment

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.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"        
## 
## [[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"
ess <- read_fst("/Users/paulcavaco/Desktop/Alyssa school/Homework 8/All-ESS-Data.fst")

Task 1

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))
belgium_data <- belgium_data %>%
  mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale)) %>%
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  ))

table(belgium_data$wrkprty)
## 
##    No   Yes 
## 14871   727
table(belgium_data$lrscale)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  468  333  970 1763 1540 5732 1877 1853 1038  244  290
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   4.9405554   0.4308338

Interpretation:

(Intercept) 4.94:

When looking at the category of people who have not worked in a political party, the expected value of trstep is approximately 4.94

wrkprtyYes shows us the difference between those who have worked in a political party and those who have not in terms of their trust in the European Parliament. In this case, the difference is approximately 0.43

With this information, one can conclude that if a person has worked in a political party, their trust in the European Parliament is 4.94. If someone has not workwd for the European Parliament, is approximately 4.51.

Task 2

bulgaria_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), 
  )
unique(bulgaria_data$stfdem)
##  [1] NA  6  8  7  2  5  9  4  1 10  3  0
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))
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(bulgaria_data$native)
## 
##    No   Yes 
##  2038 15147
model1 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)   nativeYes 
##   6.0775270  -0.7120421
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    6.08     0.0471     129.  0       
## 2 nativeYes     -0.712    0.0502     -14.2 1.85e-45
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 6.078 0.047 129.028 0
nativeYes -0.712 0.050 -14.192 0
summary(model1)
## 
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0775 -1.3655  0.6345  1.6345  4.6345 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.07753    0.04710  129.03   <2e-16 ***
## nativeYes   -0.71204    0.05017  -14.19   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.126 on 17183 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.01159,    Adjusted R-squared:  0.01153 
## F-statistic: 201.4 on 1 and 17183 DF,  p-value: < 2.2e-16

The expected average satisfaction with democracy for respondents that were not born in Bulgaria is 6.07

Task 3

italy_data <- ess %>%
  filter(cntry == "IT") %>%
  mutate(stfedu = ifelse(stfedu %in% c(77, 88, 99), NA, stfedu), 
  )
unique(italy_data$stfedu)
##  [1]  6  5  2  4  3  8  7 NA  0  1  9 10
italy_data <- italy_data %>% filter(!is.na(stfedu))
italy_data <- italy_data %>%
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    edulvlfa = case_when(
      essround < 5 & edulvlfa == 55 ~ NA_real_,
      TRUE ~ edulvlfa
    ),
    edulvlfb = case_when(
      essround >= 5 & edulvlfb == 5555 ~ NA_real_,
      TRUE ~ edulvlfb
    ),
    edulvlma = case_when(
      essround < 5 & edulvlma == 55 ~ NA_real_,
      TRUE ~ edulvlma
    ),
    edulvlmb = case_when(
      essround >= 5 & edulvlmb == 5555 ~ NA_real_,
      TRUE ~ edulvlmb
    ),
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
    educ_level_father = case_when(
      essround < 5 & edulvlfa == 5 ~ "BA",
      essround >= 5 & edulvlfb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
    educ_level_mother = case_when(
      essround < 5 & edulvlma == 5 ~ "BA",
      essround >= 5 & edulvlmb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
     eisced = case_when(
      eisced == 0 | eisced > 7 ~ as.character(NA), 
      eisced %in% 1:5 ~ "No BA",           
      eisced == 6 ~ "BA",
      eisced == 7 ~ "MA",
      TRUE ~ as.character(eisced)           
    ),
  )

table(italy_data$educ_level)
## 
##    BA No BA 
##  1456  8290
model1 <- lm(stfedu ~ educ_level, data = italy_data)
coefficients <- coef(model1)
print(coefficients)
##     (Intercept) educ_levelNo BA 
##       5.4100275      -0.1481457
tidy(model1)
## # A tibble: 2 × 5
##   term            estimate std.error statistic p.value
##   <chr>              <dbl>     <dbl>     <dbl>   <dbl>
## 1 (Intercept)        5.41     0.0559     96.8   0     
## 2 educ_levelNo BA   -0.148    0.0606     -2.44  0.0145
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 5.410 0.056 96.767 0.000
educ_levelNo BA -0.148 0.061 -2.444 0.015
summary(model1)
## 
## Call:
## lm(formula = stfedu ~ educ_level, data = italy_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.410 -1.262  0.590  1.738  4.738 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      5.41003    0.05591  96.767   <2e-16 ***
## educ_levelNo BA -0.14815    0.06062  -2.444   0.0145 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.133 on 9744 degrees of freedom
## Multiple R-squared:  0.0006126,  Adjusted R-squared:  0.00051 
## F-statistic: 5.973 on 1 and 9744 DF,  p-value: 0.01455
remotes::install_github("datalorax/equatiomatic", force = TRUE)
## 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]
## vctrs     (0.6.3  -> 0.6.4  ) [CRAN]
## lifecycle (1.0.3  -> 1.0.4  ) [CRAN]
## utf8      (1.2.3  -> 1.2.4  ) [CRAN]
## fansi     (1.0.4  -> 1.0.5  ) [CRAN]
## withr     (2.5.0  -> 2.5.2  ) [CRAN]
## stringi   (1.7.12 -> 1.8.1  ) [CRAN]
## stringr   (1.5.0  -> 1.5.1  ) [CRAN]
## evaluate  (0.21   -> 0.23   ) [CRAN]
## httpuv    (1.6.11 -> 1.6.12 ) [CRAN]
## knitr     (1.44   -> 1.45   ) [CRAN]
## shiny     (1.7.5  -> 1.7.5.1) [CRAN]
## Installing 14 packages: xfun, rlang, htmltools, vctrs, lifecycle, utf8, fansi, withr, stringi, stringr, evaluate, httpuv, knitr, shiny
## 
##   There are binary versions available but the source versions are later:
##            binary source needs_compilation
## xfun         0.40   0.41              TRUE
## rlang       1.1.1  1.1.2              TRUE
## htmltools 0.5.6.1  0.5.7              TRUE
## lifecycle   1.0.3  1.0.4             FALSE
## utf8        1.2.3  1.2.4              TRUE
## withr       2.5.1  2.5.2             FALSE
## stringi    1.7.12  1.8.1              TRUE
## stringr     1.5.0  1.5.1             FALSE
## evaluate     0.22   0.23             FALSE
## httpuv     1.6.11 1.6.12              TRUE
## knitr        1.44   1.45             FALSE
## 
## 
## The downloaded binary packages are in
##  /var/folders/k6/ltvcf0fd4l51q36qjprmzb5c0000gn/T//RtmpkJSvHD/downloaded_packages
## installing the source packages 'xfun', 'rlang', 'htmltools', 'lifecycle', 'utf8', 'withr', 'stringi', 'stringr', 'evaluate', 'httpuv', 'knitr'
## Warning in i.p(...): installation of package 'xfun' had non-zero exit status
## Warning in i.p(...): installation of package 'rlang' had non-zero exit status
## Warning in i.p(...): installation of package 'utf8' had non-zero exit status
## Warning in i.p(...): installation of package 'stringi' had non-zero exit status
## Warning in i.p(...): installation of package 'httpuv' had non-zero exit status
## Warning in i.p(...): installation of package 'htmltools' had non-zero exit
## status
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/k6/ltvcf0fd4l51q36qjprmzb5c0000gn/T/RtmpkJSvHD/remotes1346c65dfe691/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{stfedu}} = 5.41 - 0.15(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \]

y(hat) = intercept + B(hat)0 + B(hat)1 * x1 + epsilon

stfedu(hat) = 5.41 - 0.15 (No BA) + error term