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.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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("~/Documents/Soc202")
getwd()
## [1] "/Users/leo/Documents/Soc202"
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_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstep = ifelse(trstep %in% c(0, 10, 77, 88, 99), NA, trstep), 
  )
unique(belgium_data$trstep)
##  [1] NA  7  8  5  6  4  9  3  1  2
belgium_data <- belgium_data %>% filter(!is.na(trstep))

belgium_data <- belgium_data %>%
  mutate(wrkprty = case_when(
    wrkprty == 1 ~ "Yes",
    wrkprty == 2 ~ "No",
    wrkprty %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(wrkprty)
  ))
model1 <- lm(trstep ~ wrkprty, data = belgium_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept)  wrkprtyYes 
##   5.2569207   0.3469084
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic    p.value
##   <chr>          <dbl>     <dbl>     <dbl>      <dbl>
## 1 (Intercept)    5.26     0.0161    327.   0         
## 2 wrkprtyYes     0.347    0.0742      4.68 0.00000293
tidy(model1) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 5.257 0.016 326.913 0
wrkprtyYes 0.347 0.074 4.678 0
if (!requireNamespace("remotes", quietly = TRUE)) {
  install.packages("remotes")
}

remotes::install_github("datalorax/equatiomatic", force = TRUE)
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## shiny (1.7.5.1 -> 1.8.0) [CRAN]
## Installing 1 packages: shiny
## 
## The downloaded binary packages are in
##  /var/folders/fs/2z898qp57xqdt_rxc_r7cqqw0000gn/T//Rtmpjoyqci/downloaded_packages
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/fs/2z898qp57xqdt_rxc_r7cqqw0000gn/T/Rtmpjoyqci/remotes3a614d371386/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{trstep}} = 5.26 + 0.35(\operatorname{wrkprty}_{\operatorname{Yes}}) \] intepretation:the dependent variable is the estimated value of “trstep” (trust in the European Parliament) and the independent variable is “wrkprty” (working in a political party), specifically, we are lookingat when “wrkprty” is equal to yes. The left side of the formula represents the estimated or predicted value of “trstep” based on the regression equation. 5.26 is the y intercept of the equation, meaning that when “wrkprty” is equal to “No”, the estimated value of “trstep” is equal to 5.26. 0.35 is the slope which indicates the expected change in the estimated value of “trstep” for a one-unit change in “wrkprty” when it is equal to “Yes”. “Wrkprty yes” represents the indicator that “wrkprty” is equal to yes, and the value of indicator is 1.

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_data <- ess %>%
  filter(cntry == "BG") %>%
  mutate(stfdem = ifelse(stfdem %in% c(0, 10, 77, 88, 99), NA, stfdem), 
  )
unique(bulgaria_data$stfdem)
##  [1] NA  1  2  3  5  4  6  8  7  9
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))

bulgaria_data <- bulgaria_data %>%
  mutate(
  native = recode(brncntr,
                             `1` = "Yes",
                             `2` = "No",
                             `7` = NA_character_,
                             `8` = NA_character_,
                             `9` = NA_character_)
  )

table(bulgaria_data$native)
## 
##   No  Yes 
##   71 9934
model2 <- lm(stfdem ~ native, data = bulgaria_data)
coefficients <- coef(model2)
print(coefficients)
## (Intercept)   nativeYes 
##  3.54929577 -0.03047153
tidy(model2)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)   3.55       0.220    16.1   8.96e-58
## 2 nativeYes    -0.0305     0.221    -0.138 8.90e- 1
tidy(model2) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 3.549 0.220 16.126 0.00
nativeYes -0.030 0.221 -0.138 0.89
summary(model2)
## 
## Call:
## lm(formula = stfdem ~ native, data = bulgaria_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5493 -1.5188 -0.5188  1.4812  5.4812 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.54930    0.22010  16.126   <2e-16 ***
## nativeYes   -0.03047    0.22089  -0.138     0.89    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.855 on 10003 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  1.902e-06,  Adjusted R-squared:  -9.807e-05 
## F-statistic: 0.01903 on 1 and 10003 DF,  p-value: 0.8903

The expected average satisfaction with democracy for respondents that were not born in Bulgaria is equal to Y intercept which is 3.5493. This value represents the situation where the variable nativeYes is equal to 0, which is the responses of people who is not born in Bulgaria

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.

france_data <- ess %>%
  filter(cntry == "FR") %>%
  mutate(happy = ifelse(happy %in% c(0, 10, 77, 88, 99), NA, happy), 
  )
unique(france_data$happy)
##  [1] NA  3  6  5  8  7  9  4  2  1
france_data <- france_data %>% filter(!is.na(happy))

france_data <- france_data %>%
  mutate(
retired_status = case_when(
      rtrd == 1 ~ "Yes",
      rtrd == 0 ~ "No",
      TRUE ~ NA_character_
    ),
)
table(france_data$retired_status)
## 
##    No   Yes 
## 11938  5362
model3 <- lm(happy ~ retired_status, data = france_data)
coefficients <- coef(model3)
print(coefficients)
##       (Intercept) retired_statusYes 
##         7.0225331        -0.1467405
tidy(model3)
## # A tibble: 2 × 5
##   term              estimate std.error statistic      p.value
##   <chr>                <dbl>     <dbl>     <dbl>        <dbl>
## 1 (Intercept)          7.02     0.0145    486.   0           
## 2 retired_statusYes   -0.147    0.0260     -5.65 0.0000000162
tidy(model3) |>
knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 7.023 0.014 485.810 0
retired_statusYes -0.147 0.026 -5.651 0
equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{happy}} = 7.02 - 0.15(\operatorname{retired\_status}_{\operatorname{Yes}}) \] The intercept 7.02 represents the estimated average happiness level of individuals who are not retired. The coefficient for retired status yes is -0.15, meaning that being retired is associated with a decrease of approximately 0.15 in the predicted value of happiness level of people in France. In summary, if a person is not retired, then their predicted average happiness in France is 7.02. If a person is retired(retired_status yes), their predicted value of happiness is 7.02-0.15=6.87.