# List of packages
packages <- c("tidyverse", "gapminder", "fst") # 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] "gapminder" "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "fst"       "gapminder" "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"
library(tidyverse)
# 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)
## [[1]]
##  [1] "fst"       "gapminder" "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[2]]
##  [1] "fst"       "gapminder" "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[3]]
##  [1] "modelsummary" "fst"          "gapminder"    "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"
# 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)
## [[1]]
##  [1] "modelsummary" "fst"          "gapminder"    "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[2]]
##  [1] "modelsummary" "fst"          "gapminder"    "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "fst"          "gapminder"    "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"
#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")
#portugal_data <- read.fst("portugal_data.fst")
#serbia_data <- read.fst("serbia_data.fst")
#italy_data <- read.fst("italy_data.fst")
ess <- read_fst("All-ESS-Data.fst")

Make-up opportunity Note: highest mark between Homework 1 and make-up retained

Due: Sunday, February 18th, at 11:59 pm EST

Instructions: Start a new R markdown for the homework and call it “Yourlastname_Firstname_Makeup”.

Copy everything below from Task 1 to Task 5. Keep the task prompt and questions, and provide your code and answer underneath.

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.

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?

serbia_health <- ess %>%
  filter(cntry == "SE") %>%
  select(health)

serbia_health$y <- serbia_health$health

table(serbia_health$y <- serbia_health$health)
## 
##    1    2    3    4    5    7    8    9 
## 5559 8242 3525  697  167    4   16    6
mean_y <- mean(serbia_health$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")
## Mean of 'y' is: 2.001043
belgium_health <- ess %>%
  filter(cntry == "BE") %>%
  select(health)

belgium_health$y <- belgium_health$health

table(belgium_health$y <- belgium_health$health)
## 
##    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

Answer to Task 1

The mean of y for Serbia is 2.001043, and the mean of y for Belgium is 2.063377. When comparing these two means, it is clear that Belgium has the higher mean, and the mean represents the average. Based on this information, the country which reports higher levels of subjective health between Belgium and Serbia is Belgium.

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_data <- ess %>%
  filter(cntry == "NO") %>%
  select(sclmeet)

norway_data$y <- norway_data$sclmeet

table(norway_data$y)
## 
##    1    2    3    4    5    6    7   77   88 
##   44  356  746 2569 2119 5764 4458    2    7
norway_data$y[norway_data$y %in% 77:88] <- NA
table(norway_data$y)
## 
##    1    2    3    4    5    6    7 
##   44  356  746 2569 2119 5764 4458
norway_data$y_category <- factor(norway_data$y, 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_data$y_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)

norway_data$y_category <- get_mode(norway_data$y_category)

table(norway_data$y_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

Answer to Task 2

Based on the generated output, it is clear that the highest category would be category 6, with a number of 5764. Additionally, by cleaning our variables and calculating for mode, it gives us the answer of Several times a week at 16065. Finally, by using the ESS website, the 6th category represents several times a week. Therefore, the answer would be category 6, several times a week.

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.

portugal_wrclmch <- ess %>%
  filter(cntry == "PT") %>%
  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
estonia_wrclmch <- ess %>%
  filter(cntry == "RS") %>%
  select(wrclmch)

summary(estonia_wrclmch)
##     wrclmch     
##  Min.   :1.000  
##  1st Qu.:3.000  
##  Median :3.000  
##  Mean   :3.581  
##  3rd Qu.:4.000  
##  Max.   :9.000  
##  NA's   :2043

Answer to Task 3

By using the summary function for Estonia and Portugal, I have recognized that there are a few similarities and differences when analyzing the output. Firstly, I picked up on how the 1st quartile and 3rd quartile are the same for both countries, being 3.000 to 4.000. I also notice that the means are very similar, being 3.583 for Portugal and 3.581 for Estonia. Additionally, they share similar median values too, being 4.000 for Portugal and 3.000 for Estonia. Finally, the min and max are also fairly similar, being 1.000 to 9.000 for Portugal and 1.000 to 8.000 for Estonia. Some key differences I notice are that the NA’s are very different for each country, being 14773 for Portugal and 2043 for Estonia.

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 ~ "Men",
      gndr == 2 ~ "Women",
      TRUE ~ as.character(gndr)
    )
  )
table(italy_data$gndr)
## 
##   Men Women 
##  4836  5329
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 %>%
  group_by(gndr) %>%
  summarize(meanstfhlth = mean(stfhlth, na.rm = TRUE))
## # A tibble: 3 × 2
##   gndr  meanstfhlth
##   <chr>       <dbl>
## 1 Men          5.54
## 2 Women        5.38
## 3 <NA>         4

Answer to Task 4

Based on the output, the mean for men is 5.538961, and the mean for women is 5.384791. Therefore, men are more satisfied with health services in Italy compared to women, as they have the higher mean average.

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 %>%
  mutate(stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov)) %>%
  select(stfdem, stfgov) %>%
  gather (variable, value, c(stfdem, stfgov)) %>%
  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()`).

Answer to Task 5

By analyzing this boxplot, it is clear that the median French person is more satisfied with democracy as opposed to government in France due to the boxplot for stfdem being higher on the Y-axis than stfgov.

End