# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") # 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.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ 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
## 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")
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")
#2b Number of observations
# Filter the dataset for respondents in France in 2018 and 2020
count_2018 <- ess %>%
filter(cntry == "FR" & year == 2018) %>%
nrow()
count_2020 <- ess %>%
filter(cntry == "FR" & year == 2020) %>%
nrow()
# Print the counts
cat("Count of respondents in France in 2018:", count_2018, "\n")
## Count of respondents in France in 2018: 2010
cat("Count of respondents in France in 2020:", count_2020, "\n")
## Count of respondents in France in 2020: 1977
Sample, variables, and main trend continued: #2d)
#2018
varInterest_2018 <- france_data %>%
filter(year == 2018) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
year_born = ifelse(yrbrn %in% c(7777, 8888, 9999), NA,yrbrn))%>%
select(happy, year_born) %>%
group_by(year_born) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
ggplot(varInterest_2018, aes(x= year_born, y=average_happy)) + geom_bar(stat = "identity")+
labs(title = "Distribution of Happiness 2018",
x = "Year Born",
y = "Happy Level")
#2020
varInterest_2020 <- france_data %>%
filter(year == 2020) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
netustm = ifelse(netustm %in% c(6666, 7777, 8888, 9999), NA, netustm),
year_born = ifelse(yrbrn %in% c(7777, 8888, 9999), NA,yrbrn))%>%
select(happy, year_born) %>%
group_by(year_born) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
ggplot(varInterest_2020, aes(x= year_born, y=average_happy)) + geom_bar(stat = "identity")+
labs(title = "Distribution of Happiness 2020",
x = "Year Born",
y = "Happy Level")
## Warning: Removed 1 rows containing missing values (`position_stack()`).
#2e)
# Filter and compute average happy level for all countries
allcountry <- ess %>%
filter(year == 2018) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
group_by(yrbrn) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
# Filter and compute average happy level for France
france_data <- ess %>%
filter(cntry == "FR", year == 2018) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
group_by(yrbrn) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
# Create a data frame with "All Countries" and "France" categories
combined_data <- rbind(
mutate(allcountry, Country = "All Countries"),
mutate(france_data, Country = "France")
)
# Plot the average happy level for "All Countries" and "France" in a grouped bar graph
ggplot(data = combined_data, aes(x = yrbrn, y = average_happy, color = Country)) +
geom_line() +
scale_color_manual(values = c("France" = "red", "All Countries" = "blue")) +
labs(title = "Average Happy Level in 2018",
x = "Year Born",
y = "Average Happy Level") +
scale_x_continuous(limits = c(1900, 2010), breaks = seq(1927, 2005, by = 10))
## Warning: Removed 3 rows containing missing values (`geom_line()`).
# Filter and compute average happy level for all countries
allcountry <- ess %>%
filter(year == 2020) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
group_by(yrbrn) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
# Filter and compute average happy level for France
france_data <- ess %>%
filter(cntry == "FR", year == 2020) %>%
mutate(happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
group_by(yrbrn) %>%
summarize(average_happy = mean(happy, na.rm = TRUE))
# Create a data frame with "All Countries" and "France" categories
combined_data <- rbind(
mutate(allcountry, Country = "All Countries"),
mutate(france_data, Country = "France")
)
# Plot the average happy level for "All Countries" and "France" in a grouped bar graph
ggplot(data = combined_data, aes(x = yrbrn, y = average_happy, color = Country)) +
geom_line() +
scale_color_manual(values = c("France" = "red", "All Countries" = "blue")) +
labs(title = "Average Happy Level in 2020",
x = "Year Born",
y = "Average Happy Level") +
scale_x_continuous(limits = c(1900, 2010), breaks = seq(1927, 2005, by = 10))
## Warning: Removed 5 rows containing missing values (`geom_line()`).
Descriptive statistics: #2f
# happy
france_data_happy <- ess %>%
filter(cntry == "FR",, year %in% c(2018, 2020)) %>%
mutate(
sclmeet = ifelse(sclmeet %in% c(77, 88, 99), NA, sclmeet),
health = ifelse(health %in% c(7, 8, 9), NA, health)
) %>%
select(sclmeet, health)
datasummary_skim(france_data_happy)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| sclmeet | 8 | 0 | 5.2 | 1.4 | 1.0 | 6.0 | 7.0 | |
| health | 6 | 0 | 2.3 | 0.9 | 1.0 | 2.0 | 5.0 |
# netustm
france_data_netustm <- ess %>%
filter(cntry == "FR",, year %in% c(2018, 2020)) %>%
mutate(
chldhhe = ifelse(chldhhe %in% c(6,7,8,9), NA, chldhhe),
estsz = ifelse(estsz %in% c(6,7,8,9), NA, estsz),
marsts = ifelse(marsts %in% c(66,77,88,99), NA, marsts)
) %>%
select(chldhhe, estsz, marsts)
datasummary_skim(france_data_netustm)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| chldhhe | 3 | 34 | 1.4 | 0.5 | 1.0 | 1.0 | 2.0 | |
| estsz | 6 | 9 | 2.8 | 1.5 | 1.0 | 3.0 | 5.0 | |
| marsts | 6 | 46 | 5.2 | 1.2 | 1.0 | 6.0 | 6.0 |
# Filter the data for legal marital statuses 1 to 5
filtered_data <- ess %>%
filter(cntry == "FR", year %in% c(2018, 2020)) %>%
mutate(marsts = ifelse(marsts %in% c(66,77,88,99), NA, marsts),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
filter(marsts %in% 1:5)
# Calculate the mean for happiness levels
mean_happiness <- mean(filtered_data$happy)
# Print the mean
cat("Mean Happiness for Legal Marital Status 1-5: ", mean_happiness, "\n")
## Mean Happiness for Legal Marital Status 1-5: NA
Bivariate statistics and visualizations: #2g) column percentage
# Calculate conditional probabilities for chldhhe and happy in the 2018 round
conditional_prob_2018 <- ess %>%
filter(cntry == "FR", year %in% c(2018)) %>%
mutate(chldhhe = ifelse(chldhhe %in% c(6, 7, 8, 9), NA, chldhhe),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(chldhhe, happy) %>%
table() %>%
cprop()
print(conditional_prob_2018)
## happy
## chldhhe 0 1 2 3 4 5 6 7 8 9 10 All
## 1 80.0 66.7 88.2 68.0 63.2 65.6 67.9 57.8 55.7 56.4 56.7 59.8
## 2 20.0 33.3 11.8 32.0 36.8 34.4 32.1 42.2 44.3 43.6 43.3 40.2
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
# Calculate conditional probabilities for chldhhe and happy in the 2020 round
conditional_prob_2020 <- ess %>%
filter(cntry == "FR", year %in% c(2020)) %>%
mutate(chldhhe = ifelse(chldhhe %in% c(6, 7, 8, 9), NA, chldhhe),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(chldhhe, happy) %>%
table() %>%
cprop()
print(conditional_prob_2020)
## happy
## chldhhe 0 1 2 3 4 5 6 7 8 9 10 All
## 1 100.0 100.0 62.5 64.7 50.0 57.4 51.0 51.1 59.7 56.5 55.9 56.3
## 2 0.0 0.0 37.5 35.3 50.0 42.6 49.0 48.9 40.3 43.5 44.1 43.7
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
# Calculate conditional probabilities for estsz and happy in the 2018 round
conditional_prob_2018 <- ess %>%
filter(cntry == "FR", year %in% c(2018)) %>%
mutate(estsz = ifelse(estsz %in% c(6,7,8,9), NA, estsz),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(estsz, happy) %>%
table() %>%
cprop()
print(conditional_prob_2018)
## happy
## estsz 0 1 2 3 4 5 6 7 8 9 10 All
## 1 60.0 33.3 36.4 32.4 34.6 33.5 28.7 27.6 30.9 25.0 46.8 31.2
## 2 0.0 33.3 13.6 5.9 23.1 12.6 15.5 13.3 12.2 13.1 7.6 12.7
## 3 20.0 33.3 36.4 17.6 11.5 20.4 23.6 20.8 20.0 20.3 13.9 20.0
## 4 20.0 0.0 4.5 32.4 13.5 18.0 19.0 16.9 18.0 16.1 14.6 17.3
## 5 0.0 0.0 9.1 11.8 17.3 15.5 13.2 21.4 18.9 25.4 17.1 18.8
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
# Calculate conditional probabilities for estsz and happy in the 2020 round
conditional_prob_2020 <- ess %>%
filter(cntry == "FR", year %in% c(2020)) %>%
mutate(estsz = ifelse(estsz %in% c(6,7,8,9), NA, estsz),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(estsz, happy) %>%
table() %>%
cprop()
print(conditional_prob_2020)
## happy
## estsz 0 1 2 3 4 5 6 7 8 9 10 All
## 1 66.7 50.0 57.1 19.0 38.9 31.9 27.0 30.5 30.8 30.7 34.5 31.3
## 2 0.0 16.7 0.0 19.0 8.3 16.9 14.9 13.0 13.8 13.8 15.8 14.0
## 3 16.7 16.7 7.1 14.3 16.7 23.1 22.3 17.2 21.3 20.9 22.2 20.3
## 4 0.0 16.7 14.3 28.6 22.2 16.2 18.2 19.5 17.4 20.1 13.5 18.0
## 5 16.7 0.0 21.4 19.0 13.9 11.9 17.6 19.8 16.7 14.6 14.0 16.4
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
# Calculate conditional probabilities for estsz and happy in the 2018 round
conditional_prob_2018 <- ess %>%
filter(cntry == "FR", year %in% c(2018)) %>%
mutate(marsts = ifelse(marsts %in% c(66,77,88,99), NA, marsts),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(marsts, happy) %>%
table() %>%
cprop()
print(conditional_prob_2018)
## happy
## marsts 0 1 2 3 4 5 6 7 8 9 10 All
## 1 20.0 0.0 9.5 16.7 0.0 3.6 4.2 2.0 2.0 4.2 5.8 3.4
## 2 0.0 0.0 0.0 4.2 2.1 0.6 0.8 0.4 1.0 0.0 0.0 0.7
## 4 0.0 50.0 28.6 12.5 29.2 34.3 27.7 22.7 22.2 19.5 25.6 24.8
## 5 60.0 25.0 28.6 33.3 18.8 20.1 21.8 18.4 16.2 13.6 15.1 18.4
## 6 20.0 25.0 33.3 33.3 50.0 41.4 45.4 56.6 58.6 62.7 53.5 52.7
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
# Calculate conditional probabilities for estsz and happy in the 2020 round
conditional_prob_2020 <- ess %>%
filter(cntry == "FR", year %in% c(2020)) %>%
mutate(marsts = ifelse(marsts %in% c(66,77,88,99), NA, marsts),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(marsts, happy) %>%
table() %>%
cprop()
print(conditional_prob_2020)
## happy
## marsts 0 1 2 3 4 5 6 7 8 9 10 All
## 1 0.0 0.0 10.0 6.2 3.3 3.3 8.3 2.5 3.5 2.8 8.5 4.2
## 2 0.0 0.0 0.0 0.0 3.3 0.8 0.0 0.4 0.4 0.0 0.0 0.4
## 4 57.1 60.0 30.0 25.0 20.0 21.5 24.8 16.5 22.2 20.8 15.9 20.9
## 5 14.3 0.0 20.0 25.0 10.0 17.4 11.9 10.6 8.8 4.7 8.5 10.5
## 6 28.6 40.0 40.0 43.8 63.3 57.0 55.0 69.9 65.1 71.7 67.1 64.0
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
#2h
france_child_data <- ess %>%
filter(cntry == "FR", year %in% c(2018, 2020)) %>%
mutate(chldhhe2 = ifelse(chldhhe %in% c(6,7,8,9), NA, chldhhe),
happy2 = ifelse(happy %in% c(77, 88, 99), NA, happy)) %>%
select(year, happy2, chldhhe2)%>%
group_by(chldhhe2) %>%
summarize(average_happy = mean(happy2, na.rm = TRUE))
# Remove rows with NA values in chldhhe2
france_child_data <- france_child_data[!is.na(france_child_data$chldhhe2), ]
# Create a ggplot to compare the relationship between having or child or not and happy level
plot <- ggplot(france_child_data, aes(x = factor(chldhhe2), y = average_happy)) +
geom_bar(stat = "identity", width = 0.5) +
labs(title = "Comparison of Average Happiness Levels Between Parents and Non-Parents
in France (2018-2020)",
x = "Having child or not",
y = "Average Happy Level") +
scale_x_discrete(breaks = c("1", "2"), labels = c("Yes", "No"))
print(plot)