packages <- c("tidyverse", "infer", "modelsummary", "effects", "survey", "interactions", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "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.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: 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
##
##
## Loading required package: viridisLite
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## [[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] "modelsummary" "infer" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[4]]
## [1] "effects" "carData" "modelsummary" "infer" "lubridate"
## [6] "forcats" "stringr" "dplyr" "purrr" "readr"
## [11] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [16] "graphics" "grDevices" "utils" "datasets" "methods"
## [21] "base"
##
## [[5]]
## [1] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "modelsummary" "infer" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "interactions" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "modelsummary" "infer" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[7]]
## [1] "interactions" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "modelsummary" "infer" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[8]]
## [1] "RColorBrewer" "interactions" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "modelsummary" "infer"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[9]]
## [1] "fst" "RColorBrewer" "interactions" "survey" "survival"
## [6] "Matrix" "grid" "effects" "carData" "modelsummary"
## [11] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[10]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "interactions"
## [6] "survey" "survival" "Matrix" "grid" "effects"
## [11] "carData" "modelsummary" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[11]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "interactions" "survey" "survival" "Matrix" "grid"
## [11] "effects" "carData" "modelsummary" "infer" "lubridate"
## [16] "forcats" "stringr" "dplyr" "purrr" "readr"
## [21] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [26] "graphics" "grDevices" "utils" "datasets" "methods"
## [31] "base"
##
## [[12]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "interactions" "survey" "survival" "Matrix"
## [11] "grid" "effects" "carData" "modelsummary" "infer"
## [16] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [21] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [26] "stats" "graphics" "grDevices" "utils" "datasets"
## [31] "methods" "base"
##
## [[13]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "interactions" "survey" "survival"
## [11] "Matrix" "grid" "effects" "carData" "modelsummary"
## [16] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [21] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [26] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [31] "datasets" "methods" "base"
##
## [[14]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "interactions" "survey"
## [11] "survival" "Matrix" "grid" "effects" "carData"
## [16] "modelsummary" "infer" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [26] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [31] "utils" "datasets" "methods" "base"
##
## [[15]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "interactions" "survey"
## [11] "survival" "Matrix" "grid" "effects" "carData"
## [16] "modelsummary" "infer" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [26] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [31] "utils" "datasets" "methods" "base"
##
## [[16]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "interactions"
## [11] "survey" "survival" "Matrix" "grid" "effects"
## [16] "carData" "modelsummary" "infer" "lubridate" "forcats"
## [21] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [26] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [31] "grDevices" "utils" "datasets" "methods" "base"
ess <- read_fst("/Users/jasminevvvvvvv/Desktop/UT/SOC202/Homework/All-ESS-Data.fst")
Summary Statictis
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]
}
table(ess$hincfel)
##
## 1 2 3 4 7 8 9
## 137163 214732 91103 36961 1594 3727 3469
table(ess$hinctnta)
##
## 1 2 3 4 5 6 7 8 9 10 77 88 99
## 27483 31409 31414 31134 30205 28657 28297 26624 22420 22099 41851 23623 2987
table(ess$cptppola)
##
## 1 2 3 4 5 7 8 9
## 50289 48650 34647 11749 4598 531 2755 372
france_data <- ess %>% #
filter(cntry == "FR") %>%
mutate(
hincfel = ifelse(hincfel == 2, 0, ifelse(hincfel %in% c(7, 8, 9), NA, hincfel)),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
cptppola = ifelse(cptppola %in% c(7, 8, 9), NA, cptppola),
)
table(france_data$hincfel)
##
## 0 1 3 4
## 7868 4981 2469 356
table(france_data$hinctnta)
##
## 1 2 3 4 5 6 7 8 9 10
## 1531 1585 1185 1259 1238 1174 1215 1118 1032 1140
table(france_data$cptppola)
##
## 1 2 3 4 5
## 1588 2325 1661 277 138
datasummary_skim(france_data %>% select(hincfel, hinctnta, cptppola))
Unique (#)
Missing (%)
Mean
SD
Min
Median
Max
hincfel
5
18
0.9
1.1
0.0
0.0
4.0
hinctnta
11
34
5.2
2.9
1.0
5.0
10.0
cptppola
6
69
2.2
1.0
1.0
2.0
5.0
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
hincfel = case_when(
hincfel == 1 ~ "Living comfortably on present income",
hincfel == 2 ~ "Coping on present income",
hincfel == 3 ~ "Difficult on present income",
hincfel == 4 ~ "Very difficult on present income",
hincfel %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(hincfel)
),
hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
cptppola = case_when(
essround < 5 & cptppola == 7 ~ NA_real_,
TRUE ~ cptppola
),
)
france_data <- france_data %>% filter(!is.na(hincfel))
france_data <- france_data %>% filter(!is.na(hinctnta))
france_data <- france_data %>% filter(!is.na(cptppola))
unique(france_data$hincfel)
## [1] "Coping on present income"
## [2] "Difficult on present income"
## [3] "Living comfortably on present income"
## [4] "Very difficult on present income"
unique(france_data$hinctnta)
## [1] 3 6 7 1 2 9 8 5 4 10
unique(france_data$cptppola)
## [1] 1 2 3 4 8 5 9 7
france_data <- france_data %>%
mutate(
hinctnta = as.factor(hinctnta),
cptppola = as.factor(cptppola)
)
test_stat <- france_data %>%
specify(explanatory = hinctnta,
response = cptppola) %>%
hypothesize(null = "independence") %>%
calculate(stat = "chisq")
Visualization
test_stat <- france_data %>%
specify(explanatory = hinctnta,
response = cptppola) %>%
hypothesize(null = "independence") %>%
calculate(stat = "chisq")
print(test_stat$stat)
## X-squared
## 337.4746
null_distribution <- france_data %>%
specify(explanatory = hinctnta,
response = cptppola) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "chisq")
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, 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 %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: cptppola (factor)
## Explanatory: hinctnta (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 54.4
## 2 2 54.0
## 3 3 48.9
## 4 4 56.9
## 5 5 59.0
## 6 6 46.2
## 7 7 63.9
## 8 8 65.1
## 9 9 70.0
## 10 10 47.6
## # ℹ 990 more rows
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: cptppola (factor)
## Explanatory: hinctnta (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 54.4
## 2 2 54.0
## 3 3 48.9
## 4 4 56.9
## 5 5 59.0
## 6 6 46.2
## 7 7 63.9
## 8 8 65.1
## 9 9 70.0
## 10 10 47.6
## # ℹ 990 more rows
Regression
ess$weight <- ess$dweight * ess$pweight
survey_design <- svydesign(ids = ~1, data = ess, weights = ~weight)
library(dplyr)
france_data <- ess %>%
filter(cntry == "FR")
france_data <- france_data %>%
mutate(
hinctnta_recoded = case_when(
hinctnta < 10000 ~ "Low",
hinctnta < 50000 ~ "Medium",
TRUE ~ "High"
),
hincfel_recoded = ifelse(hincfel < 20000, "Low", "High"),
cptppola_recoded = ifelse(cptppola > 0, "Positive", "Negative")
)
model1 <- lm(cptppola ~ hinctnta, data = france_data,)
modelsummary(
list(model1),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
 (1)
hinctnta
0.0 (0.0)
Num.Obs.
6057
R2
0.000
R2 Adj.
0.000
AIC
18558.2
BIC
18578.3
Log.Lik.
−9276.082
RMSE
1.12
france_data <- france_data %>%
mutate(
Feeling = case_when(
hincfel %in% c(7, 8, 9) ~ NA_character_,
hincfel == 1 ~ "Comfortably",
hincfel == 2 ~ "Coping",
hincfel == 3 ~ "Difficult",
hincfel == 4 ~ "Very difficult",
TRUE ~ as.character(hincfel)
),
Confident = case_when(
cptppola %in% c(1, 2) ~ "Not Confident",
cptppola %in% c(3, 4, 5) ~ "Confident",
cptppola %in% c(7, 8, 9) ~ NA_character_
),
Income = case_when(
hinctnta == 1 ~ "J",
hinctnta == 2 ~ "R",
hinctnta == 3 ~ "C",
hinctnta == 4 ~ "M",
hinctnta == 5 ~ "F",
hinctnta == 6 ~ "S",
hinctnta == 7 ~ "K",
hinctnta == 8 ~ "P",
hinctnta == 9 ~ "D",
hinctnta == 10 ~ "H",
hinctnta %in% c(77, 88, 99) ~ NA_character_
)
)
model2 <- lm(hinctnta ~ hincfel, data = france_data, weights = weight)
model3 = lm(hincfel ~ cptppola, data = france_data, weights = weight)
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)
hinctnta
0.0 (0.0)
hincfel
0.1 (0.2)
cptppola
−0.1 (0.0)***
Num.Obs.
6057
13743
6057
R2
0.000
0.000
0.006
R2 Adj.
0.000
0.000
0.006
AIC
18558.2
127155.8
16088.4
BIC
18578.3
127178.4
16108.5
Log.Lik.
−9276.082
−63574.923
−8041.206
RMSE
1.12
22.20
0.86
Interaction Effect
france_data$household_feeling <- scales::rescale(france_data$hincfel, na.rm = TRUE, to = c(0, 100))
france_data$hinctnta <- ifelse(france_data$essround<5 & france_data$hinctnta==5, 1,
ifelse(france_data$essround>=5 & france_data$hinctnta>600, 1, 0))
france_data$hinctnta <- ifelse(france_data$hinctnta == 1, "Upper decile", "Lower decile")
library(dplyr)
france_data <- france_data %>%
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale))
model4 <- lm(household_feeling ~ lrscale + hinctnta + lrscale*hinctnta, data = france_data, weights = weight)
interaction_plot <- effect("lrscale*hinctnta", model4, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect",
xlab="Confident in own ability to participate in politics",
ylab="Pr(household feeling)")