setwd("~/Desktop/tut files and data sets")  # Replace /path/to/your/folder with the actual path you just copied.
# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the 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
## [[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"
ess <- read_fst("All-ESS-Data.fst")
netherlands_data <- ess %>%
  filter(cntry == "NL") %>%

  mutate(
    happy = ifelse(happy %in% c(77, 88, 99), NA, happy), 
    health = ifelse(health %in% c(7, 8, 9), NA, health), 
    rlgatnd = ifelse(rlgatnd %in% c(77, 88, 99), NA, rlgatnd),  
    rlgdgr = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr),
    dscrgrp = ifelse(dscrgrp %in% c(7, 8, 9), NA, dscrgrp)# 
  )
netherlands_data <- netherlands_data %>%
  mutate(dscrgrp = case_when(
    pbldmn == 1 ~ 1,
    pbldmn == 2 ~ 0,   
    TRUE ~ NA_real_    
  ))
model5 <- glm(happy ~  rlgatnd + rlgdgr + rlgatnd*rlgdgr, family = poisson, data = netherlands_data, weights = round(weight))
interaction_plot <- effect("rlgatnd*rlgdgr", model5, na.rm=TRUE)

plot(interaction_plot,
     main="Interaction effect",
     xlab="Religious Service Attendance",
     ylab="Happiness")

interaction_plot
## 
##  rlgatnd*rlgdgr effect
##        rlgdgr
## rlgatnd        0        2        5        8       10
##       1 7.143728 7.328418 7.614443 7.911633 8.116175
##       2 7.248241 7.402592 7.640304 7.885649 8.053574
##       4 7.461878 7.553202 7.692289 7.833937 7.929816
##       6 7.681811 7.706876 7.744628 7.782565 7.807959
##       7 7.794196 7.784882 7.770931 7.757005 7.747735
 models <- list(
   "Model 1" = glm(happy ~ rlgdgr, family = poisson, data = netherlands_data, weights = round(weight)),
   "Model 2" = glm(happy ~ health, family = poisson, data = netherlands_data, weights = round(weight)),
   "Model 3" = glm(happy ~ rlgatnd, family = poisson, data = netherlands_data, weights = round(weight)),
   "Model 4" = glm(happy ~ dscrgrp, family = poisson, data = netherlands_data, weights = round(weight)))
   
modelplot(models, coef_omit = 'Interc')
## Warning in stats::dpois(round(resp), predicted, log = TRUE) * w: longer object
## length is not a multiple of shorter object length

## Warning in stats::dpois(round(resp), predicted, log = TRUE) * w: longer object
## length is not a multiple of shorter object length

## Warning in stats::dpois(round(resp), predicted, log = TRUE) * w: longer object
## length is not a multiple of shorter object length

## Warning in stats::dpois(round(resp), predicted, log = TRUE) * w: longer object
## length is not a multiple of shorter object length

model1 <- glm(happy ~ rlgdgr, family = poisson, data = netherlands_data)
model2 <- glm(happy ~ rlgdgr + health, family = poisson, data = netherlands_data)
model3 <- glm(happy ~ rlgdgr + rlgatnd + health, family = poisson, data = netherlands_data)
model4 <- glm(happy ~ rlgdgr + dscrgrp + rlgatnd + health, family = poisson, data = netherlands_data)
model5 <- glm(happy ~ rlgdgr + dscrgrp + rlgatnd + health + dscrgrp*rlgatnd, family = poisson, data = netherlands_data)
modelsummary(
  list(model1, model2, model3, model4, model5),
  fmt = 1,
  estimate  = c("{estimate} ({std.error})",
                "{estimate} ({std.error})",
                "{estimate} ({std.error})",
                "{estimate} ({std.error})",
                "{estimate} ({std.error})"),

statistic = NULL,
coef_omit = "Intercept")
 (1)   (2)   (3)   (4)   (5)
rlgdgr 0.0 (0.0) 0.0 (0.0) 0.0 (0.0) 0.0 (0.0) 0.0 (0.0)
health −0.1 (0.0) −0.1 (0.0) −0.1 (0.0) −0.1 (0.0)
rlgatnd 0.0 (0.0) 0.0 (0.0) 0.0 (0.0)
dscrgrp 0.0 (0.0) 0.0 (0.1)
dscrgrp × rlgatnd 0.0 (0.0)
Num.Obs. 18247 18240 18228 16754 16754
AIC 76091.6 75654.9 75605.0 69520.1 69522.0
BIC 76107.2 75678.4 75636.2 69558.7 69568.3
Log.Lik. −38043.803 −37824.467 −37798.503 −34755.042 −34754.975
RMSE 1.37 1.31 1.31 1.31 1.31
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)

\[ \log ({ \widehat{E( \operatorname{happy} )} }) = 2.22 + 0(\operatorname{rlgdgr}) + 0(\operatorname{rlgatnd}) - 0.07(\operatorname{health}) \]