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")
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]
}
uk_data <- ess %>%
filter(cntry == "GB") %>%
mutate(
netusoft = ifelse(netusoft %in% c(7, 8, 9), NA, netusoft),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
trstun = ifelse(trstun %in% c(77, 88, 99), NA, trstun),
)
datasummary_skim(uk_data %>% select(netusoft, happy, trstun))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| netusoft | 6 | 75 | 4.2 | 1.4 | 1.0 | 5.0 | 5.0 | |
| happy | 12 | 0 | 7.5 | 1.9 | 0.0 | 8.0 | 10.0 | |
| trstun | 12 | 9 | 5.1 | 2.4 | 0.0 | 5.0 | 10.0 |
“netusoft” stands for how often people use Internet, 5 represents using everyday. Mean at 4.2 and median at 5 shows that most British use Internet every day. “happy” stands for how happy people are. Median at 8.0 and the leftly skewed distribution shows that British are overall quite happy. “trstun” stands for how much trust people have in UN. Median at 5.0 and the normal distribution shows that British have a mutual level of trust in UN.
happy_by_year <- uk_data %>%
group_by(year) %>%
summarize(mean_happy = mean(happy, na.rm = TRUE))
ggplot(happy_by_year, aes(x = year, y = mean_happy)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "Happiness Level in UK (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.
British’s happiness level overall holds quite steadily at around 7.5 out of 10 during 2002-2018. However, there is a remarkable decrease in 2020, all the way down to the lowest point beginning from 2002, which could be speculated as the result of the pandemic.
ess_selected <- ess %>%
filter(cntry %in% c("GB", "FR", "FI")) %>%
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 happiness (UK, France, Finland)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 89 rows containing non-finite values (`stat_boxplot()`).
The median for three countries are very similar at around 7.8. However, their distributions are quite different. The squeezed size of quartile shows that the distribution of people for these values are very concentrated. Finland has a very small Q1 and is positively skewed, while France has a very small Q3 and is negatively skewed. It menas that for Finland, many people are below average, but a small percentage feel extremely happy, and pulls up the mean. The situation for French is the opposite.
uk_data <- uk_data %>%
mutate(born_in_country = recode(brncntr,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(uk_data$born_in_country)
##
## No Yes
## 2409 18564
table(uk_data$brncntr)
##
## 1 2 7 8
## 18564 2409 3 3
happyborn <- datasummary_crosstab(happy ~ born_in_country, data = uk_data)
happyborn
| happy | No | Yes | All | |
|---|---|---|---|---|
| 0 | N | 13 | 100 | 113 |
| % row | 11.5 | 88.5 | 100.0 | |
| 1 | N | 7 | 105 | 112 |
| % row | 6.2 | 93.8 | 100.0 | |
| 2 | N | 18 | 221 | 240 |
| % row | 7.5 | 92.1 | 100.0 | |
| 3 | N | 45 | 457 | 502 |
| % row | 9.0 | 91.0 | 100.0 | |
| 4 | N | 62 | 608 | 670 |
| % row | 9.3 | 90.7 | 100.0 | |
| 5 | N | 192 | 1376 | 1568 |
| % row | 12.2 | 87.8 | 100.0 | |
| 6 | N | 191 | 1350 | 1541 |
| % row | 12.4 | 87.6 | 100.0 | |
| 7 | N | 484 | 3319 | 3804 |
| % row | 12.7 | 87.3 | 100.0 | |
| 8 | N | 676 | 5422 | 6099 |
| % row | 11.1 | 88.9 | 100.0 | |
| 9 | N | 421 | 3319 | 3740 |
| % row | 11.3 | 88.7 | 100.0 | |
| 10 | N | 292 | 2265 | 2557 |
| % row | 11.4 | 88.6 | 100.0 | |
| All | N | 2409 | 18564 | 20979 |
| % row | 11.5 | 88.5 | 100.0 |
For the variable of being born in the country, a cross-tab table is not informative. As people not born in the country accounts for only 11.5% of the whole population that is MUCH fewer than people who were born in the country, % row does not indicate whether people were not born in the country will be happier/unhappier.
uk_data <- uk_data %>%
mutate(religion = case_when(
rlgblg == 2 ~ "No",
rlgblg == 1 ~ "Yes",
rlgblg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(rlgblg)
))
table(uk_data$religion)
##
## No Yes
## 10923 10020
table(uk_data$happy, uk_data$religion) %>%
cprop()
##
## No Yes All
## 0 0.5 0.5 0.5
## 1 0.6 0.5 0.5
## 2 1.4 0.9 1.1
## 3 2.6 2.2 2.4
## 4 3.9 2.4 3.2
## 5 7.8 7.1 7.5
## 6 7.7 7.0 7.3
## 7 18.7 17.5 18.2
## 8 29.2 29.0 29.1
## 9 17.0 18.8 17.9
## 10 10.6 14.0 12.2
## Total 100.0 100.0 100.0
Column percentages are more indicative of the relationship between religion and happiness level. For example, we can learn that 10.6% of not religious people feel extremely happy (10), while 14% of religious people feel extremely happy, and the fourth column shows that 12.2% of all population feel extremely happy. Investigation of the overall trend needs to be supported by the visualization.
uk_clean <- uk_data %>%
filter(!is.na(religion) & !is.na(happy))
uk_probs <- uk_clean %>%
count(happy, religion) %>%
group_by(religion) %>%
mutate(prob = n / sum(n))
ggplot(uk_probs, aes(x = as.factor(happy), y = prob, color = religion)) +
geom_point() +
geom_line(aes(group = religion)) +
labs(title = "Conditional Probabilities of Happiness",
subtitle = "by Belonging to a Religion",
x = "Happiness Scale",
y = "Probability") +
theme_minimal()
From the graph, we can see that the red line begins higher than the green line, intersects at the scale of 8, and becomes lower than the green line at 9 and 10. Also, there is a small difference between them at 5, 6, and 7. It shows that people belonging to a religion are more likely to feel extremely happy than people who do not. There is small difference between them to feel moderately happy, while peole not belonging to a religion are slightly more likely to feel quite unhappy than people who do.