# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer") # add any you need here
# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
# Load the 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 'viridis' was built under R version 4.3.2
## Loading required package: viridisLite
## Warning: package 'ggridges' was built under R version 4.3.2
## Warning: package 'questionr' was built under R version 4.3.2
## Warning: package 'flextable' was built under R version 4.3.2
##
## Attaching package: 'flextable'
##
## The following object is masked from 'package:purrr':
##
## compose
## Warning: package 'infer' was built under R version 4.3.2
## [[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"
# read data
ess <- read_fst("All-ESS-Data.fst")
france_data <- ess %>%
filter(cntry == "FR")
write_fst(france_data, "C:/Users/for_m/Desktop/SOC202 Quantitative/Tutorials/Gu_Jenna_Project_202/france_data.fst")
rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 1259073 67.3 2276281 121.6 2276281 121.6
## Vcells 2129123 16.3 1257002482 9590.2 1356004765 10345.5
df <- read_fst("C:/Users/for_m/Desktop/SOC202 Quantitative/Tutorials/Gu_Jenna_Project_202/france_data.fst")
df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
df$year[df$essround == i] <- replacements[i]
}
france_data <- df
# make sure you know your current folder path. you can do getwd() to check
france_data_table_subset <- france_data %>%
mutate(
atchctr = ifelse(atchctr %in% c(77, 88, 99), NA, atchctr),
stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt)
)
summary_table <- datasummary_skim(france_data_table_subset %>% select(atchctr, stfdem, trstplt), output = "flextable", title = "Table 1: Descriptive statistics for outcome 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 |
|---|---|---|---|---|---|---|---|
atchctr | 12 | 68 | 8.0 | 1.9 | 0.0 | 8.0 | 10.0 |
stfdem | 12 | 1 | 4.6 | 2.4 | 0.0 | 5.0 | 10.0 |
trstplt | 12 | 1 | 3.3 | 2.2 | 0.0 | 3.0 | 10.0 |
france_data_v2 <- france_data_table_subset %>%
rename(
`Level of Emotional Attachment to France` = atchctr,
`Satisfaction with Democracy` = stfdem,
`Trust in Politicians` = trstplt
)
summary_table_v2 <- datasummary_skim(france_data_v2 %>% select(`Level of Emotional Attachment to France`,`Satisfaction with Democracy`, `Trust in Politicians`), output = "flextable", title = "Table 1: Descriptive statistics for outcome 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_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Level of Emotional Attachment to France | 12 | 68 | 8.0 | 1.9 | 0.0 | 8.0 | 10.0 |
Satisfaction with Democracy | 12 | 1 | 4.6 | 2.4 | 0.0 | 5.0 | 10.0 |
Trust in Politicians | 12 | 1 | 3.3 | 2.2 | 0.0 | 3.0 | 10.0 |
# You might need to specify a path, otherwise it saves in the working directory
flextable::save_as_image(summary_table_v2, path = "Table 1: Descriptive statistics for outcome variables.png", # change name to whatever you would like
width = 7.0, height = 7.0) # play around with dimensions
## [1] "Table 1: Descriptive statistics for outcome variables.png"
# Re-coding education
france_data <- france_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & (edulvla == 55 | edulvla == 77 | edulvla == 88 | edulvla == 99) ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & (edulvlb == 5555 | edulvlb == 7777 | edulvlb == 8888 | edulvlb == 9999) ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
edulvla == NA_real_ ~ NA,
edulvlb == NA_real_ ~ NA,
TRUE ~ "No BA"
)
)
#unique(france_data$educ_level)
#unique(france_data$edulvlb)
#paid work status
france_data <- france_data %>%
mutate(pdwrk_recode = case_when(
pdwrk == 1 ~ 'yes',
pdwrk == 0 ~ 'no',
))
#religion
france_data <- france_data %>%
mutate(religion = case_when(
rlgblg == 2 ~ "No",
rlgblg == 1 ~ "Yes",
rlgblg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(rlgblg)
))
summary_table <- datasummary_skim(france_data %>% select(educ_level, pdwrk_recode, religion), output = "flextable", title = "Table 2: Descriptive statistics for socio-demographic variables", "categorical")
summary_table
|
| N | % |
|---|---|---|---|
educ_level | BA | 4221 | 22.2 |
No BA | 14817 | 77.8 | |
pdwrk_recode | no | 9466 | 49.7 |
yes | 9572 | 50.3 | |
religion | No | 9434 | 49.6 |
Yes | 9509 | 49.9 | |
NA | 95 | 0.5 |
france_data_v2 <- france_data %>%
rename(
`Highest Level of Education` = educ_level,
`Doing Last 7 Days: Paid Work` = pdwrk_recode,
`Belonging to a Particular Religion or Denomination` = religion
)
summary_table_v2 <- datasummary_skim(france_data_v2 %>% select(`Highest Level of Education`,`Doing Last 7 Days: Paid Work`,`Belonging to a Particular Religion or Denomination`), output = "flextable", title = "Table 2: Descriptive statistics for socio-demographic variables", "categorical")
summary_table_v2
|
| N | % |
|---|---|---|---|
Highest Level of Education | BA | 4221 | 22.2 |
No BA | 14817 | 77.8 | |
Doing Last 7 Days: Paid Work | no | 9466 | 49.7 |
yes | 9572 | 50.3 | |
Belonging to a Particular Religion or Denomination | No | 9434 | 49.6 |
Yes | 9509 | 49.9 | |
NA | 95 | 0.5 |