#Mission 1
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer") # add any you need here
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.3 ✔ 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
## [[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"
#Mission 2 Filter to your country of interest and save the dataset.
ess <- read_fst("All-ESS-Data.fst")
switzerland_data <- ess %>%
filter(cntry == "CH")
switzerland_data_file <- "C:/Users/user/Documents/uoft/Soc202-Coding/switzerland_data.fst"
write_fst(switzerland_data, switzerland_data_file)
getwd()
## [1] "/Users/user/Documents/uoft /Soc202-Coding/Abboud_Chantal_Project_202"
#Mission 3 Clean your environment and load in the filtered dataset.
rm(list = ls())
gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 1264878 67.6 2133170 114.0 NA 2133170 114.0
## Vcells 2156853 16.5 1257028544 9590.4 16384 1350420838 10302.9
df <- read_fst("C:/Users/user/Documents/uoft/Soc202-Coding/switzerland_data.fst")
#Mission 4 Produce and save a data summary output (using data summary skim) for potential outcomes of interest on a similar scale (e.g., 0-10, or 1 to 6, or binary). Add a title. You can do so while coding (explore package information for flextable and/or modelsummary) or add it directly in the word file. Title should be something like: Table 1: Descriptive Statistics for outcome variables. You can alter the title as you see fit.
switzerland_data <- read_fst("C:/Users/user/Documents/uoft/Soc202-Coding/switzerland_data.fst")
# Create a new column 'year' based on 'essround' values
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
switzerland_data$year <- NA
for (i in 1:10) {
switzerland_data$year[switzerland_data$essround == i] <- replacements[i]
}
switzerland_data_table_subset <- switzerland_data %>%
mutate(
stfedu = ifelse(stfedu %in% c(2, 5, 9, 7, 7, 3, 8, 5, 88), NA, stfedu),
pplfair = ifelse(pplfair %in% c(3, 9, 5, 7, 5, 8, 9, 7, 5, 10), NA, pplfair),
imueclt = ifelse(imueclt %in% c(6, 7, 10, 8, 8, 10, 9, 10, 10, 9), NA, imueclt),
trstun = ifelse(trstun %in% c(5, 9, 2, 6, 5, 4, 9, 7, 5, 3), NA, trstun)
)
switzerland_data_table_subset <- switzerland_data_table_subset %>%
rename(
`Educational Attainment` = stfedu,
`Perception of Fairness in Society` = pplfair,
`Cultural Importance` = imueclt,
`Trust in the United Nations` = trstun
)
summary_table_switzerland <- datasummary_skim(switzerland_data_table_subset %>%
select(`Educational Attainment`,
`Perception of Fairness in Society`,
`Cultural Importance`,
`Trust in the United Nations`),
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_switzerland
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Educational Attainment | 7 | 76 | 6.1 | 3.3 | 0.0 | 6.0 | 77.0 |
Perception of Fairness in Society | 8 | 78 | 5.6 | 9.5 | 0.0 | 6.0 | 88.0 |
Cultural Importance | 9 | 60 | 7.1 | 16.3 | 0.0 | 4.0 | 88.0 |
Trust in the United Nations | 7 | 73 | 27.9 | 36.5 | 0.0 | 8.0 | 88.0 |
#Mission 5 Produce and save a data summary output (using data summary skim) for socio-demographic variables. Add a title. You can do so while coding (explore package information for flextable and/or modelsummary) or add it directly in the word file. Title should be something like: Table 1: Descriptive Statistics for socio-demographic variables. You can alter the title as you see fit.
summary_table_demographics <- datasummary_skim(switzerland_data %>%
select(agea, gndr, edlvddk, marsts, pdjobyr),
output = "flextable",
title = "Table 1: Descriptive Statistics for Socio-Demographic 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.
# Print the summary table for socio-demographic variables
summary_table_demographics
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
agea | 85 | 0 | 50.4 | 49.7 | 14.0 | 47.0 | 999.0 |
gndr | 3 | 0 | 1.5 | 0.5 | 1.0 | 2.0 | 9.0 |
marsts | 11 | 46 | 37.4 | 30.4 | 1.0 | 66.0 | 99.0 |
pdjobyr | 92 | 0 | 5328.8 | 2146.6 | 1932.0 | 6666.0 | 9999.0 |
#Mission 6 Produce and save a plot as a PDF for your outcome of interest (any will do here).
educational_attainment_by_year <- switzerland_data_table_subset %>%
group_by(year) %>%
summarize(mean_educational_attainment = mean(`Educational Attainment`, na.rm = TRUE))
educational_attainment_plot <- ggplot(educational_attainment_by_year, aes(x = year, y = mean_educational_attainment)) +
geom_line(color = "blue", size = 1, show.legend = FALSE) +
geom_point(color = "red", size = 3, show.legend = FALSE) +
labs(title = "Mean Educational Attainment in Switzerland (2002-2020)",
x = "Survey Year",
y = "Mean Educational Attainment") +
ylim(0, 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.
ggsave(filename = "educational_attainment_plot.pdf", plot = educational_attainment_plot, device = "pdf", width = 6, height = 4)
print("Plot saved as 'educational_attainment_plot.pdf'")
## [1] "Plot saved as 'educational_attainment_plot.pdf'"
library(ggplot2)
educational_attainment_plot <- ggplot(data = switzerland_data_table_subset, aes(x = `Educational Attainment`)) +
geom_histogram(binwidth = 1, fill = "skyblue", color = "black", alpha = 0.7) +
labs(title = "Educational Attainment Distribution",
x = "Educational Attainment",
y = "Frequency") +
theme_minimal()
educational_attainment_plot
## Warning: Removed 12854 rows containing non-finite values (`stat_bin()`).
```