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") # 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.
## [[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"
ess <- read_fst("All-ESS-Data.fst")
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy), ## Example of a trust variable
health = ifelse(health %in% c(7, 8, 9), NA, health), # Example of a satisfaction variable
rlgatnd = ifelse(rlgatnd %in% c(77, 88, 99), NA, rlgatnd), # Example of a immigration attitude variable
dscrgrp = ifelse(dscrgrp %in% c(7, 8, 9), NA, dscrgrp),
rlgdgr = ifelse(rlgdgr %in% c(77, 88, 99), NA, rlgdgr)# Example of a human values variable
)
netherlands_data <- netherlands_data %>%
mutate(
dscrgrp = case_when(
dscrgrp == 1 ~ 'Yes',
dscrgrp == 2 ~ 'No',
TRUE ~ NA_character_
))
model1 <- lm(happy ~ rlgdgr, data = netherlands_data)
model2 <- lm(happy ~ rlgdgr + dscrgrp, data = netherlands_data)
model3 <- lm(happy ~ rlgdgr + dscrgrp + health, data = netherlands_data)
model4 <- lm(happy ~ rlgdgr + dscrgrp + rlgatnd + health*rlgatnd, data = netherlands_data)
modelsummary(
list(model1, model2, model3, model4))
|
|
(1)
|
(2)
|
(3)
|
(4)
|
|
(Intercept)
|
7.738
|
7.773
|
8.863
|
8.528
|
|
|
(0.018)
|
(0.018)
|
(0.031)
|
(0.126)
|
|
rlgdgr
|
0.013
|
0.016
|
0.025
|
0.016
|
|
|
(0.003)
|
(0.003)
|
(0.003)
|
(0.004)
|
|
dscrgrpYes
|
|
−0.613
|
−0.489
|
−0.489
|
|
|
|
(0.037)
|
(0.035)
|
(0.035)
|
|
health
|
|
|
−0.524
|
−0.261
|
|
|
|
|
(0.013)
|
(0.051)
|
|
rlgatnd
|
|
|
|
0.062
|
|
|
|
|
|
(0.020)
|
|
rlgatnd × health
|
|
|
|
−0.044
|
|
|
|
|
|
(0.008)
|
|
Num.Obs.
|
18247
|
18173
|
18166
|
18155
|
|
R2
|
0.001
|
0.016
|
0.103
|
0.105
|
|
R2 Adj.
|
0.001
|
0.016
|
0.102
|
0.105
|
|
AIC
|
63251.9
|
62630.3
|
60936.8
|
60861.9
|
|
BIC
|
63275.4
|
62661.5
|
60975.8
|
60916.5
|
|
Log.Lik.
|
−31622.959
|
−31311.143
|
−30463.383
|
−30423.948
|
|
RMSE
|
1.37
|
1.36
|
1.29
|
1.29
|