packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "kableExtra", "flextable", "equatiomatic", "dplyr", "interactions", "tidyr")
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
## Warning: package 'effects' was built under R version 4.3.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## Warning: package 'survey' was built under R version 4.3.2
## 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: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
## Attaching package: 'flextable'
##
## The following objects are masked from 'package:kableExtra':
##
## as_image, footnote
##
## The following object is masked from 'package:purrr':
##
## compose
## Warning: package 'interactions' was built under R version 4.3.2
## [[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] "kableExtra" "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] "flextable" "kableExtra" "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"
##
## [[10]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[11]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[12]]
## [1] "interactions" "equatiomatic" "flextable" "kableExtra" "MASS"
## [6] "survey" "survival" "Matrix" "grid" "effects"
## [11] "carData" "modelsummary" "fst" "infer" "lubridate"
## [16] "forcats" "stringr" "dplyr" "purrr" "readr"
## [21] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [26] "graphics" "grDevices" "utils" "datasets" "methods"
## [31] "base"
##
## [[13]]
## [1] "interactions" "equatiomatic" "flextable" "kableExtra" "MASS"
## [6] "survey" "survival" "Matrix" "grid" "effects"
## [11] "carData" "modelsummary" "fst" "infer" "lubridate"
## [16] "forcats" "stringr" "dplyr" "purrr" "readr"
## [21] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [26] "graphics" "grDevices" "utils" "datasets" "methods"
## [31] "base"
library(fst)
ess <- read_fst("All-ESS-Data.fst")
table(ess$rlgdnm)
##
## 1 2 3 4 5 6 7 8 66 77 99
## 145427 58582 48696 6415 12155 17568 1517 1401 182675 866 1738
table(ess$ipsuces)
##
## 1 2 3 4 5 6 7 8 9
## 48710 115969 115970 82711 68279 18852 1573 5733 4692
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
ess$year[ess$essround == i] <- replacements[i]
}
netherlands_data <- ess %>%
filter(cntry == "NL") %>%
mutate(
ipsuces = case_when(
ipsuces == 1 ~ "Very much like me",
ipsuces == 2 ~ "Like me",
ipsuces == 3 ~ "Somewhat like me",
ipsuces == 4 ~ "A little like me",
ipsuces == 5 ~ "Not like me",
ipsuces == 6 ~ "Not like me at all",
ipsuces %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(ipsuces)
),
cohort = ifelse(yrbrn < 1930 | yrbrn > 2010, NA, yrbrn),
cohort = case_when(
yrbrn %in% 1930:1939 ~ 1930,
yrbrn %in% 1940:1949 ~ 1940,
yrbrn %in% 1950:1959 ~ 1950,
yrbrn %in% 1960:1969 ~ 1960,
yrbrn %in% 1970:1979 ~ 1970,
yrbrn %in% 1980:1989 ~ 1980,
yrbrn %in% 1990:1999 ~ 1990,
yrbrn %in% 2000:2009 ~ 2000,
TRUE ~ as.numeric(cohort)
),
rlgblg = ifelse(rlgblg %in% c(7, 8, 9), NA, rlgblg),
religious = case_when(
rlgblg == 2 ~ "No",
rlgblg == 1 ~ "Yes",
rlgblg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(rlgblg)
))
table(netherlands_data$cohort)
##
## 1930 1940 1950 1960 1970 1980 1990 2000
## 1663 2848 3274 3484 2921 1978 1073 250
table(netherlands_data$religious)
##
## No Yes
## 11289 7008
table(netherlands_data$ipsuces)
##
## A little like me Like me Not like me Not like me at all
## 4180 4063 2422 751
## Somewhat like me Very much like me
## 5689 860
netherlands_data_subset <- ess %>%
filter(cntry == "NL", yrbrn > 1929 & yrbrn < 2010) %>%
mutate(
rlgblg = ifelse(rlgblg == 2, 0, rlgblg),
rlgblg = ifelse(rlgblg %in% c(7, 8, 9), NA, rlgblg),
ipsuces = ifelse(ipsuces %in% c(7, 8, 9), NA, ipsuces)
) %>%
dplyr::select(ipsuces, rlgblg, yrbrn)
table(netherlands_data_subset$ipsuces)
##
## 1 2 3 4 5 6
## 824 3928 5521 3995 2237 649
table(netherlands_data_subset$rlgblg)
##
## 0 1
## 10979 6481
table(netherlands_data_subset$yrbrn)
##
## 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
## 125 155 143 140 154 177 190 160 201 218 229 216 231 275 280 253
## 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
## 378 356 332 298 325 285 325 292 394 311 375 336 295 336 324 328
## 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
## 358 361 350 310 342 362 364 385 373 358 345 282 273 250 273 284
## 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
## 250 233 219 198 218 203 204 202 213 192 179 150 142 143 122 117
## 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
## 104 105 92 93 80 75 63 48 55 46 25 13
datasummary_skim(netherlands_data_subset)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| ipsuces | 7 | 2 | 3.3 | 1.2 | 1.0 | 3.0 | 6.0 | |
| rlgblg | 3 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 | |
| yrbrn | 76 | 0 | 1962.8 | 17.4 | 1930.0 | 1962.0 | 2005.0 |
datasummary_skim(netherlands_data_subset, output = "flextable", title = "Table 1. Summary Statistics")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
ipsuces | 7 | 2 | 3.3 | 1.2 | 1.0 | 3.0 | 6.0 |
rlgblg | 3 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 |
yrbrn | 76 | 0 | 1962.8 | 17.4 | 1930.0 | 1962.0 | 2005.0 |
test_stat_v2 <- netherlands_data %>%
specify(explanatory = religious,
response = ipsuces) %>%
hypothesize(null = "independence") %>%
calculate(stat = "Chisq")
## Warning: Removed 394 rows containing missing values.
print(test_stat_v2$stat)
## X-squared
## 85.29447
null_distribution_v2 <- netherlands_data %>%
specify(explanatory = religious,
response = ipsuces) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "Chisq")
## Warning: Removed 394 rows containing missing values.
p_val <- null_distribution_v2 %>%
get_pvalue(obs_stat = test_stat_v2, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
null_distribution_v2 %>%
visualize() +
shade_p_value(obs_stat = test_stat_v2, direction = "two-sided") +
labs(
title = "Figure 1. Simulation-Based Null Distribution"
)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution_v2
## Response: ipsuces (factor)
## Explanatory: religious (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 3.07
## 2 2 3.51
## 3 3 3.20
## 4 4 1.87
## 5 5 3.83
## 6 6 3.22
## 7 7 2.04
## 8 8 6.57
## 9 9 2.39
## 10 10 0.778
## # ℹ 990 more rows
model1 <- lm(cohort ~ ipsuces, data = netherlands_data, weights = weight)
modelsummary(
list(model1),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | |
|---|---|
| ipsucesLike me | 7.1 (1.2)*** |
| ipsucesNot like me | −3.3 (1.4)* |
| ipsucesNot like me at all | −3.0 (2.0) |
| ipsucesSomewhat like me | 4.2 (1.1)*** |
| ipsucesVery much like me | 12.2 (1.9)*** |
| Num.Obs. | 1786 |
| R2 | 0.057 |
| R2 Adj. | 0.054 |
| AIC | 15204.3 |
| BIC | 15242.8 |
| Log.Lik. | −7595.174 |
| RMSE | 17.10 |
model2 <- lm(cohort ~ ipsuces + religious, data = netherlands_data, weights = weight)
modelsummary(
list(model1, model2),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | (2) | |
|---|---|---|
| ipsucesLike me | 7.1 (1.2)*** | 6.9 (1.2)*** |
| ipsucesNot like me | −3.3 (1.4)* | −3.4 (1.4)* |
| ipsucesNot like me at all | −3.0 (2.0) | −3.2 (2.0) |
| ipsucesSomewhat like me | 4.2 (1.1)*** | 3.9 (1.1)*** |
| ipsucesVery much like me | 12.2 (1.9)*** | 11.4 (1.9)*** |
| religiousYes | −3.9 (0.8)*** | |
| Num.Obs. | 1786 | 1784 |
| R2 | 0.057 | 0.069 |
| R2 Adj. | 0.054 | 0.065 |
| AIC | 15204.3 | 15165.5 |
| BIC | 15242.8 | 15209.3 |
| Log.Lik. | −7595.174 | −7574.727 |
| RMSE | 17.10 | 16.99 |
models <- list(
"Model 1" = lm(cohort ~ ipsuces, data = netherlands_data, weights = weight),
"Model 2" = lm(cohort ~ ipsuces + religious, data = netherlands_data, weights = weight),
"Model 3" = lm(cohort ~ ipsuces + religious + ipsuces*religious, data = netherlands_data, weights = weight))
modelplot(models, coef_omit = 'Interc')
modelsummary(models, fmt = 1,
estimate = c("{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_rename = c("religious1" = "religious"),
title = 'Table 2. Regression models by religious status')
| Model 1 | Model 2 | Model 3 | |
|---|---|---|---|
| (Intercept) | 1955.9 (0.8)*** | 1957.6 (0.9)*** | 1957.9 (1.1)*** |
| ipsucesLike me | 7.1 (1.2)*** | 6.9 (1.2)*** | 5.6 (1.5)*** |
| ipsucesNot like me | −3.3 (1.4)* | −3.4 (1.4)* | −2.3 (1.8) |
| ipsucesNot like me at all | −3.0 (2.0) | −3.2 (2.0) | −2.8 (2.5) |
| ipsucesSomewhat like me | 4.2 (1.1)*** | 3.9 (1.1)*** | 3.3 (1.4)* |
| ipsucesVery much like me | 12.2 (1.9)*** | 11.4 (1.9)*** | 11.3 (2.3)*** |
| religiousYes | −3.9 (0.8)*** | −4.6 (1.6)** | |
| ipsucesLike me:religiousYes | 3.6 (2.4) | ||
| ipsucesNot like me:religiousYes | −2.6 (2.8) | ||
| ipsucesNot like me at all:religiousYes | −1.2 (4.1) | ||
| ipsucesSomewhat like me:religiousYes | 1.3 (2.2) | ||
| ipsucesVery much like me:religiousYes | 0.0 (4.5) | ||
| Num.Obs. | 1786 | 1784 | 1784 |
| R2 | 0.057 | 0.069 | 0.071 |
| R2 Adj. | 0.054 | 0.065 | 0.065 |
| AIC | 15204.3 | 15165.5 | 15170.2 |
| BIC | 15242.8 | 15209.3 | 15241.6 |
| Log.Lik. | −7595.174 | −7574.727 | −7572.114 |
| RMSE | 17.10 | 16.99 | 16.96 |
netherlands_data_subset <- netherlands_data_subset %>%
mutate(
ipsuces = case_when(
ipsuces == 1 ~ "Very much like me",
ipsuces == 2 ~ "Like me",
ipsuces == 3 ~ "Somewhat like me",
ipsuces == 4 ~ "A little like me",
ipsuces == 5 ~ "Not like me",
ipsuces == 6 ~ "Not like me at all",
ipsuces %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(ipsuces)
),
cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn),
cohort = case_when(
yrbrn %in% 1930:1939 ~ 1930,
yrbrn %in% 1940:1949 ~ 1940,
yrbrn %in% 1950:1959 ~ 1950,
yrbrn %in% 1960:1969 ~ 1960,
yrbrn %in% 1970:1979 ~ 1970,
yrbrn %in% 1980:1989 ~ 1980,
yrbrn %in% 1990:1999 ~ 1990,
yrbrn %in% 2000:2009 ~ 2000,
TRUE ~ as.numeric(yrbrn)),
)
avg_ipsuces_by_religious <- aggregate(rlgblg ~ ipsuces + cohort, data=netherlands_data_subset, mean)
p1 <- ggplot(avg_ipsuces_by_religious, aes(
x=rlgblg,
y=reorder(cohort, -rlgblg),
color=as.factor(ipsuces))) +
geom_line(aes(group=ipsuces)) +
labs(title="Table 3. Mean Importance of Being Successful",
subtitle = "by Religious or Not Religious in the Netherlands",
x="Mean of rlgblg",
y="Cohort") +
scale_color_discrete(name="Importance of Success", labels=c("Very much like me", "Like me", "Somewhat like me", "A little like me", "Not like me", "Not like me at all")) +
theme_minimal()
p1
netherlands_data_subset <- netherlands_data_subset %>%
mutate(
cohort = case_when(
yrbrn %in% 1930:1939 ~ 1930,
yrbrn %in% 1940:1949 ~ 1940,
yrbrn %in% 1950:1959 ~ 1950,
yrbrn %in% 1960:1969 ~ 1960,
yrbrn %in% 1970:1979 ~ 1970,
yrbrn %in% 1980:1989 ~ 1980,
yrbrn %in% 1990:1999 ~ 1990,
yrbrn %in% 2000:2009 ~ 2000,
TRUE ~ as.numeric(yrbrn)),
)
table(netherlands_data_subset$cohort)
##
## 1930 1940 1950 1960 1970 1980 1990 2000
## 1663 2848 3274 3484 2921 1978 1073 250
median_ipsuces_by_religious <- aggregate(rlgblg ~ ipsuces + cohort, data=netherlands_data_subset, median)
median_ipsuces_by_cohort <- aggregate(cohort ~ ipsuces + rlgblg, data=netherlands_data_subset, median)
median_religion_by_ipsuces <- aggregate(cohort ~ rlgblg + ipsuces, data=netherlands_data_subset, median)
median_cohort_by_ipsuces <- aggregate(rlgblg ~ cohort + ipsuces, data=netherlands_data_subset, median)
avg_ipsuces_by_cohort <- aggregate(cohort ~ ipsuces + rlgblg, data=netherlands_data_subset, mean)
avg_ipsuces_by_religious <- aggregate(rlgblg ~ ipsuces + cohort, data=netherlands_data_subset, mean)