# 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"
belgium_data <- read.fst("belgium_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")
Instructions: Start a new R markdown for the homework and call it “Yourlastname_Firstname_Homework_1”.
Copy everything below from Task 1 to Task 5. Keep the task prompt and questions, and provide your code and answer underneath.
To generate a new code box, click on the +C sign above. Underneath your code, provide your answer to the task question.
When you are done, click on “Knit” above, then “Knit to Html”. Wait for everything to compile. If you get an error like “Execution halted”, it means there are issues with your code you must fix. When all issues are fixed, it will prompt a new window. Then click on “Publish” in the top right, and then Rpubs (the first option) and follow the instructions to create your Rpubs account and get your Rpubs link for your document (i.e., html link as I provide for the tutorial).
Note: Make sure to provide both your markdown file and R pubs link. If you do not submit both, you will be penalized 2 pts. out of the 5 pts. total.
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.
mean(belgium_data$happy)
## [1] 7.838519
table(belgium_data$happy)
##
## 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
# 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_data$happy[belgium_data$happy %in% 77:99] <- NA
# checking again
table(belgium_data$happy)
##
## 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_data$happy, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.737334
unique(norway_data$cntry)
## [1] "NO"
norway_happy <- norway_data %>% # note: if you work from belgium_data replace "ess" with belgium_data
filter(cntry == "NO") %>%
select(happy)
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
mean_y <- mean(norway_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 7.975005
# When comparing the averages of hapiness between the countries Belgium and Norway from the ESS dataset, Noway has a higher hapiness levels of 7.97 than Belgium 7.73.
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.
ireland_alcbnge <- ireland_data %>%
filter(cntry == "IE") %>%
select(alcbnge)
table_alcbnge <- table(ireland_alcbnge$alcbnge_category)
mode_alcbnge <- names(table_alcbnge)[which.max(table_alcbnge)]
ireland_alcbnge <- ireland_data %>%
filter(cntry == "IE") %>%
select(alcbnge)
ireland_alcbnge$alcbnge_category <- case_when(
ireland_alcbnge$alcbnge == 1 ~ "Daily or almost daily",
ireland_alcbnge$alcbnge == 2 ~ "Weekly",
ireland_alcbnge$alcbnge == 3 ~ "Monthly",
ireland_alcbnge$alcbnge == 4 ~ "Less than monthly",
ireland_alcbnge$alcbnge == 5 ~ "Never",
TRUE ~ NA_character_
)
table(ireland_alcbnge$alcbnge_category)
##
## Daily or almost daily Less than monthly Monthly
## 65 417 346
## Never Weekly
## 239 650
# the most common category selected for Irish respondents for frequency of binge drinking is weekly with 650 respondents
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.
summary(portugal_data$plnftr)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 3.000 5.000 6.426 8.000 88.000 14604
summary(serbia_data$plnftr)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 0.000 4.000 4.983 8.000 88.000 1505
# when looking at the differences between the summary outcomes generated the first difference between the two countries that becomes apparent is the first quantile.The purpose of the first quantile is to limit the lowest and highest values in the In the first quantile, is to have a better representation of the more typical values of the middle 50th percentile. When looking at the outcomes produced, portugal has a 3.0 whereas serbia has a 0.0. From this observation in the outcome, it can be determined that more responders in serbia plan for their future rather than taking it day by day. This is apparent in the first quantile because as statesd perviously, the 25th percentile is supposed to be a greter representation of the 50th percentile (the median) and removes outliers, thus it can be seen that more responders from portugal reported that they take life more day by day with the first quantile at 3.0 in comparision to serbia where the value is 0, meaning that it was not uncommon for respondents to answer 0 meaning that they like to plan their life rather than take it day by day. Another difference is the median. The median represents when values are ranked smallest to the largest, and the median is the middle value (50th percentile). When assessing the outcomes, the median for Portugal is 5.00 which means that the 50th percentile of responders answered right in the middle of whether they plan their life or take life day by day. By contrast, Serbia's median is 4.0 meaning that the 50th percentile of responders was closer to planning their future. Thus from this outcome it can be determined that the median of Serbian responders like to plan for their future more than responders from Portgugal. Another difference in the data is the means. The mean adds all of the values together of this variable of interest and then divides that number by the total number of observations, it is an average. However, unlike the median, the mean can be skewed due to outlier responses. When looking at the outcomes it can be identified that Portugal's mean is 6.43, whereas Serbia's mean is 4.98. From these outcomes, it can be seen that in Portugal, more responders like to take their life day by day, whereas in Serbia with a mean of 4.98 responders like to plan their life more as the valuw is closer to 0 which represents they like to plan their life day by day.
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.
italy_data <- italy_data %>%
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 %>%
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 %>%
group_by(gndr) %>%
summarize(stfdem = mean(stfdem, na.rm = TRUE))
means_by_gender
## # A tibble: 3 × 2
## gndr stfdem
## <chr> <dbl>
## 1 9 3.25
## 2 Female 4.69
## 3 Male 4.78
# When looking at who is more dissatisfied with democracy men or women in Italy, it appears from the output that women are more dissatisfied than men with a 4.69 in comparison to men who have an average of 4.78
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.
# The median in a boxplot graph is the line that runs across the middle of the box. When assessing whether the median French person is more satisfied with the educaion system or the healthcare services it is clear from the graph that the median French person is more satisfied with healthcare services. This is because when looking at the y-axis which is on a scale from 0-10, the average satisfaction with education amongst french people is only 5.0, whereas the average satisfaction with healthcare is at 7.0. A 0 represents extreamly bad, whereas 10 represents extreamly good.
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).
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)", x = NULL, title = "Boxplot of satisfaction with the state of education vs. health services") +
theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).
Hint: copy the boxplot code above and just replace or cut what is asked.