# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
# 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
## 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] "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] "kableExtra" "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] "rmarkdown" "kableExtra" "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" "kableExtra" "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"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "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] "questionr" "ggridges" "rmarkdown" "kableExtra" "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"
# Load in ESS dataset.
ess <- read_fst("All-ESS-Data.fst")
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]
}
Do a data summary table of three variables of interest. Discuss briefly what you note (i.e., add text in your markdown after the Task 1 code).
Country of interest: Sweden
Variables of interest: iprspot, impfree, iphlppl
sweden_data <- ess %>%
filter(cntry == "SE") %>%
mutate(
respect = ifelse(iprspot %in% c(7, 8, 9), NA, iprspot),
free = ifelse(impfree %in% c(7, 8, 9), NA, impfree),
help_ppl = ifelse(iphlppl %in% c(7, 8, 9), NA, iphlppl)
)
datasummary_skim(sweden_data %>% select(respect, free, help_ppl))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| respect | 7 | 20 | 3.5 | 1.3 | 1.0 | 3.0 | 6.0 | |
| free | 7 | 20 | 2.3 | 1.1 | 1.0 | 2.0 | 6.0 | |
| help_ppl | 7 | 20 | 2.2 | 1.0 | 1.0 | 2.0 | 6.0 |
From this summary, I noted that median opinions for all three variables are between 2-3, which correspond with the answers “like me” and “somewhat like me” for the variables of whether or not it is important to get respect from others, whether it is important for people to make their own decisions, and whether it is important to help and care for others’ well beings. As a result, the majority of the responses tend towards “this is similar to me”, with few extreme choices.
Choose one of the three variables you just summarized in the table. This will be your current main outcome of interest.
Produce a visual that showcases the mean (average) for your outcome of interest by survey year (can be, e.g., point + line plot or ridge plot, depending on your variable). Discuss briefly what you note (i.e., add text in your markdown after the Task 2 code).
The main variable I chose is impfree.
free_by_year <- sweden_data %>%
group_by(year) %>%
summarize(mean_free = mean(free, na.rm = TRUE)) %>%
filter(!is.na(mean_free))
free_by_year
ggplot(free_by_year, aes(x = year, y = mean_free)) +
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 = "Belief in One's Freedom by Year (2002-2018)",
x = "Survey Year",
y = "Trust (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.
From this graph, we note that the belief that one should have the freedom to do what they want maintains about the same mean per year, somwhere around 2.5, indicating that people generally agree with this idea.
Provide a comparison visual of your outcome of interest with two other countries. You can choose the geom() you prefer. Discuss briefly what you note (i.e., add text in your markdown after the Task 3 code).
Other countries selected: UK, France
ess_selected <- ess %>%
filter(cntry %in% c("SE", "GB", "FR")) %>%
mutate(free = ifelse(impfree %in% c(7, 8, 9), NA, impfree))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -impfree, FUN=median), y = impfree, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot Comparison for Importance of Freedom (Sweden, UK, France)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 3206 rows containing non-finite values (`stat_boxplot()`).
Although the median for importance of freedom is very close for each country, we can see the difference in the middle 50% of the data varies per country. France has a large range of opinions regarding freedom, with the majority of values lying between 1 (the belief that it is important to be free is very like me), to 4 (the belief that it is important to be free is a little like me).
Produce a cross-tab between your outcome of interest and a socio-demographic variable (use datasummary_crosstab). Then, calculate column percentages using cprop(), making sure to pick a second socio-demographic variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 4 code).
Chosen socio-demographic variables: Education (edulvlb), Born in Country (brncntr)
# Education
sweden_data <- sweden_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
# View the table of educ_level
table(sweden_data$educ_level)
##
## BA No BA
## 4666 13550
# Born in Country
sweden_data <- sweden_data %>%
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(sweden_data$born_in_country)
##
## No Yes
## 2162 16044
free_edu <- datasummary_crosstab(free ~ educ_level, data = sweden_data)
free_bnc <- datasummary_crosstab(free ~ born_in_country, data = sweden_data)
free_edu
| free | BA | No BA | All | |
|---|---|---|---|---|
| 1 | N | 988 | 2682 | 3670 |
| % row | 26.9 | 73.1 | 100.0 | |
| 2 | N | 1460 | 4023 | 5483 |
| % row | 26.6 | 73.4 | 100.0 | |
| 3 | N | 836 | 2538 | 3374 |
| % row | 24.8 | 75.2 | 100.0 | |
| 4 | N | 303 | 1114 | 1417 |
| % row | 21.4 | 78.6 | 100.0 | |
| 5 | N | 124 | 486 | 610 |
| % row | 20.3 | 79.7 | 100.0 | |
| 6 | N | 11 | 73 | 84 |
| % row | 13.1 | 86.9 | 100.0 | |
| All | N | 4666 | 13550 | 18216 |
| % row | 25.6 | 74.4 | 100.0 |
free_bnc
| free | No | Yes | All | |
|---|---|---|---|---|
| 1 | N | 558 | 3111 | 3670 |
| % row | 15.2 | 84.8 | 100.0 | |
| 2 | N | 619 | 4863 | 5483 |
| % row | 11.3 | 88.7 | 100.0 | |
| 3 | N | 293 | 3081 | 3374 |
| % row | 8.7 | 91.3 | 100.0 | |
| 4 | N | 109 | 1306 | 1417 |
| % row | 7.7 | 92.2 | 100.0 | |
| 5 | N | 67 | 543 | 610 |
| % row | 11.0 | 89.0 | 100.0 | |
| 6 | N | 17 | 67 | 84 |
| % row | 20.2 | 79.8 | 100.0 | |
| All | N | 2162 | 16044 | 18216 |
| % row | 11.9 | 88.1 | 100.0 |
table(sweden_data$free, sweden_data$educ_level) %>%
cprop()
##
## BA No BA All
## 1 26.5 24.6 25.1
## 2 39.2 36.9 37.5
## 3 22.5 23.3 23.0
## 4 8.1 10.2 9.7
## 5 3.3 4.5 4.2
## 6 0.3 0.7 0.6
## Total 100.0 100.0 100.0
table(sweden_data$free, sweden_data$born_in_country) %>%
cprop()
##
## No Yes All
## 1 33.6 24.0 25.1
## 2 37.2 37.5 37.5
## 3 17.6 23.8 23.1
## 4 6.6 10.1 9.7
## 5 4.0 4.2 4.2
## 6 1.0 0.5 0.6
## Total 100.0 100.0 100.0
We can see that for people with education and without education they value freedom, with the majority of values lying between 1-3. Similarly, for people born in the country and not, the value of freedom persists throughout.
Choose one of the two socio-demographic variables you just worked with. Visualize the conditional probability (or column percentages) of your outcome given your selected socio-dem variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 5 code).
Variable chosen: Born in Country
new <- sweden_data %>%
filter(!is.na(born_in_country) & !is.na(free))
probs <- new %>%
count(free, born_in_country) %>%
group_by(born_in_country) %>%
mutate(prob = n/sum(n))
ggplot(probs, aes(x = as.factor(free), y = prob, color = born_in_country)) +
geom_point() +
geom_line(aes(group = born_in_country)) +
labs(title = "Conditional Probabilities of Importance of Freedom",
subtitle = "by Whether or Not You Were Born in Country",
x = "Importance Scale",
y = "Probability") +
theme_minimal()
The importance of freedom increases for both those born in the country, and not born in the country, indicating an overall importance for freedom in individuals. Furthermore, as you go down the importance scale, fewer people agree with the idea that freedom is not important.