title: “naccarato_Isabella_Homeork_1” author: “Isabella Naccarato” date: “2024-01-22” output: html_document

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

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:

`` read_fst


## Including Plots

You can also embed plots, for example:

<img src="Homeowrk-1-STATS-_files/figure-html/pressure-1.png" width="672" />
##Homework 
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
##QUESTION 1
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?
##My ANSWER 
#Norway reports higher levels of happiness according to the code below 

```r
ess <- read_fst("All-ESS-Data.fst")
belgium_data <- read_fst("belgium_data.fst") # this is to load the belgium dataset 
  
belgium_happy <- mean(belgium_data$happy) # this is to calculate the average of happy in the belgium dataset


ess %>%
  filter(cntry == "BE") %>%
  summarize(Belgium_Average = mean(happy, na.rm = TRUE)) # this is to first filter ess dataset for only belgium, and then calculate mean on the filtered dataset 
##   Belgium_Average
## 1        7.838519
norway_data<-read_fst("norway_data.fst")
norway_happy <- mean(norway_data$happy) 
ess %>%
  filter(cntry == "NO") %>%
  summarize(norway_Average = mean(happy, na.rm = TRUE))
##   norway_Average
## 1       8.076377

Note: we already did it for Belgium. You just need to compare to Norway’s average, making sure to provide the code for both.

##QUESTION 2

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. ##MYASNWER

# First filter the ess dataset for the country Ireland, and select the variable alcbnge

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

# Recode values 6 through 8 to NA because we should only have values 1 through 5 for alcbnge 
ireland_alcbng$alcbnge[ireland_alcbng$alcbnge %in% 6:8] <- NA

# Look at table to see which value of alcbnge is selected the most often
table(ireland_alcbng$alcbnge)
## 
##   1   2   3   4   5 
##  65 650 346 417 239
# The category 2: Weekly is selected the most often, 650 times, so this is the most common category selected.

#The most common category and frequency is 650 times

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. ##QUESTION 3 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. ### MY ANSWER

#According to this data, Serbia tends to plan out there days more than Portugal and Portugal tends to take the days as they come more. 
 Portugal_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  select(plnftr)

serbia_data <- read_fst("serbia_data.fst")
 Serbia_plnftr <- serbia_data %>% 
  select(plnftr)

 summary(Portugal_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 3.000  
##  Median : 5.000  
##  Mean   : 6.426  
##  3rd Qu.: 8.000  
##  Max.   :88.000  
##  NA's   :14604
 summary(Serbia_plnftr)
##      plnftr      
##  Min.   : 0.000  
##  1st Qu.: 0.000  
##  Median : 4.000  
##  Mean   : 4.983  
##  3rd Qu.: 8.000  
##  Max.   :88.000  
##  NA's   :1505

###QUESTION 4 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.

Info on variable here: https://ess.sikt.no/en/variable/query/stfdem/page/1 ###MY ANSWER

#stfdm is how satisfied with the way democracy works in a country 
#gndr is gender
italy_data <- read_fst("italy_data.fst")

italy_stfdem_male <- italy_data %>% 
  filter(gndr == 1) %>% 
  select(stfdem)

italy_stfdem_female <- italy_data %>% 
  filter(gndr == 2) %>% 
  select(stfdem)
 
mean(italy_stfdem_male$stfdem)
## [1] 7.425145
mean(italy_stfdem_female$stfdem)
## [1] 8.431413
#The mean for stfdem for males is lower than for females so on average, males are more dissatisfied with democracy in Italy than females.

###QUESTION 5 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.

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

Hint: copy the boxplot code above and just replace or cut what is asked. ###MY ANSWER The median French person is more satisifed with health service because on the boxplot we can see the median line is higher on the side for health services.

france_data <- read_fst("france_data.fst")


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