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.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
## Warning: 程辑包'infer'是用R版本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"
setwd("C:/Users/hp/Documents/SOC202")

getwd()
## [1] "C:/Users/hp/Documents/SOC202"
ess <- read_fst("All-ESS-Data.fst")

Task1

belgium_data <- ess %>%
  filter(cntry == "BE") %>%
  mutate(trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
  )

unique(belgium_data$trstplt)
##  [1]  8  6  7  4 NA  5  3  9  2  1  0 10
belgium_data <- belgium_data %>% filter(!is.na(trstplt))

belgium_model <- lm(trstep ~ wrkprty, data = belgium_data)

coefficients_belgium <- coef(belgium_model)
print(coefficients_belgium)
## (Intercept)     wrkprty 
##   6.9060260   0.1323991

Interception: The intercept represents the predicted value of the outcome variable (in this case, Trust in European Parliament, 0-10) when the explanatory variable is wrkprty. The expected value of trstplt is approximately 6.906. For wrkprty = 0.1323991,This coefficient represents the difference in the predicted value of trust in European Parliament between different “work in political party” categories.

Task2

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

unique(belgium_data$stfdem)
##  [1] 99  6  8  7  2  5  9  4  1 88  3 10  0 77
bulgaria_data <- bulgaria_data %>% filter(!is.na(stfdem))

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

table(bulgaria_data$native)
## 
##     No    Yes 
##  45887 443915
bulgaria_model <- lm(stfdem ~ native, bulgaria_data)

summary_output_bulgaria <- tidy(bulgaria_model)
print(summary_output_bulgaria)
## # A tibble: 2 × 5
##   term        estimate std.error statistic   p.value
##   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)    10.3     0.0764     135.  0        
## 2 nativeYes      -2.07    0.0803     -25.8 4.08e-147
avg_satisfaction_not_born_in_bulgaria <- summary_output_bulgaria %>%
  filter(term == "nativeNo") %>%
  select(estimate)

cat("Expected average satisfaction with democracy for respondents not born in Bulgaria:",avg_satisfaction_not_born_in_bulgaria$estimate, "\n")
## Expected average satisfaction with democracy for respondents not born in Bulgaria:

Task3

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
equation_belgium <- equatiomatic::extract_eq(belgium_model, use_coefs = TRUE)
cat("Regression Equation for Trust in the European Parliament in Belgium:\n")
## Regression Equation for Trust in the European Parliament in Belgium:
cat(equation_belgium, "\n")
## \operatorname{\widehat{trstep}} = 6.91 + 0.13(\operatorname{wrkprty})