setwd("~/Desktop/SOC 252 H1 /R studio 252/Soc252_Huang_Stepping Stone II")
getwd()
## [1] "/Users/marcohuang/Desktop/SOC 252 H1 /R studio 252/Soc252_Huang_Stepping Stone II"
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "aod", "interactions")

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
## Loading required package: carData
## 
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## Loading required package: grid
## 
## Loading required package: Matrix
## 
## 
## Attaching package: 'Matrix'
## 
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## 
## Loading required package: survival
## 
## 
## Attaching package: 'survey'
## 
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## 
## 
## Attaching package: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## 
## Attaching package: 'aod'
## 
## 
## The following object is masked from 'package:survival':
## 
##     rats
## [[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] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[6]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[7]]
##  [1] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[8]]
##  [1] "aod"          "MASS"         "survey"       "survival"     "Matrix"      
##  [6] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [11] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "interactions" "aod"          "MASS"         "survey"       "survival"    
##  [6] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [11] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")
UK_data <- ess %>%
  filter(cntry == "GB") %>%
  mutate(
    agea = ifelse(agea < 999, agea, NA),
    hhmmb = ifelse(hhmmb %in% c(77, 88, 99), NA, hhmmb),
    uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m),
    
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
      
 )
  )
model1 <- lm(hhmmb ~ uemp3m, data = UK_data)
model2 <- lm(hhmmb ~ uemp3m + educ_level, data = UK_data)
model3 <- lm(hhmmb ~ uemp3m + educ_level + gndr*agea, data = UK_data)

modelsummary(list(model1, model2, model3))
 (1)   (2)   (3)
(Intercept) 2.397 2.480 3.780
(0.038) (0.041) (0.045)
uemp3m −0.035 −0.039 0.158
(0.021) (0.021) (0.019)
educ_levelNo BA −0.109 0.071
(0.020) (0.018)
gndrMale −0.381
(0.046)
agea −0.035
(0.001)
gndrMale × agea 0.008
(0.001)
Num.Obs. 20911 20911 20780
R2 0.000 0.002 0.208
R2 Adj. 0.000 0.001 0.208
AIC 70296.1 70267.7 65001.8
BIC 70319.9 70299.5 65057.4
Log.Lik. −35145.034 −35129.867 −32493.889
RMSE 1.30 1.30 1.16
summary(model2)
## 
## Call:
## lm(formula = hhmmb ~ uemp3m + educ_level, data = UK_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4416 -1.2942 -0.2942  0.7058  8.7058 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      2.48021    0.04112  60.315  < 2e-16 ***
## uemp3m          -0.03858    0.02110  -1.829   0.0675 .  
## educ_levelNo BA -0.10886    0.01976  -5.509 3.65e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.298 on 20908 degrees of freedom
##   (68 observations deleted due to missingness)
## Multiple R-squared:  0.001581,   Adjusted R-squared:  0.001486 
## F-statistic: 16.55 on 2 and 20908 DF,  p-value: 6.55e-08
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(model2, use_coefs = TRUE)
## Registered S3 methods overwritten by 'broom':
##   method            from  
##   tidy.glht         jtools
##   tidy.summary.glht jtools

\[ \operatorname{\widehat{hhmmb}} = 2.48 - 0.04(\operatorname{uemp3m}) - 0.11(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \]

model4 <- lm(hhmmb ~ uemp3m + lrscale + gndr*educ_level, data = UK_data, weights = weight)
interaction_plot <- effect("gndr*educ_level", model4, na.rm=TRUE)

plot(interaction_plot,
     main="Interaction effect",
     xlab="Education level",
     ylab="gendr")

interaction_plot
## 
##  gndr*educ_level effect
##         educ_level
## gndr           BA    No BA
##   Female 2.450632 2.225428
##   Male   2.593125 2.247415
summary(model4)
## 
## Call:
## lm(formula = hhmmb ~ uemp3m + lrscale + gndr * educ_level, data = UK_data, 
##     weights = weight)
## 
## Weighted Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.476  -9.740  -1.792   7.053 158.646 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               2.3312166  0.1295810  17.990  < 2e-16 ***
## uemp3m                    0.0188215  0.0624002   0.302  0.76297    
## lrscale                   0.0060514  0.0009865   6.134 1.01e-09 ***
## gndrMale                  0.1424927  0.1087106   1.311  0.19008    
## educ_levelNo BA          -0.2252033  0.0823123  -2.736  0.00627 ** 
## gndrMale:educ_levelNo BA -0.1205058  0.1270597  -0.948  0.34302    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.21 on 2248 degrees of freedom
##   (18725 observations deleted due to missingness)
## Multiple R-squared:  0.02379,    Adjusted R-squared:  0.02161 
## F-statistic: 10.95 on 5 and 2248 DF,  p-value: 1.949e-10
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(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{hhmmb}} = 2.4 - 0.04(\operatorname{uemp3m}) \]

UK_data <- UK_data %>%
  mutate(educ_level = case_when(
    educ_level == 1 ~ "BA",
    educ_level == 0 ~ "No BA",
    TRUE ~ as.character(educ_level) 
  ))
model5 <- lm(hhmmb ~ uemp3m + lrscale + uemp3m*educ_level, data = UK_data, weights = weight)

interaction_plot <- effect("uemp3m*educ_level", model5, na.rm=TRUE)

plot(interaction_plot,
     main="Interaction effect",
     xlab="housemember",
     ylab="level of education")

interaction_plot
## 
##  uemp3m*educ_level effect
##       educ_level
## uemp3m       BA    No BA
##    1   2.465150 2.235491
##    1.2 2.477928 2.235066
##    1.5 2.497095 2.234427
##    1.8 2.516263 2.233788
##    2   2.529041 2.233362