packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer",
"effects", "survey", "kableExtra", "equatiomatic", "janitor")
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: viridisLite
##
##
## Attaching package: 'flextable'
##
##
## The following object is masked from 'package:purrr':
##
## compose
##
##
## 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: 'kableExtra'
##
##
## The following objects are masked from 'package:flextable':
##
## as_image, footnote
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
##
## Attaching package: 'janitor'
##
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
## [[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] "rmarkdown" "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] "ggridges" "rmarkdown" "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" "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"
##
## [[11]]
## [1] "questionr" "ggridges" "rmarkdown" "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] "flextable" "questionr" "ggridges" "rmarkdown" "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] "infer" "flextable" "questionr" "ggridges" "rmarkdown"
## [6] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [11] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[14]]
## [1] "effects" "carData" "infer" "flextable" "questionr"
## [6] "ggridges" "rmarkdown" "knitr" "viridis" "viridisLite"
## [11] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[15]]
## [1] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "infer" "flextable" "questionr" "ggridges"
## [11] "rmarkdown" "knitr" "viridis" "viridisLite" "fst"
## [16] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [26] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [31] "utils" "datasets" "methods" "base"
##
## [[16]]
## [1] "kableExtra" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "infer" "flextable" "questionr"
## [11] "ggridges" "rmarkdown" "knitr" "viridis" "viridisLite"
## [16] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [21] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [26] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [31] "grDevices" "utils" "datasets" "methods" "base"
##
## [[17]]
## [1] "equatiomatic" "kableExtra" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "infer" "flextable"
## [11] "questionr" "ggridges" "rmarkdown" "knitr" "viridis"
## [16] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [21] "forcats" "stringr" "dplyr" "purrr" "readr"
## [26] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [31] "graphics" "grDevices" "utils" "datasets" "methods"
## [36] "base"
##
## [[18]]
## [1] "janitor" "equatiomatic" "kableExtra" "survey" "survival"
## [6] "Matrix" "grid" "effects" "carData" "infer"
## [11] "flextable" "questionr" "ggridges" "rmarkdown" "knitr"
## [16] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [21] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [26] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [31] "stats" "graphics" "grDevices" "utils" "datasets"
## [36] "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
gb_data <- ess %>%
filter(cntry == "GB")
write_fst(gb_data, "~/Documents/R/SOC202_Research_Poster_Liana_Liu/gb_data.fst")
rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 2650663 141.6 4246115 226.8 NA 4246115 226.8
## Vcells 4501112 34.4 1256101114 9583.3 16384 1364278267 10408.7
dgb <- read_fst("~/Documents/R/SOC202_Research_Poster_Liana_Liu/gb_data.fst")
dgb$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
dgb$year[dgb$essround == i] <- replacements[i]
}
gb_data <- dgb
gb_data_table_subset <- gb_data %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
imwbcnt = ifelse(imwbcnt %in% c(7, 8, 9), NA, imwbcnt),
age = ifelse(age %in% c(999), NA, age)
)
summary_table <- datasummary_skim(gb_data_table_subset %>% select(happy, imwbcnt, age), 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.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
happy | 12 | 0 | 7.5 | 1.9 | 0.0 | 8.0 | 10.0 |
imwbcnt | 11 | 23 | 5.9 | 12.6 | 0.0 | 4.0 | 88.0 |
age | 833 | 89 | 49.8 | 19.1 | 15.0 | 48.7 | 96.8 |
summary_table <- add_header_lines(summary_table, values = "Table 1: Summary Satistics")
summary_table
Table 1: Summary Satistics | |||||||
|---|---|---|---|---|---|---|---|
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
happy | 12 | 0 | 7.5 | 1.9 | 0.0 | 8.0 | 10.0 |
imwbcnt | 11 | 23 | 5.9 | 12.6 | 0.0 | 4.0 | 88.0 |
age | 833 | 89 | 49.8 | 19.1 | 15.0 | 48.7 | 96.8 |
Justification for the test statistic: Since both my explanatory and response variables were numeric variables, the test statistic that I had to do a linear regression test statistic.
test_stat <- gb_data %>%
specify(explanatory = happy, # change variable name for explanatory variable
response = imwbcnt) %>% # change variable name for outcome of interest
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)
## happy
## 0.3657716
null_distribution <- gb_data %>%
specify(explanatory = happy, # change variable name for explanatory variable
response = imwbcnt) %>% # for this test need to set "success" as one of the categories
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
calculate(stat = "slope")
p_val <- null_distribution %>% # replace name here if you assigned something other than null_distribution above
get_pvalue(obs_stat = test_stat, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## 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")
null_distribution
## Response: imwbcnt (numeric)
## Explanatory: happy (numeric)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.0169
## 2 2 0.0237
## 3 3 0.0144
## 4 4 -0.00179
## 5 5 -0.00267
## 6 6 -0.00484
## 7 7 0.0124
## 8 8 0.0391
## 9 9 0.00605
## 10 10 -0.00597
## # ℹ 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)
null_distribution
## Response: imwbcnt (numeric)
## Explanatory: happy (numeric)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.0169
## 2 2 0.0237
## 3 3 0.0144
## 4 4 -0.00179
## 5 5 -0.00267
## 6 6 -0.00484
## 7 7 0.0124
## 8 8 0.0391
## 9 9 0.00605
## 10 10 -0.00597
## # ℹ 990 more rows
model1 <- lm(imwbcnt ~ happy, data = gb_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) happy
## 3.5027209 0.3657716
gb_data_table_subset <- gb_data %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
edulvla = edulvla %>%
na_if(55) %>%
na_if(88),
edulvlb = edulvlb %>%
na_if(5555) %>%
na_if(7777) %>%
na_if(8888),
educ.ba = case_when(
essround < 5 & edulvla == 5 ~ 1,
essround >= 5 & edulvlb > 600 ~ 1,
TRUE ~ 0
)
)
dgb$weight <- dgb$dweight * dgb$pweight
model1 <- lm(imwbcnt ~ happy, data = dgb, weights = weight)
model2 <- lm(imwbcnt ~ happy + age, data = dgb, weights = weight)
model3 <- lm(imwbcnt ~ happy + age + edulvla, data = dgb, 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) | |
|---|---|---|---|
| happy | 0.4 (0.0)*** | 0.1 (0.1) | 0.1 (0.1) |
| age | 0.0 (0.0) | 0.0 (0.0) | |
| edulvla | 0.1 (0.0)** | ||
| Num.Obs. | 20979 | 2394 | 2394 |
| R2 | 0.013 | 0.001 | 0.004 |
| R2 Adj. | 0.013 | 0.000 | 0.003 |
| AIC | 159837.0 | 17928.9 | 17922.1 |
| BIC | 159860.9 | 17952.0 | 17951.0 |
| Log.Lik. | −79915.506 | −8960.450 | −8956.033 |
| RMSE | 10.98 | 10.05 | 10.04 |
gb_data$edulvla[gb_data$edulvla==55] <- NA # "other"
gb_data$edulvlb[gb_data$edulvlb==5555] <- NA # "other"
gb_data$educ.ba <- ifelse(gb_data$essround<5 & gb_data$edulvla==5, 1,
ifelse(gb_data$essround>=5 & gb_data$edulvlb>600, 1, 0))
gb_data$educ.ba <- ifelse(gb_data$educ.ba == 1, "BA", "No BA")
gb_data <- gb_data %>%
# Clean Left-right scale
mutate(lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale))
model3 <- lm(imwbcnt ~ lrscale + educ.ba + lrscale*educ.ba, data = gb_data, weights = weight)
interaction_plot <- effect("lrscale*educ.ba", model3, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect",
xlab="Happiness (0 = Extremely unhappy, 10 = Extremely happy)",
ylab="View of Immigrants (0 = Immigrants make country a worse place to live, 10 = Immigrants make a country a better place to live ")
gb_data_table_subset <- gb_data %>%
mutate(
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt)
)
contingency_tab <- gb_data_table_subset %>%
count(happy, imwbcnt) %>%
spread(key = imwbcnt, value = n, fill = 0) %>%
adorn_totals("row")
contingency_tab
## happy 0 1 2 3 4 5 6 7 8 9 10 <NA>
## 0 45 13 6 13 3 11 2 4 2 4 9 1
## 1 22 19 14 12 7 20 3 5 5 1 3 1
## 2 43 36 34 27 15 42 10 11 8 7 3 4
## 3 53 40 63 76 47 114 23 24 26 11 11 14
## 4 49 48 96 97 84 138 45 37 32 22 17 5
## 5 188 103 160 170 163 409 102 102 74 30 32 35
## 6 77 74 148 156 202 379 145 148 108 39 39 26
## 7 172 137 266 367 454 954 418 474 301 99 98 64
## 8 235 228 398 569 601 1455 695 845 610 198 166 99
## 9 144 161 267 315 342 861 393 452 419 203 139 44
## 10 250 130 198 232 217 559 199 217 215 89 195 56
## <NA> 3 0 4 0 0 4 0 5 1 2 0 14
## Total 1281 989 1654 2034 2135 4946 2035 2324 1801 705 712 363
Total Number = (1281 - 3) + 989 + (1654 - 4) + 2034 + 2135 + (4946 - 4) + 2035 + (2324 - 5) + (1801 - 1) + (705 - 2) + 712 + (363 - 14) Total Number = 20,583