# 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.5
## ✔ 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")
belgium_data <- read.fst("belgium_data.fst")
#estonia_data <- read.fst("estonia_data.fst")
france_data <- read.fst("france_data.fst")
norway_data <- read.fst("norway_data.fst")
ireland_data <- read.fst("ireland_data.fst")
portugal_data <- read.fst("portugal_data.fst")
serbia_data <- read.fst("serbia_data.fst")
italy_data <- read.fst("italy_data.fst")
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?
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 7, 8, 9 or else will alter results.
# Recode values 7 through 9 to NA
belgium_health$y[belgium_health$y %in% 7:9] <- NA
# checking again
table(belgium_health$y)
##
## 1 2 3 4 5
## 4033 9262 3338 674 137
mean_belgium <- mean(belgium_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_belgium, "\n")
## Mean of 'y' is: 2.060995
serbia_health <- serbia_data %>%
select(health)
# Recode invalid values to NA
serbia_health$health[serbia_health$health %in% c(7, 8, 9)] <- NA
# Calculate mean
mean_serbia <- mean(serbia_health$healt, na.rm = TRUE)
cat("Mean of 'y' is:", mean_serbia, "\n")
## Mean of 'y' is: 2.526839
Belgium = 2.060995, Serbia = 2.526839 , thus Serbia reports slightly higher health.
mean_serbia
## [1] 2.526839
mean_belgium
## [1] 2.060995
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.
norway_binge <- norway_data %>%
select(sclmeet)
table(norway_binge$sclmeet)
##
## 1 2 3 4 5 6 7 77 88
## 44 356 746 2569 2119 5764 4458 2 7
1 Never
2 Less than once a month 3 Once a month 4 Several times a month
5 Once a week 6 Several times a week 7 Every day 77 Refusal 88 Don’t
know
99 No answer
norway_binge$sclmeet[norway_binge$sclmeet %in% 77:99] <- NA
table(norway_binge$sclmeet)
##
## 1 2 3 4 5 6 7
## 44 356 746 2569 2119 5764 4458
norway_binge$sclmeet_category <- factor(norway_binge$sclmeet, labels = c("Never", "Less than once a month", "Once a month", "Several times a month", "Once a week",'Several times a week','Every day'))
table(norway_binge$sclmeet_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
## Every day
## 4458
get_mode <- function(v) {
tbl <- table(v)
mode_vals <- as.character(names(tbl)[tbl == max(tbl)])
return(mode_vals)
}
mode_norway_binge <- get_mode(norway_binge$sclmeet_category)
mode_norway_binge
## [1] "Several times a week"
Norwegian respondents socially meet with friends or relatives several times a week.
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.
# For Portugal
portugal_wrclmch <- ess %>%
filter(cntry == "PT") %>%
select(wrclmch)
# For Serbia
Estonia_wrclmch <- ess %>%
filter(cntry == "EE") %>%
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.:2.000
## Median :3.000
## Mean :2.855
## 3rd Qu.:3.000
## Max. :8.000
## NA's :13295
table(portugal_wrclmch$wrclmch)
##
## 1 2 3 4 5 6 7 8
## 55 251 1171 1203 380 13 4 31
table(Estonia_wrclmch$wrclmch)
##
## 1 2 3 4 5 6 7 8
## 343 852 1612 565 125 56 1 7
portugal_wrclmch$wrclmch[portugal_wrclmch$wrclmch %in% 6:9] <- NA
table(portugal_wrclmch$wrclmch)
##
## 1 2 3 4 5
## 55 251 1171 1203 380
Estonia_wrclmch$wrclmch[Estonia_wrclmch$wrclmch %in% 6:9] <- NA
table(Estonia_wrclmch$wrclmch)
##
## 1 2 3 4 5
## 343 852 1612 565 125
summary(portugal_wrclmch)
## wrclmch
## Min. :1.000
## 1st Qu.:3.000
## Median :4.000
## Mean :3.524
## 3rd Qu.:4.000
## Max. :5.000
## NA's :14821
summary(Estonia_wrclmch)
## wrclmch
## Min. :1.000
## 1st Qu.:2.000
## Median :3.000
## Mean :2.793
## 3rd Qu.:3.000
## Max. :5.000
## NA's :13359
1st Q for Portugal = 3, for Estonia = 2. 1 is “I don’t worry about the climate change at all.” as opposed to 5 “I am extremely worried about climate change”.
From the summary, the median and mean for Portugal are equal to 4 and 3.524, which implies there is a significant amount of Portuguese care about climate change. The 1st quantile for Portugal = 3, meaning middle 50%, from 25 to 75%, starts from 3 and ends at 4. We can say there’s a significant amount of Portuguese that answer “I worried about climate change”, whereas that value is far less frequent for Estonian respondents – whose middle 50% range starts at 2.
Now what is interesting the median (50%) equals to 3rd quantile (75%) for both countries. For Portugal, median = 3rd Q = 4, meaning most of the Portuguese say they are very worried about climate change. In other words, the distribution skewed left when the values concentrated toward the higher scale. Comparatively, for Estonia, median = 3rd Q = 3, which means many respondents select the somewhat worried option. The distribution of Estonia does not skew left as the Portuguese does.
On average, the mean for Portugal and Estonia is 3.524 and 2.793. Thus, Estonians are less worried about climate change compared with the Portuguese.
ggplot(portugal_wrclmch, aes(x = wrclmch)) +
geom_histogram(bins = 5, fill = "blue", color = "black") +
theme_minimal() +
labs(title = "Distribution for portugal",
x = "Scale",
y = "Count")
## Warning: Removed 14821 rows containing non-finite values (`stat_bin()`).
ggplot(Estonia_wrclmch, aes(x = wrclmch)) +
geom_histogram(bins = 5, fill = "blue", color = "black") +
theme_minimal() +
labs(title = "Distribution for Estonia",
x = "Scale",
y = "Count")
## Warning: Removed 13359 rows containing non-finite values (`stat_bin()`).
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.
table(italy_data$gndr)
##
## 1 2 9
## 4836 5329 13
italy_data$gndr[italy_data$gndr %in% 9] <- NA
table(italy_data$gndr)
##
## 1 2
## 4836 5329
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
table(italy_data$stfhlth)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 455 279 553 778 958 1470 1718 1869 1363 411 192
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))
table(italy_data$gndr)
##
## Female Male
## 5260 4774
table(italy_data$stfhlth)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 455 278 552 775 957 1466 1716 1869 1363 411 192
italy_data %>%
group_by(gndr) %>%
summarize(meanstfdem = mean(stfhlth, na.rm = TRUE))
## # A tibble: 2 × 2
## gndr meanstfdem
## <chr> <dbl>
## 1 Female 5.38
## 2 Male 5.54
Women are more dissatisfied – although difference is only between average of 5.38 vs. 5.54.
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.
table(italy_data$stfgov)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88 99
## 1229 624 985 1201 1302 1617 1397 805 375 77 60 54 303 5
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(title = "Boxplot of satisfaction with the state of stfdem vs. stfdem",
x = "", # Removing x-axis label
y = "Satisfaction (0-10)") +
theme_minimal()
## Warning: Removed 557 rows containing non-finite values (`stat_boxplot()`).
median(france_data$stfdem)
## [1] 5
median(france_data$stfgov)
## [1] 4
Higher median satisfaction with democracy – median of 5 vs. 4. Interquartile range also tells the same story (i.e., look at the top and bottom of the box).