rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 524401 28.1 1163570 62.2 NA 669437 35.8
## Vcells 966213 7.4 8388608 64.0 16384 1851648 14.2
# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
# 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.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")
table(ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
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]
}
#Task 1
italy_data <- ess %>%
filter(cntry == "IT") %>%
mutate(
netustm = ifelse(netustm == 2, 0, ifelse(netustm %in% c(7, 8, 9), NA, netustm)),
netusoft = ifelse(netusoft %in% c(77, 99, 99), NA, netusoft),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)
)
datasummary_skim(italy_data %>% select(netustm, netusoft, happy))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| netustm | 98 | 21 | 2578.5 | 3204.0 | 0.0 | 240.0 | 8888.0 | |
| netusoft | 8 | 21 | 3.7 | 1.6 | 1.0 | 5.0 | 8.0 | |
| happy | 12 | 1 | 7.0 | 1.8 | 0.0 | 7.0 | 10.0 |
The variable “netustm” measures the duration of Internet usage, showing a broad range with a mean of 2578.5 and a median of 240.0. “netusoft” indicates the frequency of Internet use, with an average of 3.7, suggesting varied usage patterns. The “happy” variable reflects happiness levels; with its mean at 7.0 and a median of 7.0, it showcases a general sense of contentment among the respondents.
#Task2
happy_by_year_italy <- italy_data %>%
group_by(year) %>%
summarize(mean_happy = mean(happy, na.rm = TRUE))
ggplot(happy_by_year_italy, aes(x = year, y = mean_happy)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "How happy are people in Italy (2002-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 blue line indicates a consistent level of happiness, averaging
around 7.5 on a 0-10 scale throughout the years. However, the red dots,
representing specific data points, show that there were moments of
slight deviation from this average, suggesting occasional fluctuations
in the general mood of the population during this period.
#Task3
ess_selected <- ess %>%
filter(cntry %in% c("IT", "FR", "SE")) %>%
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 = "Happiness Level Comparison (Italy, France, Sweden)",
x = "Country",
y = "Scale (0-10)")
print(task3plot)
## Warning: Removed 149 rows containing non-finite values (`stat_boxplot()`).
France exhibits the highest median happiness level, with a narrow
interquartile range indicating less variation in responses. Italy’s
median happiness is slightly lower than France’s, but it has a wider
range of responses, as indicated by the longer box and whiskers. Sweden,
while having a median happiness similar to Italy, has the broadest
interquartile range, showing a greater diversity in happiness levels
among its population.
#Task4
italy_data <- italy_data %>%
mutate(happiness = case_when(
happy == 0 ~ "Extremely unhappy",
happy == 1 ~ "1",
happy == 2 ~ "2",
happy == 3 ~ "3",
happy == 4 ~ "4",
happy == 5 ~ "5",
happy == 6 ~ "6",
happy == 7 ~ "7",
happy == 8 ~ "8",
happy == 9 ~ "9",
happy == 10 ~ "Extremely happy",
happy %in% c(77, 88, 99) ~ NA_character_,
TRUE ~ as.character(happy)
))
table(italy_data$happy)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 81 45 148 278 367 806 1405 2540 2793 982 678
italy_data <- italy_data %>%
mutate(jobpaidever = case_when(
pdjobev == 2 ~ "No",
pdjobev == 1 ~ "Yes",
pdjobev %in% c(6, 7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pdjobev)
))
table(italy_data$jobpaidever)
##
## No Yes
## 2224 2829
happyjob <- datasummary_crosstab(happiness ~ pdjobev, data = italy_data)
happyjob
| happiness | 1 | 2 | 6 | 7 | 8 | 9 | All | |
|---|---|---|---|---|---|---|---|---|
| 1 | N | 19 | 10 | 14 | 1 | 0 | 1 | 45 |
| % row | 42.2 | 22.2 | 31.1 | 2.2 | 0.0 | 2.2 | 100.0 | |
| 2 | N | 64 | 37 | 44 | 2 | 0 | 1 | 148 |
| % row | 43.2 | 25.0 | 29.7 | 1.4 | 0.0 | 0.7 | 100.0 | |
| 3 | N | 110 | 72 | 92 | 4 | 0 | 0 | 278 |
| % row | 39.6 | 25.9 | 33.1 | 1.4 | 0.0 | 0.0 | 100.0 | |
| 4 | N | 124 | 102 | 133 | 5 | 1 | 2 | 367 |
| % row | 33.8 | 27.8 | 36.2 | 1.4 | 0.3 | 0.5 | 100.0 | |
| 5 | N | 313 | 166 | 323 | 4 | 0 | 0 | 806 |
| % row | 38.8 | 20.6 | 40.1 | 0.5 | 0.0 | 0.0 | 100.0 | |
| 6 | N | 422 | 322 | 642 | 16 | 0 | 3 | 1405 |
| % row | 30.0 | 22.9 | 45.7 | 1.1 | 0.0 | 0.2 | 100.0 | |
| 7 | N | 681 | 516 | 1326 | 13 | 1 | 3 | 2540 |
| % row | 26.8 | 20.3 | 52.2 | 0.5 | 0.0 | 0.1 | 100.0 | |
| 8 | N | 618 | 583 | 1578 | 8 | 2 | 4 | 2793 |
| % row | 22.1 | 20.9 | 56.5 | 0.3 | 0.1 | 0.1 | 100.0 | |
| 9 | N | 221 | 230 | 524 | 4 | 0 | 3 | 982 |
| % row | 22.5 | 23.4 | 53.4 | 0.4 | 0.0 | 0.3 | 100.0 | |
| Extremely happy | N | 206 | 154 | 313 | 3 | 0 | 2 | 678 |
| % row | 30.4 | 22.7 | 46.2 | 0.4 | 0.0 | 0.3 | 100.0 | |
| Extremely unhappy | N | 34 | 23 | 21 | 3 | 0 | 0 | 81 |
| % row | 42.0 | 28.4 | 25.9 | 3.7 | 0.0 | 0.0 | 100.0 | |
| All | N | 2829 | 2224 | 5032 | 68 | 6 | 19 | 10178 |
| % row | 27.8 | 21.9 | 49.4 | 0.7 | 0.1 | 0.2 | 100.0 |
table(italy_data$happiness, italy_data$jobpaidever) %>%
cprop()
##
## No Yes All
## 1 0.5 0.7 0.6
## 2 1.7 2.3 2.0
## 3 3.3 3.9 3.6
## 4 4.6 4.4 4.5
## 5 7.5 11.1 9.5
## 6 14.5 15.0 14.8
## 7 23.3 24.2 23.8
## 8 26.3 22.0 23.9
## 9 10.4 7.9 9.0
## Extremely happy 7.0 7.3 7.2
## Extremely unhappy 1.0 1.2 1.1
## Total 100.0 100.0 100.0
People tend to report higher levels of happiness when they have had a paying job, as seen in the categories 5 through 9. However, when it comes to expressing extreme happiness, the data shows a slightly higher percentage (7.3% for “Yes” vs. 7.0% for “No”) of those without a paying job rating themselves as “Extremely happy” compared to those with a job. On the other hand, both groups have nearly equal percentages of respondents who rate themselves as “Extremely unhappy.”
#Task5
italy_clean <- italy_data %>%
filter(!is.na(jobpaidever) & !is.na(happy))
italy_probs <- italy_clean %>%
count(happy, jobpaidever) %>%
group_by(jobpaidever) %>%
mutate(prob = n / sum(n))
ggplot(italy_probs, aes(x = as.factor(happy), y = prob, color = jobpaidever)) +
geom_point() +
geom_line(aes(group = jobpaidever)) +
labs(title = "Conditional Probabilities of Happiness in Italy",
subtitle = "by Ever Having a Paying Job",
x = "Happiness Scale",
y = "Probability") +
theme_minimal()
People who have had a paid job in Italy are more likely to rate their
happiness as an 8. However, when it comes to the highest happiness
rating of 10, those who have never had a paid job show a slightly higher
probability. Overall, in Italy, the likelihood of any given happiness
level remains fairly consistent regardless of whether individuals have
had a paying job or not.