# 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
## [[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"
Task 1 Provide code and answer.
Prompt and question: calculate the average for the variable ‘health’ for the countries of Serbia and Belgium. On average, based on the ESS data, who reports higher levels of subjective health: Serbia and Belgium?
library(fst)
serbia_data <- read_fst("serbia_data.fst")
library(fst)
belgium_data <- read_fst("belgium_data.fst")
ess <- read_fst("All-ESS-Data.fst")
belgium_health <- belgium_data %>% # note: if you work from belgium_data replace "ess" with belgium_data
filter(cntry == "BE") %>%
select(health)
belgium_health$y <- belgium_health$health
table(belgium_health$y)
##
## 1 2 3 4 5 7 8 9
## 4033 9262 3338 674 137 2 3 2
# need to remove 77, 88, 99 or else will alter results. See data portal for what they represent (e.g. DK, Refusal, etc.)
# Recode values 77 through 99 to NA
belgium_health$y[belgium_health$y %in% 77:99] <- NA
mean_health <- mean(belgium_health$health, na.rm = TRUE)
cat("Mean of 'y' is:", mean_health, "\n")
## Mean of 'y' is: 2.063377
serbia_health <- ess %>%
filter(cntry == "SE") %>%
select(health)
serbia_health$y <- serbia_health$health
table(serbia_health$y)
##
## 1 2 3 4 5 7 8 9
## 5559 8242 3525 697 167 4 16 6
# need to remove 77, 88, 99 or else will alter results. See data portal for what they represent (e.g. DK, Refusal, etc.)
# Recode values 77 through 99 to NA
serbia_health$y[serbia_health$y %in% 77:99] <- NA
mean_health <- mean(serbia_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_health, "\n")
## Mean of 'y' is: 2.001043
#From the data presented on average Belgium shows higher levels of subjective health on average.
Task 2 Provide code and answer.
Prompt and question: what is the most common category selected, for Norwegian respondents, for how often they socially meet with friends or relatives. The variable of interest is: sclmeet.
library(fst)
norway_data <- read_fst("norway_data.fst")
norway_sclmeet <- ess %>% # note: if you work from belgium_data replace "ess" with belgium_data
filter(cntry == "NO") %>%
select(sclmeet)
norway_sclmeet$y <- norway_sclmeet$sclmeet
table(norway_sclmeet$y)
##
## 1 2 3 4 5 6 7 77 88
## 44 356 746 2569 2119 5764 4458 2 7
# need to remove 77, 88, 99 or else will alter results. See data portal for what they represent (e.g. DK, Refusal, etc.)
# Recode values 77 through 99 to NA
norway_sclmeet$y[norway_sclmeet$y %in% 77:99] <- NA
library(dplyr)
library(forcats)
df <- norway_sclmeet %>%
mutate(
y_category = case_when(
y == 1 ~ "Never",
y == 2 ~ "Less than once a month",
y == 3 ~ "Once a month",
y == 4 ~ "Several times a month",
y == 5 ~ "Once a week",
y == 6 ~ "Several times a week",
y == 7 ~ "Everyday",
TRUE ~ NA_character_
)
) %>%
mutate(
y_category = fct_relevel(factor(y_category),
"Never",
"Less than once a month",
"Once a month",
"Several times a month",
"Once a week",
"Several times a week",
"Everyday")
)
# To confirm the conversion:
table(df$y_category)
##
## Never Less than once a month Once a month
## 44 356 746
## Several times a month Once a week Several times a week
## 2569 2119 5764
## Everyday
## 4458
get_mode <- function(v) {
tbl <- table(v)
mode_vals <- as.character(names(tbl)[tbl == max(tbl)])
return(mode_vals)
}
mode_values <- get_mode(df$y_category)
cat("Mode of y category:", paste(mode_values, collapse = ", "), "\n")
## Mode of y category: Several times a week
#Based on the inforamtion the most common category would be several times a week.
Task 3 Provide code and answer.
Prompt and question: when you use the summary() function for the variable wrclmch (i.e., how worried about climate change) for both the countries of Estonia and Portugal – what do you notice? What stands out as different when you compare the two countries (note: look up the variable information on the ESS website to help with interpretation)? Explain while referring to the output generated.
portugal_wrclmch <- ess %>%
filter(cntry == "PT") %>%
select(wrclmch)
estonia_wrclmch <- ess %>%
filter(cntry == "RS") %>%
select(wrclmch)
summary(portugal_wrclmch)
## wrclmch
## Min. :1.000
## 1st Qu.:3.000
## Median :4.000
## Mean :3.583
## 3rd Qu.:4.000
## Max. :8.000
## NA's :14773
summary(estonia_wrclmch)
## wrclmch
## Min. :1.000
## 1st Qu.:3.000
## Median :3.000
## Mean :3.581
## 3rd Qu.:4.000
## Max. :9.000
## NA's :2043
#The piece of information that stands out the most when comparing the two would be that the median and maximum of each data set is different. Estonias median being 3.0 and maximum beig 9.0 and Portugals median being 4.0 and maximum 8.0. While these are different the 1st and 3rd quartiles are the same.
Task 4 Provide code and answer.
Prompt and question: using the variables stfhlth and gndr, answer the following: on average, who is more sastified with health services in Italy, men or women? Explain while referring to the output generated.
library(fst)
italy_data <- read_fst("italy_data.fst")
table(italy_data$gndr)
##
## 1 2 9
## 4836 5329 13
italy_data$gndr[italy_data$gndr %in% 9] <- NA
table(italy_data$stfhlth)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88 99
## 455 279 553 778 958 1470 1718 1869 1363 411 192 24 105 3
italy_data$stfhlth[italy_data$stfhlth %in% 77:99] <- NA
italy_data <- italy_data %>%
mutate(
gndr = case_when(
gndr == 1 ~ "Male",
gndr == 2 ~ "Female",
TRUE ~ as.character(gndr)
)
)
italy_data <- italy_data %>% filter(!is.na(gndr))
italy_data <- italy_data %>% filter(!is.na(stfhlth))
italy_data %>%
group_by(gndr) %>%
summarize(meanstfhlth = mean(stfhlth, na.rm = TRUE))
## # A tibble: 2 × 2
## gndr meanstfhlth
## <chr> <dbl>
## 1 Female 5.38
## 2 Male 5.54
#The data shows that on average men are more sastified with health services in Italy.
Task 5 Create boxplots, in the same graph, for stfdem and stfgov for the country of France. Provide an appropriate title and labels. Further, tell us: according to ESS data, would we say that the median French person is more satisfied with the democracy or the national government? Explain.
library(fst)
france_data <- read_fst("france_data.fst")
france_data %>%
# Setting values to NA
mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov)) %>%
# Reshaping the data
select(stfdem, stfgov) %>%
gather(variable, value, c(stfdem, stfgov)) %>%
# Creating the boxplot
ggplot(aes(x = variable, y = value)) +
geom_boxplot() +
labs(y = "Y-axis", x = "X-axis", title = "Boxplot of stfdem vs. stfgov") +
theme_minimal()
## Warning: Removed 557 rows containing non-finite values (`stat_boxplot()`).
#Based on the information provided, it seems that the average french
person is more saticfied with democracy since the median for stfdem is
5.0 and the median or stfgov is around 3.5. End