R Markdown

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

Including Plots

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.

# 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"
rm(list=ls()); gc()
##           used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
## Ncells 1224002 65.4    2172744 116.1         NA  2172744 116.1
## Vcells 2095669 16.0    8388608  64.0      16384  3095529  23.7
ess <- read_fst("All-ESS-Data.fst")

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?

belgium_health <- ess %>% # 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

# checking again
table(belgium_health$y)
## 
##    1    2    3    4    5    7    8    9 
## 4033 9262 3338  674  137    2    3    2
mean_y <- mean(belgium_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 2.063377
serbia_health <- ess %>% # note: if you work from belgium_data replace "ess" with belgium_data
  filter(cntry == "RS") %>% 
  select(health)
serbia_health$y <- serbia_health$health

table(serbia_health$y)
## 
##    1    2    3    4    5    8    9 
##  620 1051 1332  411  107    3   24
# 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

# checking again
table(serbia_health$y)
## 
##    1    2    3    4    5    8    9 
##  620 1051 1332  411  107    3   24
mean_y <- mean(serbia_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 2.575254

Answer for Task 1: Belgium = 2.06, Serbia = 2.58, thus Serbia reports higher health.

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.

norway_sclmeet <- ess %>%
  filter(cntry == "NO") %>%
  select(sclmeet)
table(norway_sclmeet$sclmeet)
## 
##    1    2    3    4    5    6    7   77   88 
##   44  356  746 2569 2119 5764 4458    2    7
norway_sclmeet$sclmeet[norway_sclmeet$sclmeet %in% 6:8] <- NA
table(norway_sclmeet$sclmeet)
## 
##    1    2    3    4    5   77   88 
##   44  356  746 2569 2119    2    7
norway_sclmeet$sclmeet_category <- factor(norway_sclmeet$sclmeet, labels = c("Never", "Less than once a month", "Once a month", "Several times a month", "Once a week", "Refusal", "Don't know"))

table(norway_sclmeet$sclmeet_category)
## 
##                  Never Less than once a month           Once a month 
##                     44                    356                    746 
##  Several times a month            Once a week                Refusal 
##                   2569                   2119                      2 
##             Don't know 
##                      7
get_mode <- function(v) {
  tbl <- table(v)
  mode_vals <- as.character(names(tbl)[tbl == max(tbl)])
  return(mode_vals)
}

mode_norway_sclmeet <- get_mode(norway_sclmeet$sclmeet_category)
mode_norway_sclmeet
## [1] "Several times a month"

Answer to Task 2: Several times a month is the most common category selected.

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.

# For Portugal
portugal_wrclmch <- ess %>%
  filter(cntry == "PT") %>%
  select(wrclmch)


# For Estonia
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
portugal_wrclmch$wrclmch[portugal_wrclmch$wrclmch %in% 88] <- NA
table(estonia_wrclmch$wrclmch)
## 
##    1    2    3    4    5    6    7    8 
##  343  852 1612  565  125   56    1    7
estonia_wrclmch$wrclmch[estonia_wrclmch$wrclmch %in% 77:88] <- NA
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

The data from Portugal reveals a first quartile of 3, a median of 4, a mean of 3.583, and a third quartile of 4, indicating that a substantial portion of respondents rated their behavior or attitude relatively high on a scale of 1 to 8, with a slight skew towards higher values. In contrast, Estonia exhibits a lower first quartile of 2, a median of 3, a mean of 2.855, and a third quartile of 3, suggesting generally lower ratings compared to Portugal. While both countries share a similar third quartile, indicating a common distribution towards higher values for a significant portion of respondents, Portugal tends to have slightly higher average ratings overall.

To Visualize it:

ggplot(portugal_wrclmch, aes(x = wrclmch)) +
  geom_histogram(bins = 10, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Distribution for Portugal",
       x = "Scale",
       y = "Count")
## Warning: Removed 14773 rows containing non-finite values (`stat_bin()`).

ggplot(estonia_wrclmch, aes(x = wrclmch)) +
  geom_histogram(bins = 10, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Distribution for Estonia",
       x = "Scale",
       y = "Count")
## Warning: Removed 13295 rows containing non-finite values (`stat_bin()`).

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.

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))
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

Answer to Task 4: Based on the output generated, Men are slighty more satisfies than Woman with health services with a 5.54 over woman with a 5.38

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.

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()`).

Higher median satisfaction with democracy- median of 5 vs. 4.

median(france_data$stfdem)
## [1] 5
median(france_data$stfgov)
## [1] 4
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)) %>%
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(title = "Boxplot of satisfaction with the state of democracy vs. national government", 
       x = "",  # Removing x-axis label
       y = "Satisfaction (0-10)") +
  theme_minimal()
## Warning: Removed 557 rows containing non-finite values (`stat_boxplot()`).