#Read data
ess <- read_fst("All-ESS-Data.fst")
#Importing Iceland data
Ic_data <- ess %>%
filter(cntry == "IS")
#Create a new column called Year
Ic_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
Ic_data$year[Ic_data$essround == i] <- replacements[i]
}
#Fetch the desired dataset
IC_data_table_subset <- Ic_data %>%
mutate(
brncntr = ifelse(brncntr %in% 1:2, brncntr, NA),
stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
edlvdis = ifelse(edlvdis %in% 1:16, edlvdis, NA)
)
summary_table <- datasummary_skim(IC_data_table_subset %>% select(stfdem,brncntr,edlvdis), output = "flextable", title = "Summary Table of Iceland")
## 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 |
|---|---|---|---|---|---|---|---|
stfdem | 12 | 3 | 6.0 | 2.2 | 0.0 | 7.0 | 10.0 |
brncntr | 3 | 0 | 1.1 | 0.2 | 1.0 | 1.0 | 2.0 |
edlvdis | 17 | 15 | 8.9 | 4.2 | 1.0 | 8.0 | 16.0 |
#Number of data displayed
dim(IC_data_table_subset[,c("brncntr","stfdem","edlvdis")])
## [1] 3975 3
#The first value represents 3975 rows
#The second value is for 3 columns
#Annual average of democratic satisfaction
stfdem_by_year <- IC_data_table_subset %>%
group_by(year) %>%
summarize(mean_stfdem = mean(stfdem, na.rm = TRUE))
stfdem_by_year
## # A tibble: 5 × 2
## year mean_stfdem
## <dbl> <dbl>
## 1 2004 5.86
## 2 2012 5.73
## 3 2016 5.81
## 4 2018 6.00
## 5 2020 6.57
ggplot(stfdem_by_year, aes(x = year, y = mean_stfdem)) +
geom_line(color = "blue", size = 1) + # Line to show the trend
geom_point(color = "red", size = 3) + # Points to highlight each year's value
labs(title = "stfdem in iceland (2002-2020)",
x = "Survey Year",
y = "stfdem (0-10)") +
ylim(0, 10) + # Setting the y-axis limits from 0 to 10
theme_minimal()
## 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.
#Annual average of average level of education
edlvdis_by_year <- IC_data_table_subset %>%
group_by(year) %>%
summarize(mean_edlvdis = mean(edlvdis, na.rm = TRUE))
edlvdis_by_year
## # A tibble: 5 × 2
## year mean_edlvdis
## <dbl> <dbl>
## 1 2004 NaN
## 2 2012 8.06
## 3 2016 8.86
## 4 2018 9.25
## 5 2020 9.21
ggplot(edlvdis_by_year[2:5,], aes(x = year, y = mean_edlvdis)) +
geom_line(color = "blue", size = 1) + # Line to show the trend
geom_point(color = "red", size = 3) + # Points to highlight each year's value
labs(title = "edlvdis in iceland (2002-2020)",
x = "Survey Year",
y = "edlvdis (0-16)") +
ylim(0, 16) + # Setting the y-axis limits from 0 to 10
theme_minimal()
#Table of Satisfaction with Democracy Across Survey Years For Iceland
IC_data_table_subset %>%
ggplot(aes(x = stfdem, y = as.factor(year), fill = as.factor(year))) +
geom_density_ridges() + # geometry used for visualizing
labs(title = "Satisfaction with Democracy Across Survey Years For Iceland",
x = "Satisfaction (0-10)", y = "Survey Year",
fill = "Survey Year") +
theme_minimal()
## Picking joint bandwidth of 0.498
## Warning: Removed 129 rows containing non-finite values
## (`stat_density_ridges()`).
IC_data_table_subset %>%
ggplot(aes(x = edlvdis, y = as.factor(year), fill = as.factor(year))) +
geom_density_ridges() + # geometry used for visualizing
labs(title = "edlvdis Years For Iceland",
x = "Satisfaction (0-10)", y = "Survey Year",
fill = "Survey Year") +
theme_minimal()
## Picking joint bandwidth of 0.978
## Warning: Removed 598 rows containing non-finite values
## (`stat_density_ridges()`).
# renaming to born_in_country and recoding to Y/N
IC_data_table_subset <- IC_data_table_subset %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
# always check what you did
table(IC_data_table_subset$born_in_country)
##
## No Yes
## 255 3708
stfdemedu <- datasummary_crosstab(stfdem ~ born_in_country, data = IC_data_table_subset)
stfdemedu
| stfdem | No | Yes | All | |
|---|---|---|---|---|
| 0 | N | 4 | 82 | 86 |
| % row | 4.7 | 95.3 | 100.0 | |
| 1 | N | 2 | 72 | 74 |
| % row | 2.7 | 97.3 | 100.0 | |
| 2 | N | 9 | 155 | 164 |
| % row | 5.5 | 94.5 | 100.0 | |
| 3 | N | 9 | 265 | 274 |
| % row | 3.3 | 96.7 | 100.0 | |
| 4 | N | 9 | 271 | 280 |
| % row | 3.2 | 96.8 | 100.0 | |
| 5 | N | 27 | 461 | 488 |
| % row | 5.5 | 94.5 | 100.0 | |
| 6 | N | 32 | 503 | 535 |
| % row | 6.0 | 94.0 | 100.0 | |
| 7 | N | 47 | 786 | 833 |
| % row | 5.6 | 94.4 | 100.0 | |
| 8 | N | 61 | 720 | 781 |
| % row | 7.8 | 92.2 | 100.0 | |
| 9 | N | 21 | 221 | 243 |
| % row | 8.6 | 90.9 | 100.0 | |
| 10 | N | 16 | 72 | 88 |
| % row | 18.2 | 81.8 | 100.0 | |
| All | N | 255 | 3708 | 3975 |
| % row | 6.4 | 93.3 | 100.0 |
# Calculate mean_stfdem and mean_edlvdis by year
stfdem_by_year <- IC_data_table_subset %>%
group_by(year) %>%
summarize(mean_stfdem = mean(stfdem, na.rm = TRUE))
edlvdis_by_year <- IC_data_table_subset %>%
group_by(year) %>%
summarize(mean_edlvdis = mean(edlvdis, na.rm = TRUE))
# Merge the two datasets
combined_data <- merge(stfdem_by_year, edlvdis_by_year, by = "year")
# Reshape the data to long format
long_data <- combined_data %>%
pivot_longer(
cols = c(mean_stfdem, mean_edlvdis),
names_to = "Variable",
values_to = "Mean_Value"
)
# Plotting
ggplot(long_data, aes(x = year, y = Mean_Value, color = Variable)) +
geom_line(size = 1) +
geom_point(size = 3) +
labs(title = "The Mean of Democratic Satisfaction and Education on Iceland (2002-2020)",
x = "Survey Year",
y = "Mean Value") +
scale_color_manual(values = c("blue", "red"),
name = "Variable",
labels = c("Satisfaction", "Education")) +
theme_minimal()
## Warning: Removed 1 row containing missing values (`geom_line()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
#regression result
model1 <- lm(stfdem ~ edlvdis, data = IC_data_table_subset)
model2 <- lm(stfdem ~ edlvdis + brncntr, data = IC_data_table_subset)
model3 <- lm(stfdem ~ edlvdis + brncntr + brncntr*edlvdis, data =IC_data_table_subset)
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | (2) | (3) | |
|---|---|---|---|
| edlvdis | 0.1 (0.0)*** | 0.1 (0.0)*** | 0.1 (0.0)* |
| brncntr | 0.7 (0.2)*** | 1.0 (0.4)* | |
| edlvdis × brncntr | 0.0 (0.0) | ||
| Num.Obs. | 3283 | 3283 | 3283 |
| R2 | 0.011 | 0.017 | 0.017 |
| R2 Adj. | 0.010 | 0.016 | 0.016 |
| AIC | 14513.2 | 14495.2 | 14496.6 |
| BIC | 14531.5 | 14519.6 | 14527.1 |
| Log.Lik. | −7253.597 | −7243.610 | −7243.309 |
| RMSE | 2.20 | 2.20 | 2.20 |
interaction_plot <- effect(c("brncntr", "edlvdis"), model3)
## Warning in term == terms: longer object length is not a multiple of shorter
## object length
## NOTE: brncntredlvdis does not appear in the model
## Warning in term == names: longer object length is not a multiple of shorter
## object length
## NOTE: brncntredlvdis is not a high-order term in the model
plot(interaction_plot,
main="Interaction effect",
xlab="Educational attainment",
ylab="Democratic Satisfaction")
# set graphical parameters
par(mfrow = c(2,2)) # plot 2 X 2 matrix, by row
# plot diagnostics plots
plot(model2, pch = 19, col = scales::alpha("grey", .8), cex = .5)
# Remove NA values
IC_data_table_subset$brncntr <- as.factor(IC_data_table_subset$brncntr)
table(IC_data_table_subset$brncntr) # View counts at each level
##
## 1 2
## 3708 255
IC_data_table_subset<-na.omit(IC_data_table_subset[,c("brncntr","stfdem","edlvdis")])
# Specify the t-test using the infer pipeline
t_test_result <- IC_data_table_subset %>%
specify(response = stfdem, explanatory = brncntr) %>%
hypothesize(null = "independence") %>%
calculate(stat = "t")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "1" - "2", or divided in the order "1" / "2" for ratio-based statistics.
## To specify this order yourself, supply `order = c("1", "2")` to the calculate()
## function.
print(t_test_result$stat)
## t
## -4.746476
# Extract the observed test statistic
obs_test_stat <- t_test_result$stat
# Generate the null distribution
null_dist <- IC_data_table_subset %>%
specify(response = stfdem, explanatory = brncntr) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "t")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "1" - "2", or divided in the order "1" / "2" for ratio-based statistics.
## To specify this order yourself, supply `order = c("1", "2")` to the calculate()
## function.
null_dist
## Response: stfdem (numeric)
## Explanatory: brncntr (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.154
## 2 2 -0.648
## 3 3 -0.465
## 4 4 -0.754
## 5 5 -1.35
## 6 6 0.286
## 7 7 -0.108
## 8 8 0.435
## 9 9 -0.754
## 10 10 0.151
## # ℹ 990 more rows
# Calculate confidence intervals
conf_int <- get_confidence_interval(null_dist, level = 0.95, type = "percentile")
# Null distribution、observed t-statistics and confidence intervals for visualization simulations
null_dist_plot <- null_dist %>%
visualize(method = "simulation") +
geom_vline(aes(xintercept = obs_test_stat), color = "red", linetype = "dashed", size = 1) +
shade_confidence_interval(endpoints = conf_int) + # 添加置信区间
labs(title = "Simulation-Based Null Distribution with Confidence Interval",
subtitle = paste("Observed t-statistic:", round(obs_test_stat, 2)),
x = "t statistic",
y = "Density")
# print table
print(null_dist_plot)