First things first: setting up your environment

In the list following c(, you would be any package you need in between quotation marks.

# 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
## `modelsummary` has built-in support to draw text-only (markdown) tables.
##   To generate tables in other formats, you must install one or more of
##   these libraries:
##   
## install.packages(c(
##     "kableExtra",
##     "gt",
##     "flextable",
##    
##   "huxtable",
##     "DT"
## ))
## 
##   Alternatively, you can set markdown as the default table format to
##   silence this alert:
##   
## config_modelsummary(factory_default = "markdown")
## [[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")

Homework 1 (5%): due by next lecture on Jan. 23

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.

Task 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?

Note: we already did it for Belgium. You just need to compare to Norway’s average, making sure to provide the code for both. (I did all the code for these questions and sent a couple hours on them, and i had no errors or anything, they were all loading on the mini screen to the right. When i went to knit the code s that i could submit everything it said execution halted. The only way it would knit was if i added a # before the code. Which i know may be wrong but i think some of the code might be right. i hope this dose not effect my mark. Juts wanted to let you know.)

#Code for belgium (Code for norway is below) #{r} belgium_happy <- ess %>% # note: if you work from belgium_data replace "ess" with belgium_data filter(cntry == "BE") %>% select(happy) #```{r} belgium_happy\(y <- belgium_happy\)happy

table(belgium_happy\(y) ``` #have to remove 77,88,99 to clean it out #```{r} #belgium_happy\)y[belgium_happy\(y %in% 77:99] <- NA #double cheking table(belgium_happy\)y)

#Calculating the mean
#```{r}
mean_y <- mean(belgium_happy$y, na.rm = TRUE)
cat("Mean of 'y' is:", mean_y, "\n")

#Calculating the median #{r} median_y <- median(belgium_happy$y, na.rm = TRUE) cat("Median of 'y' is:", median_y, "\n") #Calculating the mode #```{r} mode_y <- belgium_happy %>% filter(!is.na(y)) %>% count(y) %>% arrange(desc(n)) %>% slice(1) %>% pull(y)

cat(“of Y:”, mode_y, “”)

#Calculating the sandard deviation 
#```{r}
sd_y <- sd(belgium_happy$y, na.rm = TRUE)
cat("Standard Deviation of 'y':", sd_y, "\n")

This shows us that the standard for happiness on a scale from 0-10 (10 being happy) belgium is 7.7 on the scale

#Code for Norway #{r} #norway_happy <- ess %>% # note: if you work from belgium_data replace "ess" with belgium_data filter(cntry == "NO") %>% select(happy)

#```{r} norway_happy\(y <- norway_happy\)happy

table(norway_happy\(y) ``` #Have to remove 77 and 88 to clean it #```{r} norway_happy\)y[norway_happy$y %in% 77:88] <- NA

checking again to make sure that it was cleaned

table(norway_happy\(y) ``` #Calculating the mean #```{r} mean_y <- mean(norway_happy\)y, na.rm = TRUE) cat(“Mean of ‘y’ is:”, mean_y, “”)

#determine the median of 'y'
#```{r}
median_y <- median(norway_happy$y, na.rm = TRUE)
cat("Median of 'y' is:", median_y, "\n")

#Finding mode #```{r} #mode_y <- norway_happy %>% filter(!is.na(y)) %>% count(y) %>% arrange(desc(n)) %>% slice(1) %>% pull(y)

cat(“of Y:”, mode_y, “”)

#Finding the standard deviation 
#```{r}
#sd_y <- sd(norway_happy$y, na.rm = TRUE)
cat("Standard Deviation of 'y':", sd_y, "\n")

#Answer: People in norway on a scale from 0-10, 10 being happy, rank 7.9 on the happy scale. Therfore making norway happier than the people in belgium who are at 7.7 on the scale.

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

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.

#filtering to a specific value #{r} #irish_alcbnge <- ess %>% filter(cntry == "IE") %>% select(alcbnge) #```{r} irish_alcbnge\(y <- irish_alcbnge\)alcbnge

table(irish_alcbnge$y)

#The answer: the most common category selected, for Irish respondents, for frequency of binge drinking is, cateroy 2 which on the ESS website says "drinking weekly"
------------------------------------------------------------------
## Task 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.
--

#Portugal code 
#```{r}
#portugal_plnftr <- ess %>% 
  filter(cntry == "PT") %>% 
  select(plnftr)

#```{r} portugal_plnftr\(y <- portugal_plnftr\)plnftr

table(portugal_plnftr$y)

#Finding the mena and the median 
#```{r}
portugal_plnftr %>%
  summarize(
    mean_y = mean(y, na.rm = TRUE),
    median_y = median(y, na.rm = TRUE)
  ) %>%
  print()

#Calculating the mode for portugal #```{r} mode_y <- portugal_plnftr %>% filter(!is.na(y)) %>% count(y) %>% arrange(desc(n)) %>% slice(1) %>% pull(y)

cat(“of Y:”, mode_y, “”)

#Calculation the standard deviation for portugal
#```{r}
sd_y <- sd(portugal_plnftr$y, na.rm = TRUE)
cat("Standard Deviation of 'y':", sd_y, "\n")

#Calculating the summary function for portugal #{r} summary(portugal_plnftr)

#Code for Serbia #{r} serbia_plnftr <- ess %>% filter(cntry == "RS") %>% select(plnftr)

#```{r} serbia_plnftr\(y <- serbia_plnftr\)plnftr

table(serbia_plnftr$y)

#Finding the mean and the median for Serbia 
#```{r}
serbia_plnftr %>%
  summarize(
    mean_y = mean(y, na.rm = TRUE),
    median_y = median(y, na.rm = TRUE)
  ) %>%
  print()

#Calculating the mode of serbia #```{r} mode_y <- serbia_plnftr %>% filter(!is.na(y)) %>% count(y) %>% arrange(desc(n)) %>% slice(1) %>% pull(y)

cat(“of Y:”, mode_y, “”)

#Calculating the standard deviation
#```{r}
sd_y <- sd(serbia_plnftr$y, na.rm = TRUE)
cat("Standard Deviation of 'y':", sd_y, "\n")

#Calculating the summary function of Serbia #{r} summary(serbia_plnftr) #Answer: Comparing Portugal to Serbia. Portugals mean and median are different. Portugal has a higher mean and median with 5 and 6 rather than serbia that has 4 in both mean and median. ——————————————————————- ## Task 4 ( I had alot of toruble with this one) ——————————————————————- 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

#writitng the code for gndr

#```{r} italy_data <- ess %>% filter(cntry == “IT”)

Convert gender and lrscale (representing pol ID self-placement from left to right)

italy_data <- italy_data %>% mutate( gndr = case_when( gndr == 1 ~ “Male”, gndr == 2 ~ “Female”, TRUE ~ as.character(gndr) ), lrscale = ifelse(lrscale %in% c(77, 88), NA, lrscale) # Convert lrscale values )


#writing the code for stfdem
#```{r}
italy_data %>%
  # Setting values to NA
  mutate(stfdem= ifelse(stfdem %in% c(77, 88, 99), NA, stfdem),
         gndr = ifelse(gndr %in% c(77, 88, 99), NA, gndr)) %>%
  # Reshaping the data
  select(stfdem, gndr) %>%
  gather(variable, value, c(stfdem, gndr)) %>%
  # Creating the boxplot
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  labs(y = "Y-axis", x = "X-axis", title = "Boxplot of stfdem vs. gndr") +
  theme_minimal()

Answer: women are more happy then the men.

## Task 5

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.

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.

#{r} 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() Answer: The french people are more satisitifed with the healthcare system as stfhlth had a median of 6.5 and stfedu had a median of 5,0. (I did graph it but im not sure why the graph is not showng up)