# 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"
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]
}
Important: Must post link to your markdown on the discussion board (file and “knit” html), along with post detailing your general takeaways from exploring variables of interest, comparing to other countries, and looking into socio-demographics. You must also attach your R markdown file.
In your post, please link to the ESS website for the three variables you considered, as well as highlight the country you wish to focus on and for what main reason. Finally, please attach (or provide a screenshot) the visual that you produced while doing the tasks that is most interesting to you and explain why (just one visual not all).
You cannot use the same three variables we considered in the tutorial. You can use one of the three, but not all three.
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).
table(ess$gincdif)
##
## 1 2 3 4 5 7 8 9
## 147968 204208 70201 47040 11323 492 8673 650
#Noting values 7, 8, 9 need to be treated as missing
table(ess$freehms)
##
## 1 2 3 4 5 7 8 9
## 152529 167624 69465 43607 36444 1592 18458 836
#Noting values 7, 8, 9 also need to be treated as missing
table(ess$stfhlth)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 25347 20246 34866 47881 48022 66773 56600 71547 69247 27888 16093 267 4627
## 99
## 1151
#The values 77, 88, and 99 will be treated as missing
france_data <- ess %>% # Start with the original 'ess' dataset
filter(cntry == "FR") %>% # Filter the data to only include rows where country (cntry) is France ("FR")
mutate(
gincdif = ifelse(gincdif %in% c(7, 8, 9), NA, gincdif), # If 'gincdif' is 7, 8, or 9, set it to NA.
freehms = ifelse(freehms %in% c(7, 8, 9), NA, freehms), # If 'freehms' is 7, 8, or 9, set it to NA.
stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth), # For 'stfhlth', set values 77, 88, and 99 to NA.
)
table(france_data$gincdif)
##
## 1 2 3 4 5
## 7642 7061 2039 1454 724
table(france_data$freehms)
##
## 1 2 3 4 5
## 11046 4702 1537 679 921
table(france_data$stfhlth)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 314 228 715 1297 1616 2664 2599 3885 3744 1225 705
datasummary_skim(france_data %>% select(gincdif, freehms, stfhlth))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| gincdif | 6 | 1 | 2.0 | 1.1 | 1.0 | 2.0 | 5.0 | |
| freehms | 6 | 1 | 1.7 | 1.1 | 1.0 | 1.0 | 5.0 | |
| stfhlth | 12 | 0 | 6.1 | 2.2 | 0.0 | 7.0 | 10.0 |
The tables show that French people tend to strongly agree with the statements “government should reduce differences in income levels” and “gays and lesbians free to live life as they wish”. It also shows that many feel the national health system is doing fine–it’s doing a good job but could definitely be improved.
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).
lfree_by_year <- france_data %>%
group_by(year) %>%
summarize(mean_lfree = mean(freehms, na.rm = TRUE))
lfree_by_year
## # A tibble: 10 × 2
## year mean_lfree
## <dbl> <dbl>
## 1 2002 1.87
## 2 2004 1.95
## 3 2006 1.92
## 4 2008 1.65
## 5 2010 1.76
## 6 2012 1.84
## 7 2014 1.69
## 8 2016 1.56
## 9 2018 1.51
## 10 2020 1.45
ggplot(lfree_by_year, aes(x = year, y = mean_lfree)) +
geom_line(color = "pink", size = 1) + # Line to show the trend
geom_point(color = "purple", size = 3) + # Points to highlight each year's value
labs(title = "Lesbians and gays free to live life as they wish in France (2002-2020)",
x = "Survey Year",
y = "Freedom (0-5)") +
ylim(0, 5) + # 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.
The graph shows that on average, French participants have a slightly declining rate of agreement in lesbians and gays should live freely as they wish in the recent decade.
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).
ess_selected <- ess %>%
filter(cntry %in% c("FR", "AT", "LT")) %>%
mutate(freehms = ifelse(freehms %in% c(7, 8, 9), NA, freehms))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -freehms, FUN=median), y = freehms, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for lesbian and gay freedom (France, Austria, Lithuania)",
x = "Country",
y = "Scale (0-5)")
task3plot
## Warning: Removed 1436 rows containing non-finite values (`stat_boxplot()`).
This graph shows that there French people are more likely to agree with freedom for lesbian and gays, followed by Austrians, while Lithuanian tend to feel ambivalent towards or disagree. The majority of French participants strongly agree, while Austrians agree, and Lithuanians feel ambivalent.
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).
https://ess-search.nsd.no/variable/query/domicil/1
france_data <- france_data %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban", # or set to Urban | Regardless decision needs to be justified
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_))
# check
table(france_data$geo)
##
## Peri-Urban Rural Urban
## 2361 13087 3584
# compare
table(france_data$domicil)
##
## 1 2 3 4 5 7 8
## 3584 2361 6257 5668 1162 3 3
freehmsgeo <- datasummary_crosstab(freehms ~ geo, data = france_data)
freehmsgeo
| freehms | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 1 | N | 1382 | 7443 | 2219 | 11046 |
| % row | 12.5 | 67.4 | 20.1 | 100.0 | |
| 2 | N | 580 | 3345 | 773 | 4702 |
| % row | 12.3 | 71.1 | 16.4 | 100.0 | |
| 3 | N | 199 | 1086 | 252 | 1537 |
| % row | 12.9 | 70.7 | 16.4 | 100.0 | |
| 4 | N | 74 | 487 | 118 | 679 |
| % row | 10.9 | 71.7 | 17.4 | 100.0 | |
| 5 | N | 101 | 626 | 194 | 921 |
| % row | 11.0 | 68.0 | 21.1 | 100.0 | |
| All | N | 2361 | 13087 | 3584 | 19038 |
| % row | 12.4 | 68.7 | 18.8 | 100.0 |
# Re-coding education
france_data <- france_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
# View the table of educ_level
table(france_data$educ_level)
##
## BA No BA
## 4235 14803
table(france_data$freehms, france_data$educ_level) %>%
cprop()
##
## BA No BA All
## 1 68.0 55.8 58.5
## 2 22.2 25.7 24.9
## 3 5.6 8.9 8.1
## 4 2.0 4.1 3.6
## 5 2.2 5.6 4.9
## Total 100.0 100.0 100.0
The crosstab shows that there are many more participants living in rural areas than participants living in urban and peri-rural areas. Since there are more number of responses who lived in rural areas, the row percentages hold less relevance in this case, as a high row percentage will not mean a higher likelihood of this group of population voting for this particular answer–it simply means that there are more participants from rural areas participating in this servey. The column percentage table, however, shows that participants with higher education tend to agree more for freedom for homosexual population in France.
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).
# double check clean
france_clean <- france_data %>%
filter(!is.na(educ_level) & !is.na(freehms))
# calculate conditional probabilities
france_probs <- france_clean %>%
count(freehms, educ_level) %>%
group_by(educ_level) %>%
mutate(prob = n / sum(n))
# plot
ggplot(france_probs, aes(x = as.factor(freehms), y = prob, color = educ_level)) +
geom_point() +
geom_line(aes(group = educ_level)) +
labs(title = "Conditional Probabilities of Freedom for Homosexuals",
subtitle = "by Level of Education",
x = "Degree of Agreement",
y = "Probability") +
theme_minimal()
The graph here shows that population with higher level of education tend
to agree more with freedom for lesbians and gays in France. While both
groups of population show a stong inclination to agreement, the group
with a bachelor degree is shown to have a stronger agreement.