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:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
packages <- c("tidyverse", "fst", "modelsummary") # add any you need here
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_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
## [[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"
library(fst)
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 ‘happy’ for the country of Norway. On average, based on the ESS data, who reports higher levels of happiness: Norway or Belgium?
Note: we already did it for Belgium. You just need to compare to Norway’s average, making sure to provide the code for both.
#Code:
Filtering a specific value (Norway) -> Variable of interest -> happy
Norway_happy <- ess %>%
filter(cntry == "NO") %>%
select(happy)
Removing 77,88 and 99 values (these include Dont know, refusal, no answer responses)
Norway_happy$y <- Norway_happy$happy
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
# 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_happy$y[Norway_happy$y %in% 77:99] <- NA
# checking again
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
Calculating the mean (avg.) of the Variable happy for Norway
mean_y <- mean(Norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005
Repeating the process for Belgium:
Filtering a specific value (Belgium) -> Variable of interest -> happy
belgium_happy <- ess %>%
filter(cntry == "BE") %>%
select(happy)
Removing 77,88 and 99 (these include Dont know, refusal, no answer responses)
belgium_happy$y <- belgium_happy$happy
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
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
Calculating the mean (avg.) of the Variable happy for Norway
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
The mean of Y (happy) for Norway is 7.975005. Belgium’s mean of happy was 7.737334.Based on the ESS data Norway shows higher average levels of happiness. (Higher on the scale means greater 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.
Hint: need to convert numeric value entries to categories as specified in the variable information link. We did similar steps for Estonia and the climate change attitude variable.
Loading Ireland Data set and cleaning values 6 to 9 (Not applicable values)
Ireland_alcbnge <- ess %>%
filter(cntry == "IE") %>%
select(alcbnge)
Ireland_alcbnge$y <- Ireland_alcbnge$alcbnge
# Recode values 6 through 9 to NA
Ireland_alcbnge$y[Ireland_alcbnge$y %in% 6:9] <- NA
table(Ireland_alcbnge$y)
##
## 1 2 3 4 5
## 65 650 346 417 239
Converting to categories so mode appears as a category instead of a number
df <- Ireland_alcbnge %>%
mutate(
y_category = case_when(
y == 1 ~ "Daily or almost daily",
y == 2 ~ "Weekly",
y == 3 ~ "Monthly",
y == 4 ~ "Less than monthly",
y == 5 ~ "Never",
TRUE ~ NA_character_
),
y_category = fct_relevel(factor(y_category), ### here you would put the categories in order you want them to appear or else it will appear alphabetically
"Daily or almost daily",
"Weekly",
"Monthly",
"Less than monthly",
"Never")
)
# To confirm the conversion:
table(df$y_category)
##
## Daily or almost daily Weekly Monthly
## 65 650 346
## Less than monthly Never
## 417 239
Finding the mode as a category:
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: Weekly
The most commonly selected category for Irish respondents for the frequency of binge drinking is weekly.
Provide code and answer.
Prompt and question: when you use the summary() function for the variable plnftr (about planning for future or taking every each day as it comes from 0-10) for both the countries of Portugal and Serbia, 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.
Filtering a specific value (Portugal) -> Variable of interest -> plnftr Also, cleaning the Non applicable values
Portugal_plnftr <- ess %>%
filter(cntry == "PT") %>%
select(plnftr)
Portugal_plnftr$y <- Portugal_plnftr$plnftr
table(Portugal_plnftr$y)
##
## 0 1 2 3 4 5 6 7 8 9 10 88
## 114 184 313 356 264 481 262 382 345 166 370 40
# Recode values 77 through 99 to NA (refusal, dont know, no)
Portugal_plnftr$y[Portugal_plnftr$y %in% 77:99] <- NA
table(Portugal_plnftr$y)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 114 184 313 356 264 481 262 382 345 166 370
Running the summary function for Portugal
summary(Portugal_plnftr)
## plnftr y
## Min. : 0.000 Min. : 0.000
## 1st Qu.: 3.000 1st Qu.: 3.000
## Median : 5.000 Median : 5.000
## Mean : 6.426 Mean : 5.418
## 3rd Qu.: 8.000 3rd Qu.: 8.000
## Max. :88.000 Max. :10.000
## NA's :14604 NA's :14644
Filtering a specific value (Serbia) -> Variable of interest -> plnftr Also, cleaning the Non applicable values
Serbia_plnftr <- ess %>%
filter(cntry == "RS") %>%
select(plnftr)
Serbia_plnftr$y <- Serbia_plnftr$plnftr
table(Serbia_plnftr$y)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 587 133 152 138 95 246 70 87 103 47 364 4 17
# Recode values 77 through 99 to NA (refusal, dont know, no)
Serbia_plnftr$y[Serbia_plnftr$y %in% 77:99] <- NA
table(Serbia_plnftr$y)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 587 133 152 138 95 246 70 87 103 47 364
Running the summary function for Serbia
summary(Serbia_plnftr)
## plnftr y
## Min. : 0.000 Min. : 0.000
## 1st Qu.: 0.000 1st Qu.: 0.000
## Median : 4.000 Median : 4.000
## Mean : 4.983 Mean : 4.143
## 3rd Qu.: 8.000 3rd Qu.: 8.000
## Max. :88.000 Max. :10.000
## NA's :1505 NA's :1526
The mean value for Portugal is higher than Serbia which shows that Portuguese people are more likely to take each day as it comes than Serbians, who will plan more ahead for the future. (The 1-10 scale shows how likely people are to take each day as it comes, with 10 showing the greatest value for taking each day as it comes). The distribution of data is also different. 50% of the data for Portugal falls between 3.00 and 8.00 (the interquartile range is 5) while for Serbia it in between 0.000 and 8.000 showing that the data is spread greater as the interquartile range is greater (8). The median value for Portugal is 5.000 which is greater than Serbia’s median value of 4.000.
Provide code and answer.
Prompt and question: using the variables stfdem and gndr, answer the following: on average, who is more dissatisfied 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
Dealing with both variables (gender and stfdem) after filtering factor Italy
Italy_data <- ess %>%
filter(cntry == "IT")
# Convert gender and stfdem (representing How satisfied with the way democracy works in country)
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, 99), NA, stfdem) # Convert stfdem values
)
Mean value for male satisfaction
# Compute mean for male
mean_male_stfdem <- Italy_data %>%
filter(gndr == "Male") %>%
summarize(mean_stfdem_ = mean(stfdem, na.rm = TRUE))
print(mean_male_stfdem)
## mean_stfdem_
## 1 4.782646
Mean value for female satisfaction
# Compute mean for female
mean_Female_stfdem <- Italy_data %>%
filter(gndr == "Female") %>%
summarize(mean_stfdem_women = mean(stfdem, na.rm = TRUE))
print(mean_Female_stfdem)
## mean_stfdem_women
## 1 4.657553
Judging by the mean (avg) women are more dissatisfied with democracy in Italy (1-10 scale with 10 showing the greatest satisfaction with how democracy works)
Provide code and answer.
install.packages("ggplot2")
## Warning: package 'ggplot2' is in use and will not be installed
Loading data for France:
france_data <- read.fst("france_data.fst")
Creating a box plot for France for the variables stfedu and stfhlth (cleaning data and creating a box plot)
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 = "Y-axis", x = "X-axis", title = "Boxplot of stfedu vs. stfhlth") +
theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).
The dark line in the middle of the box shows the median value. As such, the median French person is more satisfied with the health service than with the education service. (Higher Y axis value shows greater satisfaction on a 1-10 scale)
Running the code for the box plot again but with the changes required in the prompt
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()`).