–metadata title= Homework_5_Zidao_Xu

Zidao Xu (Jasmine), 1008963775

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")


new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ 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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")

Task 1

ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  ess$year[ess$essround == i] <- replacements[i]
}

france_data <- ess %>% 
  filter(cntry == "FR") %>% 
  mutate(
    happy = ifelse(happy == 2, 0, ifelse(happy %in% c(7, 8, 9), NA, happy)), 
    netuse = ifelse(netuse %in% c(77, 88, 99), NA, netuse), 
    yrbrn = ifelse(yrbrn %in% c(77, 88, 99), NA, yrbrn),
  )

datasummary_skim(france_data %>% select(happy, netuse, yrbrn))
Unique (#) Missing (%) Mean SD Min Median Max
happy 10 63 6.3 5.3 0.0 5.0 88.0
netuse 9 70 4.0 3.1 0.0 6.0 7.0
yrbrn 100 0 1964.1 116.4 1905.0 1962.0 8888.0

There are huge missing data of “happy” which stands how happy are a person and “netuse”, the personal use of internet/email/www. However, “happy” has a broader range of values and a wider spread. The median of netuse is higher then the mean which may show that the majority of people spending time on internet might not be that nuch, but there are some relatively high values that are affecting the median, which may refer that certain amount of people spend lots of time on internet. “yrbrn” contains no missing data and represents a broad range of birth years.

Task 2

happy_by_year <- france_data %>%
  group_by(year) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))
happy_by_year
## # A tibble: 10 × 2
##     year mean_happy
##    <dbl>      <dbl>
##  1  2002       6.32
##  2  2004       6.29
##  3  2006       6.35
##  4  2008       6.04
##  5  2010       5.94
##  6  2012       6.01
##  7  2014       6.21
##  8  2016       6.21
##  9  2018       6.97
## 10  2020       6.71
ggplot(happy_by_year, aes(x = year, y = mean_happy)) +
  geom_line(color = "blue", size = 1) + 
  geom_point(color = "red", size = 3) + 
  labs(title = "How happy are you in France (2010-2020)", 
       x = "Survey Year", 
       y = "Happy (0-10)") +
  ylim(0, 10) + 
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

The mean for the happiness of people in France is quite stable, continous, since they kept getting the value around 7.5 from 2010 to 2020. And the value is relatively high, compared to the full amount of value is 10.

Task 3

ess_selected <- ess %>%
  filter(cntry %in% c("FR", "DE", "NO")) %>%
  mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -happy, FUN=median), y = happy, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for how happy are you (France, Ferman, Norway)", 
       x = "Country", 
       y = "Scale (0-10)")

task3plot
## Warning: Removed 242 rows containing non-finite values (`stat_boxplot()`).

The mdeian of France is almost as great as it’s max number, shows on average, people in France report relatively high happiness levels, and there might be fewer extremely low happiness scores in the dataset. And the IQR of the French boxplot is more concentrate, compared to German’s. This also indicate a more consistent level of happiness among the population.

For German, the IQR is high compared with the rest of the two countries, which tells that the population’s happiness scores in Germany cover a wide range. The median of happiness level of Germanys is positioned at a high status in the IQR boxplot, so generally, people in Germany may report higher levels of happiness.

As the boxplot shown by Norway, they have the highest Q1 and Q3 compared with Germany and French, which means people in Norway are happier than Germans and Frances in a general way. Their median of happiness level stays around the midpoint in the IQR graph, a balanced spread of happiness scores. And IQR value is relatively small compared to Germany, shows that most people in Norway report happiness levels that are clustered closely together.

Task 4

france_data <- france_data %>%
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    )
  )

clsprtyedu <- datasummary_crosstab(happy ~ educ_level, data = france_data)

clsprtyedu
happy BA No BA All
0 N 34 241 275
% row 12.4 87.6 100.0
1 N 3 54 57
% row 5.3 94.7 100.0
3 N 47 299 346
% row 13.6 86.4 100.0
4 N 66 448 514
% row 12.8 87.2 100.0
5 N 276 2073 2349
% row 11.7 88.3 100.0
6 N 351 1518 1869
% row 18.8 81.2 100.0
10 N 293 1329 1622
% row 18.1 81.9 100.0
77 N 3 11 14
% row 21.4 78.6 100.0
88 N 1 11 12
% row 8.3 91.7 100.0
All N 4235 14803 19038
% row 22.2 77.8 100.0
france_data <- france_data %>%
  mutate(geo = recode(as.character(domicil), 
                      '1' = "Urban", 
                      '2' = "Peri-Urban", # or set to Urban | Regardless decision needs to be justified
                      '3' = "Rural", 
                      '4' = "Rural", 
                      '5' = "Rural",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))

table(france_data$geo)
## 
## Peri-Urban      Rural      Urban 
##       2361      13087       3584
table(france_data$happy, france_data$geo) %>%
  cprop()
##        
##         Peri-Urban Rural Urban All  
##   0       3.0        4.1   3.6   3.9
##   1       0.4        0.8   0.9   0.8
##   3       5.1        4.7   5.6   4.9
##   4       7.5        7.4   6.9   7.3
##   5      31.4       33.2  34.9  33.3
##   6      27.1       26.3  26.7  26.5
##   10     25.4       23.2  20.6  22.9
##   77      0.0        0.2   0.4   0.2
##   88      0.1        0.1   0.4   0.2
##   Total 100.0      100.0 100.0 100.0

The first graph shows the distribution of happiness levels for each education level, and people who have a bachelor degree seems happier

The second dataset shows how happiness levels are distributed across different geographical regions: “Peri-Urban,” “Rural,” “Urban,” and “All.” Urban and Peri-Urban usually has the higher happy value than Rural and All.

Task 5

df <- france_data %>%
  filter(!is.na(geo) & !is.na(happy))

df <- df %>%
  mutate(happy = case_when(
    happy == 1 ~ "Yes",
    happy == 0 ~ "No",
    TRUE ~ as.character(happy)
  ))

table(df$happy)
## 
##   10    3    4    5    6   77   88   No  Yes 
## 1619  346  514 2349 1869   14   12  275   57
table(df$happy, df$geo) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="How happy are you in France",
       y = "Conditional Percentage",
       x = "How happy are you",
       fill = "Rural vs. Urban vs. Peri-Urban")

france_clean <- france_data %>%
  filter(!is.na(geo) & !is.na(happy))

france_probs <- france_clean %>%
  count(happy, geo) %>%
  group_by(geo) %>%
  mutate(prob = n / sum(n))

ggplot(france_probs, aes(x = as.factor(happy), y = prob, color = geo)) +
  geom_point() +
  geom_line(aes(group = geo)) +
  labs(title = "Conditional Probabilities of How Happy Are You",
       subtitle = "by Place of Residence",
       x = "Happy Scale", 
       y = "Probability") +
  theme_minimal()

In the graph it significantly shows most people in France has a happy scale of 8, and the peak value is achieved by Peri-Urban people. Not many people choose they are extremely happy or not happy at all in all three palces. However according to the general information, it could tell the scores of happiness level in France is not very happiness scores, with a very high median.