packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "effects")
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
## Loading required package: viridisLite
## Warning: package 'knitr' was built under R version 4.3.2
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## Warning: package 'effects' was built under R version 4.3.2
## 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] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[13]]
## [1] "effects" "carData" "questionr" "ggridges" "rmarkdown"
## [6] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [11] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
setwd("~/SOC202 Documents/tutorial")
getwd()
## [1] "C:/Users/Adrien/Documents/SOC202 Documents/tutorial"
ess <- read_fst("All-ESS-Data.fst")
The variable for stfeco was converted into stfecosc for purposes of the linear regression model and to categorize the ranges of views on the economy into smaller sets from 11 to 3 for ease of understanding.
table(ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
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]
}
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
atchctr = ifelse(atchctr %in% c(77, 88, 99), NA, atchctr),
stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco),
impcntr = ifelse(impcntr %in% c(7, 8, 9), NA, impcntr),
)
france_data <- france_data %>%
filter(cntry == "FR") %>%
mutate(
stfecosc = case_when(
stfeco %in% 0:2 ~ 1,
stfeco %in% 3:6 ~ 2,
stfeco %in% 7:10 ~ 3,
))
table(france_data$atchctr)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 42 15 63 75 101 373 339 784 1569 1069 1613
table(france_data$stfecosc)
##
## 1 2 3
## 5963 11416 1388
table(france_data$impcntr)
##
## 1 2 3 4
## 2063 8033 5916 2603
france_clean <- france_data %>%
filter(!is.na(atchctr) & !is.na(stfecosc) & !is.na(impcntr))
datasummary_skim(france_data %>% select(atchctr, stfecosc, impcntr))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| atchctr | 12 | 68 | 8.0 | 1.9 | 0.0 | 8.0 | 10.0 | |
| stfecosc | 4 | 1 | 1.8 | 0.6 | 1.0 | 2.0 | 3.0 | |
| impcntr | 5 | 2 | 2.5 | 0.9 | 1.0 | 2.0 | 4.0 |
Model 1 (Simulated Null-Distribution)
france_clean <- france_clean %>%
filter(cntry == "FR") %>%
mutate(
stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco),
impcntr = ifelse(impcntr %in% c(7, 8, 9), NA, impcntr),
atchctr = ifelse(atchctr %in% c(77, 88, 99), NA, atchctr))
france_clean <- france_clean %>%
filter(cntry == "FR") %>%
mutate(
stfecosc = case_when(
stfeco %in% 0:2 ~ "Dissatisfied",
stfeco %in% 3:6 ~ "Indifferent",
stfeco %in% 7:10 ~ "Satisfied",
))
df <- france_clean
library(infer)
test_stat <- df %>%
specify(explanatory = atchctr,
response = impcntr) %>%
hypothesize(null = "independence") %>%
calculate(stat = "slope")
## Message: The independence null hypothesis does not inform calculation of the
## observed statistic (a slope) and will be ignored.
print(test_stat$stat)
## atchctr
## 0.01861647
null_dist <- df %>%
specify(impcntr ~ atchctr) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "slope")
null_dist
## Response: impcntr (numeric)
## Explanatory: atchctr (numeric)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.00192
## 2 2 0.0143
## 3 3 -0.00299
## 4 4 -0.00756
## 5 5 0.000426
## 6 6 0.0104
## 7 7 -0.00713
## 8 8 -0.00270
## 9 9 0.000618
## 10 10 -0.00304
## # ℹ 990 more rows
p_val <- null_dist %>% #
get_pvalue(obs_stat = test_stat, direction = "greater")
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.001
conf_int <- null_dist%>%
get_confidence_interval(level = 0.95, type = "percentile")
null_dist %>%
visualize(data, bins = 10, method = "simulation", dens_color = "black") +
shade_p_value(obs_stat = test_stat, direction = "greater") +
shade_confidence_interval(endpoints = conf_int)
Model 2 (Regression)
df$weight <- df$dweight * df$pweight
model1 <- lm(impcntr ~ atchctr, data = df, weights = weight)
model2 <- lm(impcntr ~ atchctr + stfecosc, data = df, weights = weight)
model3 <- lm(impcntr ~ atchctr * stfecosc, data = df, weights = weight)
modelsummary(
list(model1, model2, model3),
title = "Regression Model Summary: Views on Immigration in relation to Emotional Attachment to France and Satisfaction with the Economy",
fmt = 1,
estimate = c("{estimate} ({std.error}){stars}"),
statistic = NULL,
)
| (1) | (2) | (3) | |
|---|---|---|---|
| (Intercept) | 2.1 (0.0)*** | 2.3 (0.1)*** | 2.5 (0.1)*** |
| atchctr | 0.0 (0.0)** | 0.0 (0.0)*** | 0.0 (0.0) |
| stfecoscIndifferent | −0.3 (0.0)*** | −0.5 (0.1)*** | |
| stfecoscSatisfied | −0.5 (0.0)*** | −0.8 (0.2)*** | |
| atchctr × stfecoscIndifferent | 0.0 (0.0)* | ||
| atchctr × stfecoscSatisfied | 0.0 (0.0) | ||
| Num.Obs. | 5761 | 5761 | 5761 |
| R2 | 0.001 | 0.034 | 0.035 |
| R2 Adj. | 0.001 | 0.033 | 0.034 |
| AIC | 15054.6 | 14868.7 | 14866.1 |
| BIC | 15074.6 | 14902.0 | 14912.7 |
| Log.Lik. | −7524.289 | −7429.370 | −7426.043 |
| F | 8.431 | 67.192 | 41.678 |
| RMSE | 0.87 | 0.86 | 0.86 |
Model 3 (Interaction)
library(effects)
interaction_plot <- effect("atchctr * stfecosc", model3, na.rm=TRUE)
plot(interaction_plot,
main = "Interaction model",
xlab="Attachment to France",
ylab= "Views on Immigration")
interaction_plot
##
## atchctr*stfecosc effect
## stfecosc
## atchctr Dissatisfied Indifferent Satisfied
## 0 2.471921 1.928126 1.670473
## 2 2.485875 2.003782 1.756171
## 5 2.506807 2.117265 1.884719
## 8 2.527739 2.230749 2.013268
## 10 2.541694 2.306405 2.098966
equatiomatic::extract_eq(model3, use_coefs = TRUE)
\[ \operatorname{\widehat{impcntr}} = 2.47 + 0.01(\operatorname{atchctr}) - 0.54(\operatorname{stfecosc}_{\operatorname{Indifferent}}) - 0.8(\operatorname{stfecosc}_{\operatorname{Satisfied}}) + 0.03(\operatorname{atchctr} \times \operatorname{stfecosc}_{\operatorname{Indifferent}}) + 0.04(\operatorname{atchctr} \times \operatorname{stfecosc}_{\operatorname{Satisfied}}) \] The findings for our equation show that atchctr has very little effect on impcntr even in when placed in an interaction with stfecosc. Rather, it highlights the significance of stfecosc in relation to impcntr. It shows that indifference and a satisfied outlook on the current economy has an influence on pro-immigration sentiments.