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 <- 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)
modelsummary(
list(model1, model2, model3, model4))
| (1) | (2) | (3) | (4) | |
|---|---|---|---|---|
| (Intercept) | 2.044 | 2.191 | 2.218 | 2.218 |
| (0.005) | (0.009) | (0.018) | (0.018) | |
| rlgdgr | 0.002 | 0.003 | 0.002 | 0.002 |
| (0.001) | (0.001) | (0.001) | (0.001) | |
| health | −0.071 | −0.070 | −0.070 | |
| (0.004) | (0.004) | (0.004) | ||
| rlgatnd | −0.004 | −0.004 | ||
| (0.002) | (0.002) | |||
| dscrgrp | −0.006 | |||
| (0.016) | ||||
| Num.Obs. | 16766 | 16759 | 16754 | 16754 |
| AIC | 69943.2 | 69538.7 | 69518.2 | 69520.1 |
| BIC | 69958.7 | 69561.9 | 69549.1 | 69558.7 |
| Log.Lik. | −34969.617 | −34766.348 | −34755.100 | −34755.042 |
| 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)
\[ \log ({ \widehat{E( \operatorname{happy} )} }) = 2.22 + 0(\operatorname{rlgdgr}) - 0.01(\operatorname{dscrgrp}) + 0(\operatorname{rlgatnd}) - 0.07(\operatorname{health}) \]