# List of packages
packages <- c("fst", "dplyr", "modelsummary", "ggplot2", "equatiomatic", "effects", "interactions", "car")
options(warn = -1)
# Install any missing packages
new_packages <- setdiff(packages, installed.packages()[,"Package"])
if(length(new_packages)) install.packages(new_packages)
# Load packages quietly
lapply(packages, function(pkg) {
suppressPackageStartupMessages(require(pkg, character.only = TRUE))
})
## [[1]]
## [1] TRUE
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
##
## [[5]]
## [1] TRUE
##
## [[6]]
## [1] TRUE
##
## [[7]]
## [1] TRUE
##
## [[8]]
## [1] TRUE
# Read FST file
ess <- read_fst("All-ESS-Data.fst") # Note: This step will not work if the working directory is not set correctly
# Rename the data frame to df for future use
df <- ess
# Try another way and display the first five columns
first_five_cols <- ess[, 1:5]
head(first_five_cols)
## name essround edition proddate cntry
## 1 ESS1e06_6 1 6.6 01.12.2018 AT
## 2 ESS1e06_6 1 6.6 01.12.2018 AT
## 3 ESS1e06_6 1 6.6 01.12.2018 AT
## 4 ESS1e06_6 1 6.6 01.12.2018 AT
## 5 ESS1e06_6 1 6.6 01.12.2018 AT
## 6 ESS1e06_6 1 6.6 01.12.2018 AT
# Get all unique values of a specific column
unique_values <- unique(ess$cntry)
# Display unique values
print(unique_values)
## [1] "AT" "BE" "CH" "CZ" "DE" "DK" "ES" "FI" "FR" "GB" "GR" "HU" "IE" "IL" "IT"
## [16] "LU" "NL" "NO" "PL" "PT" "SE" "SI" "EE" "IS" "SK" "TR" "UA" "BG" "CY" "RU"
## [31] "HR" "LV" "RO" "LT" "AL" "XK" "ME" "RS" "MK"
# Note: The $ operator is used to refer to the content named "ESS" in the dataset. I want to see the content in the specific "X" column.
# Display the frequency table for the 'cntry' column
table(ess$cntry)
##
## AL AT BE BG CH CY CZ DE DK EE ES FI FR
## 1201 15225 17451 13240 16925 6065 20090 34425 12408 16856 19452 19532 19038
## GB GR HR HU IE IL IS IT LT LU LV ME MK
## 20979 12558 6535 16642 22233 16218 3975 10178 11652 3187 3921 2478 1429
## NL NO PL PT RO RS RU SE SI SK TR UA XK
## 18329 16065 17689 17881 2146 3548 12458 18216 13484 11292 4272 9987 1295
# Extract data for Germany ('GB') and display the class of the 'happy' column
GB_data <- ess[ess$cntry == "GB", ]
class(GB_data$happy)
## [1] "numeric"
# Display the frequency table for the 'happy' column in the Germany data
table(GB_data$happy)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 113 112 240 502 670 1568 1541 3804 6099 3740 2557 7 26
# We will use the following approach
library(dplyr)
summaries <- GB_data %>%
summarise(
mean_iscoco = mean(iscoco, na.rm = TRUE),
median_iscoco = median(iscoco, na.rm = TRUE),
na_iscoco = sum(is.na(iscoco)),
# ... (similar summarization for other columns)
mean_sclmeet = mean(sclmeet, na.rm = TRUE),
median_sclmeet = median(sclmeet, na.rm = TRUE),
na_sclmeet = sum(is.na(sclmeet))
)
print(summaries)
## mean_iscoco median_iscoco na_iscoco mean_sclmeet median_sclmeet na_sclmeet
## 1 8224.201 5123 9862 4.99795 5 0
library(modelsummary)
# Select specific columns from GB_data
# Create an NA column of the same length as GB_data rows
GB_data$year <- NA
# Fill the 'year' column based on conditions
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for (i in 1:10) {
GB_data$year[GB_data$essround == i] <- replacements[i]
}
# Select specific columns and create a new dataset
new_dataset <- GB_data %>%
select(happy, wkhct, wkhtot, wkhtotp, wrkctra, sclact, sclmeet, hinctnt, year)
str(new_dataset)
## 'data.frame': 20979 obs. of 9 variables:
## $ happy : num 7 6 5 9 10 5 9 8 5 3 ...
## $ wkhct : num 35 25 38 38 36 38 888 13 40 60 ...
## $ wkhtot : num 50 25 38 40 36 40 60 13 888 80 ...
## $ wkhtotp: num 666 666 666 666 45 666 72 40 666 36 ...
## $ wrkctra: num NA NA NA NA NA NA NA NA NA NA ...
## $ sclact : num 3 3 2 4 3 3 3 3 2 3 ...
## $ sclmeet: num 6 6 6 6 6 5 6 3 5 4 ...
## $ hinctnt: num 10 88 4 10 10 77 12 8 4 77 ...
## $ year : num 2002 2002 2002 2002 2002 ...
# This package allows for a lot of functional and aesthetic adjustments
datasummary_skim(new_dataset, output = "flextable")
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
happy | 13 | 0 | 7.6 | 3.6 | 0.0 | 8.0 | 88.0 |
wkhct | 100 | 0 | 97.6 | 207.5 | 0.0 | 37.0 | 999.0 |
wkhtot | 110 | 0 | 86.2 | 181.6 | 0.0 | 40.0 | 999.0 |
wkhtotp | 90 | 0 | 471.5 | 294.5 | 0.0 | 666.0 | 999.0 |
wrkctra | 8 | 10 | 2.3 | 2.0 | 1.0 | 1.0 | 9.0 |
sclact | 7 | 0 | 2.8 | 1.1 | 1.0 | 3.0 | 8.0 |
sclmeet | 9 | 0 | 5.0 | 2.7 | 1.0 | 5.0 | 88.0 |
hinctnt | 16 | 70 | 21.6 | 30.1 | 1.0 | 8.0 | 99.0 |
year | 10 | 0 | 2010.6 | 5.4 | 2002.0 | 2010.0 | 2020.0 |
# Assume GB_data has a column named year
# Draw a boxplot
ggplot(new_dataset, aes(x = factor(year), y = happy)) +
geom_boxplot() +
labs(title = "Distribution of Happy Index in Different Years",
x = "Year",
y = "Happy Index") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
# Draw a scatter plot to show the relationship between happy and wkhct
ggplot(new_dataset, aes(x = wkhct, y = happy)) +
geom_point() +
labs(title = "Relationship Between Happy Index and Working Hours",
x = "Working Hours",
y = "Happy Index") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) # Ensure the title is centered
# Data Cleaning
# Process the wkhct column
new_dataset <- new_dataset %>%
mutate(wkhct = case_when(
wkhct %in% c(666, 777, 888, 999) ~ NA_real_, # Map specified values to missing values
TRUE ~ wkhct # Keep other values unchanged
))
# Process the wkhtot column
new_dataset <- new_dataset %>%
mutate(wkhtot = case_when(
wkhtot %in% c(666, 777, 888, 999) ~ NA_real_,
TRUE ~ wkhtot
))
# Process the wkhtotp column
new_dataset <- new_dataset %>%
mutate(wkhtotp = case_when(
wkhtotp %in% c(666, 777, 888, 999) ~ NA_real_,
TRUE ~ wkhtotp
))
# Process the wrkctra column
new_dataset <- new_dataset %>%
mutate(wrkctra = case_when(
wrkctra %in% c(6, 7, 8, 9) ~ NA_real_,
TRUE ~ wrkctra
))
# Print the processed data frame
# Assume the data frame is named new_dataset
# Load the dplyr package
library(dplyr)
# Process the sclact column
new_dataset <- new_dataset %>%
mutate(sclact = case_when(
sclact %in% c(7, 8, 9) ~ NA_real_, # Map specified values to missing values
TRUE ~ sclact # Keep other values unchanged
))
# Process the sclmeet column
new_dataset <- new_dataset %>%
mutate(sclmeet = case_when(
sclmeet %in% c(77, 88, 99) ~ NA_real_,
TRUE ~ sclmeet
))
# Process the hinctnt column
new_dataset <- new_dataset %>%
mutate(hinctnt = case_when(
hinctnt %in% c(77, 88, 99) ~ NA_real_, # Map specified values to missing values
TRUE ~ hinctnt # Keep other values unchanged
))
# Standardize all variables except 'happy'
cols_to_scale <- setdiff(names(new_dataset), c("happy"))
new_dataset_norm <- new_dataset
new_dataset_norm[cols_to_scale] <- scale(new_dataset_norm[cols_to_scale])
# Create a new dataset
extracted_dataset <- new_dataset_norm %>%
select(happy, wkhct, wkhtot, wkhtotp, wrkctra)
# Create new datasets
extracted_dataset2 <- new_dataset %>%
select(happy, sclact, sclmeet)
extracted_dataset3 <- new_dataset %>%
select(happy, hinctnt)
extracted_dataset4 <- new_dataset %>%
select(happy, wkhct, wkhtot, wkhtotp, wrkctra, sclact)
# Remove rows with missing values
new_dataset_norm1 <- na.omit(extracted_dataset)
new_dataset_norm2 <- na.omit(extracted_dataset2)
new_dataset_norm3 <- na.omit(extracted_dataset3)
new_dataset_norm4 <- na.omit(extracted_dataset4)
# Calculate the 0.75 quantile of wkhtotp
quantile_hinctnt <- quantile(new_dataset_norm3$hinctnt, 0.75, na.rm = TRUE)
# Create a subset based on conditions
subset_cleaned_data <- subset(new_dataset_norm3, hinctnt > quantile_hinctnt)
# 1. Test for linear relationships: Visualize through scatterplots
pairs(new_dataset_norm[, c("happy", "wkhct", "wkhtot", "wkhtotp", "wrkctra", "sclact", "sclmeet", "hinctnt")])
# 2. Test for homoscedasticity: Residuals vs. Fitted values plot
plot(lm_model1$fitted.values, lm_model1$residuals)
abline(h = 0, col = "red")
plot(lm_model2$fitted.values, lm_model2$residuals)
abline(h = 0, col = "red")
plot(lm_model3$fitted.values, lm_model3$residuals)
abline(h = 0, col = "red")
plot(model4$fitted.values, model4$residuals)
abline(h = 0, col = "red")
# 3. Q-Q plot: Check normal distribution of errors
qqPlot(lm_model1$residuals, main = "Q-Q Plot for Model 1")
## 405849 317809
## 4560 3252
qqPlot(lm_model2$residuals, main = "Q-Q Plot for Model 2")
## 18967 110212
## 230 507
qqPlot(lm_model3$residuals, main = "Q-Q Plot for Model 3")
## 112200 265684
## 6121 11624
qqPlot(model4$residuals, main = "Q-Q Plot for Model 4")
## 317809 405849
## 3241 4541
models <- list(
"Model 1" = lm(happy ~wkhct + wkhtot +wkhtot + wkhtotp + wrkctra, data = extracted_dataset),
"Model 2" = lm(happy ~ hinctnt, data = subset_cleaned_data),
"Model 3" = lm(happy ~sclact + sclmeet, data = new_dataset_norm2),
"Model 4" = lm(happy ~wkhct + wkhtot +wkhct*sclact+wkhtot + wkhtotp + wrkctra, data = new_dataset_norm4)
)
modelsummary(models)
| Model 1 | Model 2 | Model 3 | Model 4 |
|---|---|---|---|---|
(Intercept) | 7.773 | 7.565 | 6.118 | 7.214 |
(0.034) | (0.705) | (0.086) | (0.282) | |
wkhct | -0.028 | -0.005 | ||
(0.059) | (0.007) | |||
wkhtot | -0.025 | -0.002 | ||
(0.046) | (0.003) | |||
wkhtotp | -0.021 | -0.002 | ||
(0.035) | (0.003) | |||
wrkctra | -0.084 | -0.088 | ||
(0.040) | (0.051) | |||
hinctnt | 0.022 | |||
(0.066) | ||||
sclact | 0.299 | 0.320 | ||
(0.026) | (0.082) | |||
sclmeet | 0.129 | |||
(0.016) | ||||
wkhct × sclact | 0.001 | |||
(0.002) | ||||
Num.Obs. | 4848 | 859 | 20840 | 4829 |
R2 | 0.001 | 0.000 | 0.015 | 0.020 |
R2 Adj. | 0.000 | -0.001 | 0.015 | 0.019 |
AIC | 21886.7 | 3149.8 | 109447.1 | 21716.2 |
BIC | 21925.7 | 3164.0 | 109478.9 | 21768.1 |
Log.Lik. | -10937.373 | -1571.880 | -54719.543 | -10850.096 |
F | 1.346 | 0.114 | 160.106 | 16.782 |
RMSE | 2.31 | 1.51 | 3.34 | 2.29 |
modelsummary(models, vcov = "robust")
| Model 1 | Model 2 | Model 3 | Model 4 |
|---|---|---|---|---|
(Intercept) | 7.773 | 7.565 | 6.118 | 7.214 |
(0.035) | (0.725) | (0.102) | (0.388) | |
wkhct | -0.028 | -0.005 | ||
(0.051) | (0.006) | |||
wkhtot | -0.025 | -0.002 | ||
(0.043) | (0.003) | |||
wkhtotp | -0.021 | -0.002 | ||
(0.043) | (0.003) | |||
wrkctra | -0.084 | -0.088 | ||
(0.040) | (0.052) | |||
hinctnt | 0.022 | |||
(0.069) | ||||
sclact | 0.299 | 0.320 | ||
(0.029) | (0.075) | |||
sclmeet | 0.129 | |||
(0.019) | ||||
wkhct × sclact | 0.001 | |||
(0.002) | ||||
Num.Obs. | 4848 | 859 | 20840 | 4829 |
R2 | 0.001 | 0.000 | 0.015 | 0.020 |
R2 Adj. | 0.000 | -0.001 | 0.015 | 0.019 |
AIC | 21886.7 | 3149.8 | 109447.1 | 21716.2 |
BIC | 21925.7 | 3164.0 | 109478.9 | 21768.1 |
Log.Lik. | -10937.373 | -1571.880 | -54719.543 | -10850.096 |
F | 1.112 | 0.107 | 135.667 | 19.972 |
RMSE | 2.31 | 1.51 | 3.34 | 2.29 |
Std.Errors | HC3 | HC3 | HC3 | HC3 |
models <- list(
"Model 1" = lm(happy ~wkhct + wkhtot +wkhtot + wkhtotp + wrkctra, data = extracted_dataset),
"Model 2" = lm(happy ~ hinctnt, data = subset_cleaned_data),
"Model 3" = lm(happy ~sclact + sclmeet, data = new_dataset_norm2),
"Model 4" = lm(happy ~wkhct + wkhtot +wkhct*sclact+wkhtot + wkhtotp + wrkctra, data = new_dataset_norm4))
modelplot(models, coef_omit = 'Interc')
library(equatiomatic)
equatiomatic::extract_eq(lm_model1, use_coefs = TRUE)
## Registered S3 methods overwritten by 'broom':
## method from
## tidy.glht jtools
## tidy.summary.glht jtools
\[ \operatorname{\widehat{happy}} = 7.77 - 0.03(\operatorname{wkhct}) - 0.03(\operatorname{wkhtot}) - 0.02(\operatorname{wkhtotp}) - 0.08(\operatorname{wrkctra}) \]
equatiomatic::extract_eq(lm_model2, use_coefs = TRUE)
\[ \operatorname{\widehat{happy}} = 7.57 + 0.02(\operatorname{hinctnt}) \]
equatiomatic::extract_eq(lm_model3, use_coefs = TRUE)
\[ \operatorname{\widehat{happy}} = 6.12 + 0.3(\operatorname{sclact}) + 0.13(\operatorname{sclmeet}) \]
modelsummary(
list(lm_model1, lm_model2, lm_model3,model4),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | (2) | (3) | (4) |
|---|---|---|---|---|
wkhct | 0.0 (0.1) | 0.0 (0.0) | ||
wkhtot | 0.0 (0.0) | 0.0 (0.0) | ||
wkhtotp | 0.0 (0.0) | 0.0 (0.0) | ||
wrkctra | -0.1 (0.0)* | -0.1 (0.1)+ | ||
hinctnt | 0.0 (0.1) | |||
sclact | 0.3 (0.0)*** | 0.3 (0.1)*** | ||
sclmeet | 0.1 (0.0)*** | |||
wkhct × sclact | 0.0 (0.0) | |||
Num.Obs. | 4848 | 859 | 20840 | 4829 |
R2 | 0.001 | 0.000 | 0.015 | 0.020 |
R2 Adj. | 0.000 | -0.001 | 0.015 | 0.019 |
AIC | 21886.7 | 3149.8 | 109447.1 | 21716.2 |
BIC | 21925.7 | 3164.0 | 109478.9 | 21768.1 |
Log.Lik. | -10937.373 | -1571.880 | -54719.543 | -10850.096 |
F | 1.346 | 0.114 | 160.106 | 16.782 |
RMSE | 2.31 | 1.51 | 3.34 | 2.29 |
interaction_plot <- effect("wkhct*sclact", model4, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect",
xlab="wkhct",
ylab="SWB")
interaction_plot
##
## wkhct*sclact effect
## sclact
## wkhct 1 2 3 4 5
## 0 7.283075 7.603304 7.923533 8.243762 8.563990
## 100 6.937221 7.393636 7.850051 8.306466 8.762881
## 300 6.245511 6.974299 7.703086 8.431874 9.160661
## 400 5.899657 6.764630 7.629604 8.494578 9.359552
## 600 5.207947 6.345293 7.482640 8.619986 9.757332