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_    
  ))

netherlands_data <- netherlands_data %>% filter(!is.na(dscrgrp))
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="Religiosity and Religious Service Attendance",
     ylab="Happiness")

interaction_plot
## 
##  rlgatnd*rlgdgr effect
##        rlgdgr
## rlgatnd        0        2        5        8       10
##       1 7.113916 7.307863 7.608741 7.922007 8.137984
##       2 7.223087 7.385329 7.635551 7.894251 8.071569
##       4 7.446480 7.542735 7.689456 7.839030 7.940359
##       6 7.676782 7.703496 7.743741 7.784196 7.811283
##       7 7.794591 7.785157 7.771027 7.756922 7.747534
 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 <- lm(happy ~ rlgdgr, data = netherlands_data)
model2 <- lm(happy ~ rlgdgr + health, data = netherlands_data)
model3 <- lm(happy ~ rlgdgr + rlgatnd + health, data = netherlands_data)
model4 <- lm(happy ~ rlgdgr + rlgatnd + health*rlgatnd, data = netherlands_data)
modelsummary(
  list(model1, model2, model3, model4))
 (1)   (2)   (3)   (4)
(Intercept) 7.723 8.860 9.069 8.543
(0.019) (0.033) (0.065) (0.132)
rlgdgr 0.014 0.023 0.015 0.015
(0.003) (0.003) (0.004) (0.004)
health −0.541 −0.540 −0.304
(0.013) (0.013) (0.053)
rlgatnd −0.030 0.058
(0.008) (0.021)
rlgatnd × health −0.040
(0.009)
Num.Obs. 16766 16759 16754 16754
R2 0.001 0.093 0.093 0.095
R2 Adj. 0.001 0.093 0.093 0.094
AIC 58264.8 56629.6 56605.2 56586.1
BIC 58288.0 56660.5 56643.9 56632.5
Log.Lik. −29129.410 −28310.817 −28297.614 −28287.050
RMSE 1.38 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(model4, use_coefs = TRUE)

\[ \operatorname{\widehat{happy}} = 8.54 + 0.02(\operatorname{rlgdgr}) + 0.06(\operatorname{rlgatnd}) - 0.3(\operatorname{health}) - 0.04(\operatorname{rlgatnd} \times \operatorname{health}) \]