packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "flextable")
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 'infer' was built under R version 4.3.2
## 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
## Warning: package 'flextable' was built under R version 4.3.2
##
## Attaching package: 'flextable'
##
## The following object is masked from 'package:purrr':
##
## compose
## [[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] "flextable" "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"
ess <- read_fst("All-ESS-Data.fst")
#Context
uk_data <- ess %>%
filter(cntry == "GB")
uk_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
uk_data$year[uk_data$essround == i] <- replacements[i]
}
uk_data <- uk_data
df <- uk_data %>%
mutate(
polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr),
netuse = ifelse(netuse %in% c(77, 88, 99), NA, netuse),
trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl)
)
df <- df %>% filter(!is.na(polintr))
df <- df %>% filter(!is.na(netuse))
df <- df %>% filter(!is.na(trstprl))
datasummary_skim(df %>%
dplyr::select(polintr, netuse, trstprl))
## 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 |
|---|---|---|---|---|---|---|---|
polintr | 4 | 0 | 2.5 | 0.9 | 1.0 | 2.0 | 4.0 |
netuse | 8 | 0 | 3.7 | 3.0 | 0.0 | 5.0 | 7.0 |
trstprl | 11 | 0 | 4.3 | 2.4 | 0.0 | 4.0 | 10.0 |
uk_data_table <- df %>%
rename(
`Interest in Politics` = polintr,
`Use of Internet` = netuse,
`Trust in Parliament` = trstprl
)
summary_table <- datasummary_skim(uk_data_table %>%
dplyr::select(`Interest in Politics`, `Use of Internet`,
`Trust in Parliament`),
output = "flextable",
title = "Table 1: Descriptive Statistics for main Variables")
## 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.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Interest in Politics | 4 | 0 | 2.5 | 0.9 | 1.0 | 2.0 | 4.0 |
Use of Internet | 8 | 0 | 3.7 | 3.0 | 0.0 | 5.0 | 7.0 |
Trust in Parliament | 11 | 0 | 4.3 | 2.4 | 0.0 | 4.0 | 10.0 |
df2 <- df %>%
mutate(
netuse = case_when(
netuse %in% 0:1 ~ "1",
netuse %in% 2:3 ~ "2",
netuse %in% 4:5 ~ "3",
netuse %in% 6:7 ~ "4",
TRUE ~ as.character(netuse)
),
netuse = factor(netuse, levels = c("1", "2", "3", "4"),
labels = c("Never Use", "Seldom Use", "Sometimes Use", "Often Use")),
politr = case_when(
polintr %in% c(1, 2) ~ "Interested",
polintr %in% c(3, 4) ~ "Not Interested",
polintr %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(polintr)
)
)
test_stat <- df2 %>%
specify(explanatory = netuse,
response = trstprl) %>%
hypothesize(null = "independence") %>%
calculate(stat = "F")
print(test_stat$stat)
## [1] 29.63611
null_distribution <- df2 %>%
specify(trstprl ~ netuse) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "F")
null_distribution
## Response: trstprl (numeric)
## Explanatory: netuse (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.278
## 2 2 0.172
## 3 3 2.88
## 4 4 0.266
## 5 5 1.01
## 6 6 0.139
## 7 7 0.104
## 8 8 0.577
## 9 9 2.02
## 10 10 0.684
## # ℹ 990 more rows
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "greater")
## 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
conf_int <- null_distribution%>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize(data, bins = 10, method = "simulation", dens_color = "balck") +
shade_p_value(obs_stat = test_stat, direction = "greater") +
shade_confidence_interval(endpoints = conf_int)
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf
test_stat <- 29.63611
confidence_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize(bins = 15, method = "simulation", dens_color = "black") +
geom_vline(aes(xintercept = test_stat), color = "red", linetype = "dashed", size = 1) +
shade_p_value(obs_stat = test_stat, direction = "greater") +
shade_confidence_interval(endpoints = confidence_int) +
coord_cartesian(xlim = c(0, max(null_distribution$stat) * 0.9)) +
labs(title = "Simulation-Based Null Distribution",
x = "F statistic",
y = "Count")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf
null_distribution
## Response: trstprl (numeric)
## Explanatory: netuse (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.278
## 2 2 0.172
## 3 3 2.88
## 4 4 0.266
## 5 5 1.01
## 6 6 0.139
## 7 7 0.104
## 8 8 0.577
## 9 9 2.02
## 10 10 0.684
## # ℹ 990 more rows
model1 <- lm(trstprl ~ netuse, data = df2)
model2 <- lm(trstprl ~ netuse + polintr, data = df2)
model3 <- lm(trstprl ~ netuse + polintr + netuse*polintr, data = df2)
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c("{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_rename = c("rlgblgYes" = "Belonging to a Relgion"),
title = 'Table 2: Regression models predicting Trust in Parliament (scale 0 to 10)')
| (1) | (2) | (3) |
|---|---|---|---|
(Intercept) | 4.0 (0.0)*** | 5.6 (0.1)*** | 5.6 (0.1)*** |
netuseSeldom Use | 0.1 (0.1) | -0.1 (0.1) | -0.4 (0.3) |
netuseSometimes Use | 0.2 (0.1)** | 0.1 (0.1) | -0.1 (0.3) |
netuseOften Use | 0.5 (0.0)*** | 0.3 (0.0)*** | 0.2 (0.1) |
polintr | -0.6 (0.0)*** | -0.6 (0.0)*** | |
netuseSeldom Use:polintr | 0.1 (0.1) | ||
netuseSometimes Use:polintr | 0.1 (0.1) | ||
netuseOften Use:polintr | 0.0 (0.1) | ||
Num.Obs. | 10905 | 10905 | 10905 |
R2 | 0.008 | 0.053 | 0.054 |
R2 Adj. | 0.008 | 0.053 | 0.053 |
AIC | 50009.7 | 49502.0 | 49505.8 |
BIC | 50046.1 | 49545.8 | 49571.5 |
Log.Lik. | -24999.827 | -24744.991 | -24743.892 |
RMSE | 2.40 | 2.34 | 2.34 |
interaction_plot <- effect("netuse*polintr", model3, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect",
xlab="Educational attainment",
ylab="Authoritarian attitudes scale")
interaction_plot
##
## netuse*polintr effect
## polintr
## netuse 1 1.8 2.5 3.2 4
## Never Use 5.035388 4.563726 4.151022 3.738317 3.266655
## Seldom Use 4.766148 4.406795 4.092362 3.777929 3.418576
## Sometimes Use 5.004662 4.618493 4.280596 3.942699 3.556530
## Often Use 5.269553 4.811164 4.410074 4.008984 3.550595