This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# List of packages
packages <- c("tidyverse", "fst", "modelsummary") # 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.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ 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
## `modelsummary` has built-in support to draw text-only (markdown) tables.
## To generate tables in other formats, you must install one or more of
## these libraries:
##
## install.packages(c(
## "kableExtra",
## "gt",
## "flextable",
##
## "huxtable",
## "DT"
## ))
##
## Alternatively, you can set markdown as the default table format to
## silence this alert:
##
## config_modelsummary(factory_default = "markdown")
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "fst" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "fst" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
Provide code and answer.
Prompt and question: calculate the average for the variable ‘happy’ for the country of Norway. On average, based on the ESS data, who reports higher levels of happiness: Norway or Belgium?
norway_happy <- ess %>% # asssigned the 'happy' variable to the country Norway
filter(cntry == "NO") %>%
select(happy)
norway_happy$y <- norway_happy$happy # filtered to Norway with 'happy' as the variable of interest
table(norway_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 15 29 59 163 238 730 817 2617 5235 3796 2344 12 10
norway_happy$y[norway_happy$y %in% 77:99] <- NA # removed 77 and 88
table(norway_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 15 29 59 163 238 730 817 2617 5235 3796 2344
mean_y <- mean(norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005
The average for the variable ‘happy’ in Norway is 7.975005
belgium_happy <- ess %>% # assigned the 'happy' variable to the country of Belgium
filter(cntry == "BE") %>%
select(happy)
belgium_happy$y <- belgium_happy$happy # filitered to Belgium with 'happy' has the variable of interest
table(belgium_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88 99
## 50 27 104 194 234 830 999 3503 6521 3402 1565 3 16 3
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA # removed 77, 88, and 99
table(belgium_happy$y)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 50 27 104 194 234 830 999 3503 6521 3402 1565
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
The average for the variable ‘happy’ in Belgium is 7.737334
Therefore, based on the ESS data, Norway reports higher levels of happiness.
Provide code and answer.
Prompt and question: what is the most common category selected, for Irish respondents, for frequency of binge drinking? The variable of interest is: alcbnge.
More info here: https://ess-search.nsd.no/en/variable/0c65116e-7481-4ca6-b1d9-f237db99a694.
# # This chunk filters out the country Ireland through its binge drinking levels. THe Ireland database must be selected and select the alcbnge varibale. '
ireland_alcbnge <- ess %>%
filter(cntry == "IE") %>%
select(alcbnge)
ireland_alcbnge$y <- ireland_alcbnge$alcbnge
table(ireland_alcbnge$y)
##
## 1 2 3 4 5 6 7 8
## 65 650 346 417 239 641 26 6
ireland_alcbnge$y[ireland_alcbnge$y %in% 6:8] <- NA
# Following the code above, this chunk categorizes the names taken from the ESS website.
ireland_alcbnge <- ireland_alcbnge %>%
mutate(
alcbnge_category = case_when(
alcbnge == 1 ~ "Daily or almost daily",
alcbnge == 2 ~ "Weekly",
alcbnge == 3 ~ "Monthly",
alcbnge == 4 ~ "Less than monthly",
alcbnge == 5 ~ "Never",
TRUE ~ NA_character_
),
alcbnge_category = fct_relevel(factor(alcbnge_category),
"Daily or almost daily",
"Weekly",
"Monthly",
"Less than monthly",
"Never")
)
table(ireland_alcbnge$alcbnge_category)
##
## Daily or almost daily Weekly Monthly
## 65 650 346
## Less than monthly Never
## 417 239
The data shows that weekly is the most prominent frequency of binge drinking.
result <- ess %>% # Here, we are gathering the mean for the countries of Portugal and Serbia for the variable plnftr
filter(cntry %in% c("PT", "RS")) %>%
mutate(plnftr = recode(plnftr, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
group_by(cntry) %>%
summarize(mean_plnftr = mean(plnftr, na.rm = TRUE))
print(result)
## # A tibble: 2 × 2
## cntry mean_plnftr
## <chr> <dbl>
## 1 PT 5.42
## 2 RS 4.14
We can see that Portugal has a higher rate of planning for the future compared to Serbia.
Provide code and answer.
Prompt and question: using the variables stfdem and gndr, answer the following: on average, who is more dissastified with democracy in Italy, men or women? Explain while referring to the output generated.
Info on variable here: https://ess.sikt.no/en/variable/query/stfdem/page/1
italy_data <- ess %>% # Here, we are filtering Italy through the ESS database.
filter(cntry == "IT")
italy_data <- italy_data %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
TRUE ~ as.character(gndr)
),
stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem)
)
mean_male_stfdem <- italy_data %>% # After filering, we found the mean for the variable stfdem for men.
filter(gndr == "Male") %>%
summarize(mean_stfdem_men = mean(stfdem, na.rm = TRUE))
print(mean_male_stfdem)
## mean_stfdem_men
## 1 4.782646
means_by_gender <- italy_data %>% # To compare, this line of code groups the average for the disatisfaction of democracy.
group_by(gndr) %>%
summarize(stfdem = mean(stfdem, na.rm = TRUE))
print(means_by_gender)
## # A tibble: 3 × 2
## gndr stfdem
## <chr> <dbl>
## 1 9 3.25
## 2 Female 4.69
## 3 Male 4.78
We can see that females in Italy are more dissatisfied with their democracy.
Provide code and answer.
Prompt: Interpret the boxplot graph of stfedu and stfhlth that we generated already: according to ESS data, would we say that the median French person is more satisfied with the education system or health services? Explain.
Change the boxplot graph: provide the code to change some of the key labels: (1) Change the title to: Boxplot of satisfaction with the state of education vs. health services; (2) Remove the x-axis label; (3) Change the y-axis label to: Satisfaction (0-10).
# The following chunk of code produces the boxplot according to the satisfaction with the state of satisfaction with the state of education vs. health services in France
france_data <- ess %>%
filter(cntry=="FR")
france_data %>%
# Setting values to NA
mutate(stfedu = ifelse(stfedu %in% c(77, 88, 99), NA, stfedu),
stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth)) %>%
# Reshaping the data
select(stfedu, stfhlth) %>%
gather(variable, value, c(stfedu, stfhlth)) %>%
# Creating the boxplot
ggplot(aes(x = variable, y = value)) +
geom_boxplot() +
labs(y = "Satisfaction (0-10)", title = "Boxplot of satisfaction with the state of education vs. health services") +
theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).
We can see that the median French person is more satisfied with their
health care because the variable ‘stfhlth’ indicates a higher median
suggesting that based on the spread of data, the median French person is
more satisfied with their healthcare over their education.