packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "aod", "interactions", "kableExtra", "flextable", "scales", "ggeffects", "marginaleffects", "viridis", "knitr", "rmarkdown", "ggridges", "questionr")
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.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ 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
##
##
##
## Attaching package: 'aod'
##
##
## The following object is masked from 'package:survival':
##
## rats
##
##
##
## 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
##
##
##
## Attaching package: 'scales'
##
##
## The following object is masked from 'package:purrr':
##
## discard
##
##
## The following object is masked from 'package:readr':
##
## col_factor
##
##
##
## Attaching package: 'ggeffects'
##
##
## The following object is masked from 'package:interactions':
##
## johnson_neyman
##
##
## Loading required package: viridisLite
##
##
## Attaching package: 'viridis'
##
##
## The following object is masked from 'package:scales':
##
## viridis_pal
## [[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] "aod" "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] "interactions" "aod" "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] "kableExtra" "interactions" "aod" "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] "flextable" "kableExtra" "interactions" "aod" "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"
##
## [[12]]
## [1] "scales" "flextable" "kableExtra" "interactions" "aod"
## [6] "MASS" "survey" "survival" "Matrix" "grid"
## [11] "effects" "carData" "modelsummary" "fst" "infer"
## [16] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [21] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [26] "stats" "graphics" "grDevices" "utils" "datasets"
## [31] "methods" "base"
##
## [[13]]
## [1] "ggeffects" "scales" "flextable" "kableExtra" "interactions"
## [6] "aod" "MASS" "survey" "survival" "Matrix"
## [11] "grid" "effects" "carData" "modelsummary" "fst"
## [16] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [21] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [26] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [31] "datasets" "methods" "base"
##
## [[14]]
## [1] "marginaleffects" "ggeffects" "scales" "flextable"
## [5] "kableExtra" "interactions" "aod" "MASS"
## [9] "survey" "survival" "Matrix" "grid"
## [13] "effects" "carData" "modelsummary" "fst"
## [17] "infer" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr"
## [25] "tibble" "ggplot2" "tidyverse" "stats"
## [29] "graphics" "grDevices" "utils" "datasets"
## [33] "methods" "base"
##
## [[15]]
## [1] "viridis" "viridisLite" "marginaleffects" "ggeffects"
## [5] "scales" "flextable" "kableExtra" "interactions"
## [9] "aod" "MASS" "survey" "survival"
## [13] "Matrix" "grid" "effects" "carData"
## [17] "modelsummary" "fst" "infer" "lubridate"
## [21] "forcats" "stringr" "dplyr" "purrr"
## [25] "readr" "tidyr" "tibble" "ggplot2"
## [29] "tidyverse" "stats" "graphics" "grDevices"
## [33] "utils" "datasets" "methods" "base"
##
## [[16]]
## [1] "knitr" "viridis" "viridisLite" "marginaleffects"
## [5] "ggeffects" "scales" "flextable" "kableExtra"
## [9] "interactions" "aod" "MASS" "survey"
## [13] "survival" "Matrix" "grid" "effects"
## [17] "carData" "modelsummary" "fst" "infer"
## [21] "lubridate" "forcats" "stringr" "dplyr"
## [25] "purrr" "readr" "tidyr" "tibble"
## [29] "ggplot2" "tidyverse" "stats" "graphics"
## [33] "grDevices" "utils" "datasets" "methods"
## [37] "base"
##
## [[17]]
## [1] "rmarkdown" "knitr" "viridis" "viridisLite"
## [5] "marginaleffects" "ggeffects" "scales" "flextable"
## [9] "kableExtra" "interactions" "aod" "MASS"
## [13] "survey" "survival" "Matrix" "grid"
## [17] "effects" "carData" "modelsummary" "fst"
## [21] "infer" "lubridate" "forcats" "stringr"
## [25] "dplyr" "purrr" "readr" "tidyr"
## [29] "tibble" "ggplot2" "tidyverse" "stats"
## [33] "graphics" "grDevices" "utils" "datasets"
## [37] "methods" "base"
##
## [[18]]
## [1] "ggridges" "rmarkdown" "knitr" "viridis"
## [5] "viridisLite" "marginaleffects" "ggeffects" "scales"
## [9] "flextable" "kableExtra" "interactions" "aod"
## [13] "MASS" "survey" "survival" "Matrix"
## [17] "grid" "effects" "carData" "modelsummary"
## [21] "fst" "infer" "lubridate" "forcats"
## [25] "stringr" "dplyr" "purrr" "readr"
## [29] "tidyr" "tibble" "ggplot2" "tidyverse"
## [33] "stats" "graphics" "grDevices" "utils"
## [37] "datasets" "methods" "base"
##
## [[19]]
## [1] "questionr" "ggridges" "rmarkdown" "knitr"
## [5] "viridis" "viridisLite" "marginaleffects" "ggeffects"
## [9] "scales" "flextable" "kableExtra" "interactions"
## [13] "aod" "MASS" "survey" "survival"
## [17] "Matrix" "grid" "effects" "carData"
## [21] "modelsummary" "fst" "infer" "lubridate"
## [25] "forcats" "stringr" "dplyr" "purrr"
## [29] "readr" "tidyr" "tibble" "ggplot2"
## [33] "tidyverse" "stats" "graphics" "grDevices"
## [37] "utils" "datasets" "methods" "base"
ess <- read.fst("All-ESS-Data.fst")
croatia_data <- ess %>%
filter(cntry == "HR")
croatia_data$weight <- croatia_data$dweight * croatia_data$pweight
survey_design <- svydesign(ids = ~1, data = croatia_data, weights = ~weight)
croatia_descriptive <- croatia_data %>%
mutate(
impenv = case_when (
impenv == 1 ~ "Very Much Like Me",
impenv == 2 ~ "Like Me",
impenv == 3 ~ "Somewhat Like Me",
impenv == 4 ~ "A Little Like Me",
impenv == 5 ~ "Not Like Me",
impenv == 6 ~ "Not Like Me At All",
impenv %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(impenv)
)) %>%
filter (!is.na(impenv))
croatia_descriptive <- croatia_data %>%
mutate(
impenv = ifelse(impenv %in% c(7, 8, 9), NA, impenv)
) %>%
filter(!is.na(impenv))
croatia_descriptive <- croatia_descriptive %>%
mutate(
lrscale = ifelse(lrscale %in% c(77,88,99), NA, lrscale)) %>%
filter(!is.na(lrscale))
croatia_descriptive <- croatia_descriptive %>%
mutate(
gndr = ifelse(gndr %in% c(9), NA, gndr)
) %>%
filter(!is.na(gndr))
croatia_descriptive <- croatia_descriptive %>%
mutate(
Environment = impenv,
PoliticalID = lrscale,
Gender = gndr
)
croat_table1 <- datasummary_skim(croatia_descriptive %>% dplyr::select(Environment, PoliticalID, Gender), title = "Table 1. Descriptive statistics of environmental attitudes, political ideology, and gender", output = "flextable")
## 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.
croat_table1
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Environment | 6 | 0 | 2.1 | 1.1 | 1.0 | 2.0 | 6.0 |
PoliticalID | 11 | 0 | 5.0 | 2.5 | 0.0 | 5.0 | 10.0 |
Gender | 2 | 0 | 1.5 | 0.5 | 1.0 | 2.0 | 2.0 |
flextable::save_as_docx(croat_table1, path = "Croat_Table1.docx", width = 7.0, height = 9.0)
croatia_null <- croatia_data %>%
mutate(
impenv = case_when (
impenv == 1 ~ "Very Much Like Me",
impenv == 2 ~ "Like Me",
impenv == 3 ~ "Somewhat Like Me",
impenv == 4 ~ "A Little Like Me",
impenv == 5 ~ "Not Like Me",
impenv == 6 ~ "Not Like Me At All",
impenv %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(impenv)
)) %>%
filter (!is.na(impenv))
croatia_null <- croatia_null %>%
mutate(
lrscale = case_when(
lrscale %in% 0:4 ~ "Left",
lrscale %in% 6:10 ~ "Right",
lrscale %in% c(5, 77, 88, 99) ~ NA_character_)
) %>%
filter(!is.na(lrscale))
croatia_null <- croatia_null %>%
mutate (gndr = case_when (
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr %in% c(9) ~ NA_character_,
)) %>%
filter(!is.na(gndr))
test_stat1 <- croatia_null %>%
specify(explanatory = lrscale,
response = impenv) %>%
hypothesize (null = "independence") %>%
calculate (stat = "Chisq")
print(test_stat1$stat)
## X-squared
## 13.63082
A chi-square test was used in this case for the test statistic because impenv, the response variable, is a categorical variable with six categories and lrscale, the explanatory variable, is a categorical variable with only two categories As a result, the other test statistics would not fit for these types of categorical variables for the test statistic since there are no numeric variables and both variables do not have only two categories.
croat_tab <- croatia_null
HRtab <- table(croat_tab$lrscale, croat_tab$impenv)
rsum <- rowSums(HRtab)
csums <- colSums(HRtab)
N <- sum(HRtab)
ptab <- tcrossprod(rsum/N, csums/N)
cat("Table of Expected Proportions:\n")
## Table of Expected Proportions:
print(ptab)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.04123718 0.1882107 0.01419888 0.003776299 0.08322963 0.1723503
## [2,] 0.04074480 0.1859634 0.01402934 0.003731209 0.08223584 0.1702924
ftab <- N*ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 137.3198 626.7417 47.28228 12.57508 277.1547 573.9264
## [2,] 135.6802 619.2583 46.71772 12.42492 273.8453 567.0736
alpha <- 0.05
c_val <- qchisq(alpha, df = 1, lower.tail = FALSE)
cat("Critical Value:", round(c_val, 3), "\n")
## Critical Value: 3.841
null_distribution <- croatia_null %>%
specify(explanatory = lrscale,
response = impenv) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat="Chisq")
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat1, direction = "two-sided")
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.046
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat1, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: impenv (factor)
## Explanatory: lrscale (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 6.73
## 2 2 4.59
## 3 3 7.11
## 4 4 2.06
## 5 5 8.30
## 6 6 7.78
## 7 7 1.35
## 8 8 5.50
## 9 9 2.05
## 10 10 7.96
## # ℹ 990 more rows
croatia_model1 <- croatia_data %>%
mutate(impenv = na_if(impenv, 7) %>% na_if(8) %>% na_if(9)) %>%
mutate(impenv = 7 - impenv) %>%
filter(!is.na(impenv))
croatia_model1 <- croatia_model1 %>%
mutate(
lrscale = case_when(
lrscale %in% 0:4 ~ "Left",
lrscale %in% 6:10 ~ "Right",
lrscale %in% c(5, 77, 88, 99) ~ NA_character_
)) %>%
filter(!is.na(lrscale))
croatia_model1 <- croatia_model1 %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr %in% c(9) ~ NA_character_,
TRUE ~ as.character(gndr)
)) %>%
filter(!is.na(gndr))
model1 <- lm(impenv ~ lrscale, data = croatia_model1, weights = weight)
modelsummary(
list(model1),
fmt = 3,
estimate = c("{estimate} ({std.error}){stars}"),
statistic = NULL
)
| Â (1) | |
|---|---|
| (Intercept) | 4.908 (0.027)*** |
| lrscaleRight | −0.047 (0.038) |
| Num.Obs. | 3330 |
| R2 | 0.000 |
| R2 Adj. | 0.000 |
| AIC | 10401.4 |
| BIC | 10419.8 |
| Log.Lik. | −5197.715 |
| RMSE | 1.10 |
model2 <- lm(impenv ~ lrscale + gndr, data = croatia_model1, weights = weight)
model3 <- lm(impenv ~ lrscale + gndr + lrscale*gndr, data = croatia_model1, weights = weight)
modelsummary(
list(model1, model2, model3),
fmt = 3,
estimate = c("{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
title = "Table 2: Regression model for Environmental Attitude, Politicla Ideology, and Gender in Croatia")
| Â (1) | Â Â (2) | Â Â (3) | |
|---|---|---|---|
| (Intercept) | 4.908 (0.027)*** | 4.967 (0.032)*** | 4.978 (0.036)*** |
| lrscaleRight | −0.047 (0.038) | −0.033 (0.038) | −0.057 (0.053) |
| gndrMale | −0.135 (0.038)*** | −0.160 (0.054)** | |
| lrscaleRight × gndrMale | 0.050 (0.076) | ||
| Num.Obs. | 3330 | 3330 | 3330 |
| R2 | 0.000 | 0.004 | 0.004 |
| R2 Adj. | 0.000 | 0.004 | 0.003 |
| AIC | 10401.4 | 10390.9 | 10392.5 |
| BIC | 10419.8 | 10415.4 | 10423.0 |
| Log.Lik. | −5197.715 | −5191.458 | −5191.240 |
| RMSE | 1.10 | 1.09 | 1.09 |
This model has lower AIC and BIC scores and will be used in the report.
model1.2 <- glm(impenv ~ lrscale, data = croatia_model1, family = poisson)
model2.2 <- glm(impenv ~ lrscale + gndr, data = croatia_model1, family = poisson)
model3.2 <- glm(impenv ~ lrscale + gndr + lrscale*gndr, data = croatia_model1, family = poisson)
modelsummary(
list(model1.2, model2.2, model3.2),
fmt = 3,
estimate = c("{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL
)
| Â (1) | Â Â (2) | Â Â (3) | |
|---|---|---|---|
| (Intercept) | 1.596 (0.011)*** | 1.610 (0.013)*** | 1.613 (0.015)*** |
| lrscaleRight | −0.013 (0.016) | −0.011 (0.016) | −0.017 (0.022) |
| gndrMale | −0.033 (0.016)* | −0.040 (0.022)+ | |
| lrscaleRight × gndrMale | 0.013 (0.032) | ||
| Num.Obs. | 3330 | 3330 | 3330 |
| AIC | 12345.2 | 12342.7 | 12344.5 |
| BIC | 12357.4 | 12361.0 | 12368.9 |
| Log.Lik. | −6170.578 | −6168.336 | −6168.247 |
| RMSE | 1.09 | 1.09 | 1.09 |
croatia_plot <- croatia_data %>%
mutate(
impenv = case_when(
impenv %in% c(7,8,9) ~ NA_character_,
TRUE ~ as.character(impenv))
) %>%
filter (!is.na(impenv))
croatia_plot <- croatia_plot %>%
mutate(
lrscale = case_when(
lrscale %in% 0:4 ~ "Left",
lrscale %in% 6:10 ~ "Right",
lrscale %in% c(5, 77, 88, 99) ~ NA_character_)
) %>%
filter(!is.na(lrscale))
croatia_plot <- croatia_plot %>%
mutate (gndr = case_when (
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
gndr %in% c(9) ~ NA_character_,
)) %>%
filter(!is.na(gndr))
table(croatia_plot$impenv, croatia_plot$lrscale) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Importance of Caring for the Environment in Croatia by Political Ideology",
y = "Conditional Percentage",
x = "Very much - to - Not like me at all",
fill = "Political Ideology")
table(croatia_plot$impenv, croatia_plot$gndr) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Importance of Caring for the Environment in Croatia by Gender",
y = "Conditional Percentage",
x = "Very much - to - Not like me at all",
fill = "Gender")