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:

# 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
## [[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")

###PROMT 1###

unique(ess$cntry)
##  [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"
Norway_happy <- ess %>%
  filter(cntry == "NO")%>%
  select(happy)
Norway_happy$y <- Norway_happy$happy

#pre-filter
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
Norway_happy$y[Norway_happy$y %in% 77:99] <- NA


#table post-filter
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)
## Mean of 'y' is: 7.975005

#THEREFORE, GIVEN THAT NORWAY HAS A MEAN OF 7.98, WE CAN CONCLUDE THAT ON AVERAGE, NORWAY REPORTS HIGHER LEVELS OF HAPPINESS THAN BELGIUM

###PROMT 2###

ireland_alcbnge <- ess %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

ireland_alcbnge$y <- ireland_alcbnge$alcbnge

table (ireland_alcbnge$y)
## 
##   1   2   3   4   5   6   7   8 
##  65 650 346 417 239 641  26   6
mean_y <- mean(ireland_alcbnge$y, na.rm = TRUE)
median_y <- median(ireland_alcbnge$y, na.rm = TRUE)

cat("mean of 'y' is:", mean_y, "\n")
## mean of 'y' is: 3.908787
cat("median of 'y' is:", median_y)
## median of 'y' is: 4
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),
                            "Daily or almost daily",
                            "Weekly",
                            "Monthly",
                            "Less than monthly",
                            "Never")
  )

table(df$y_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    65                   650                   346 
##     Less than monthly                 Never 
##                   417                   239
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 = ", "))
## Mode of y category: Weekly

#THEREFORE, THE MOST COMMON CATEGORY OF DRINKING WAS WEEKLY

ireland_alcbnge <- ess %>%
  filter(cntry == "IE") %>%
  select(alcbnge)

ireland_alcbnge$y <- ireland_alcbnge$alcbnge

mean_y <- mean(ireland_alcbnge$y, na.rm = TRUE)
median_y <- median(ireland_alcbnge$y, na.rm = TRUE)

cat("mean of 'y' is:", mean_y, "\n")
## mean of 'y' is: 3.908787
cat("median of 'y' is:", median_y)
## median of 'y' is: 4
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),
                            "Daily or almost daily",
                            "Weekly",
                            "Monthly",
                            "Less than monthly",
                            "Never")
  )

table(df$y_category)
## 
## Daily or almost daily                Weekly               Monthly 
##                    65                   650                   346 
##     Less than monthly                 Never 
##                   417                   239
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 = ", "))
## Mode of y category: Weekly

###PROMT 3###

unique(ess$cntry)
##  [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"
portugal_plnftr <- ess %>%
  filter(cntry == "PT") %>%
  select(plnftr)
table(portugal_plnftr)
## plnftr
##   0   1   2   3   4   5   6   7   8   9  10  88 
## 114 184 313 356 264 481 262 382 345 166 370  40

#NOTE: FILTERING 88 AS “NA” JUST IN CASE AND IN THE EVENT THAT SOME CODING MISTAKE THAT I’M NOT AWARE ABOUT HAPPENS

portugal_plnftr$y <- portugal_plnftr$plnftr
portugal_plnftr$y [portugal_plnftr$y %in% 88] <- NA
table(portugal_plnftr)
##       y
## plnftr   0   1   2   3   4   5   6   7   8   9  10
##     0  114   0   0   0   0   0   0   0   0   0   0
##     1    0 184   0   0   0   0   0   0   0   0   0
##     2    0   0 313   0   0   0   0   0   0   0   0
##     3    0   0   0 356   0   0   0   0   0   0   0
##     4    0   0   0   0 264   0   0   0   0   0   0
##     5    0   0   0   0   0 481   0   0   0   0   0
##     6    0   0   0   0   0   0 262   0   0   0   0
##     7    0   0   0   0   0   0   0 382   0   0   0
##     8    0   0   0   0   0   0   0   0 345   0   0
##     9    0   0   0   0   0   0   0   0   0 166   0
##     10   0   0   0   0   0   0   0   0   0   0 370
##     88   0   0   0   0   0   0   0   0   0   0   0
mean_y <-mean(portugal_plnftr$plnftr, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 6.426305
median_y <- median(portugal_plnftr$y, na.rm = TRUE)
cat("Median of 'y' is:", median_y, "\n")
## Median of 'y' is: 5
unique(ess$cntry)
##  [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"
serbia_plnftr <- ess %>%
  filter(cntry =="RS") %>%
  select(plnftr)
table(serbia_plnftr)
## plnftr
##   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
serbia_plnftr$y <- serbia_plnftr$plnftr
serbia_plnftr$y [serbia_plnftr$y %in% 77:88] <- NA
table(serbia_plnftr)
##       y
## plnftr   0   1   2   3   4   5   6   7   8   9  10
##     0  587   0   0   0   0   0   0   0   0   0   0
##     1    0 133   0   0   0   0   0   0   0   0   0
##     2    0   0 152   0   0   0   0   0   0   0   0
##     3    0   0   0 138   0   0   0   0   0   0   0
##     4    0   0   0   0  95   0   0   0   0   0   0
##     5    0   0   0   0   0 246   0   0   0   0   0
##     6    0   0   0   0   0   0  70   0   0   0   0
##     7    0   0   0   0   0   0   0  87   0   0   0
##     8    0   0   0   0   0   0   0   0 103   0   0
##     9    0   0   0   0   0   0   0   0   0  47   0
##     10   0   0   0   0   0   0   0   0   0   0 364
##     77   0   0   0   0   0   0   0   0   0   0   0
##     88   0   0   0   0   0   0   0   0   0   0   0
mean_y <- mean(serbia_plnftr$plnftr, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 4.983358
median_y <- median(serbia_plnftr$plnftr, na.rm = TRUE)
cat("median of 'y' is:", median_y, "\n")
## median of 'y' is: 4
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
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

Conclusion:

Therefore, we see that Serbians tend to plan for their future more than the French given that Serbia has a lower mean average as a result of more individuals scoring lower as a result of future planning. We also see that there is more variance of scores in the middle of the distribution for the French, and at the extremes for Serbia

PROMPT 4

italy_data <- ess %>%
  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_male_stfdem = mean(stfdem, na.rm = TRUE))

means_by_gender <- italy_data %>%
  group_by(gndr) %>%
  summarize(stfdem = mean(stfdem, na.rm = TRUE))

print (means_by_gender)
## # A tibble: 3 × 2
##   gndr   stfdem
##   <chr>   <dbl>
## 1 9        3.25
## 2 Female   4.69
## 3 Male     4.78

THEREFORE, MEN ON AVERAGE TEND TO BE MORE SATISFIED WITH DEMOCRACY THAN WOMEN IN ITALY

PROMPT 5

france_data <- ess %>%
  filter(cntry == "FR")
ggplot(france_data %>% 
         mutate(clsprty = ifelse(clsprty %in% c(7, 8), NA, clsprty)),
       aes(x = clsprty)) + 
  geom_bar(na.rm = TRUE) +
  labs(title = "Boxplot of satisfaciton with the state of education vs health services", 
       x = "X-axis",  # note: you would want to label these appropriately but showcasing what happens when you just put 'X-axis'
       y = "Y-axis") # as well as 'Y-axis'

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 = "", title = "Boxplot of satisfaction with the state of education vs. health services") +
  theme_minimal()
## Warning: Removed 364 rows containing non-finite values (`stat_boxplot()`).

We would say that the median French person is more satisfied with the health services as indicative of a higher bolded median line in the box of the box plot (stfhlth)