Research Poster Group Member: Yiran Chen, Doris Wu, Erica Liu
Load the packages and read ess data.
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "kableExtra", "flextable", "equatiomatic")
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: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
##
## Loading required package: grid
##
## Loading required package: Matrix
##
##
## Attaching package: 'Matrix'
##
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
##
## Loading required package: survival
##
##
## Attaching package: 'survey'
##
##
## The following object is masked from 'package:graphics':
##
## dotchart
##
##
##
## Attaching package: 'MASS'
##
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
##
## Attaching package: 'flextable'
##
##
## The following objects are masked from 'package:kableExtra':
##
## as_image, footnote
##
##
## The following object is masked from 'package:purrr':
##
## compose
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[6]]
## [1] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "modelsummary" "fst" "infer" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[7]]
## [1] "MASS" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "modelsummary" "fst" "infer"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[8]]
## [1] "kableExtra" "MASS" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "modelsummary" "fst"
## [11] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[9]]
## [1] "flextable" "kableExtra" "MASS" "survey" "survival"
## [6] "Matrix" "grid" "effects" "carData" "modelsummary"
## [11] "fst" "infer" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
ess <- read_fst("All-ESS-Data.fst")
setwd("/Users/chenyiran/Desktop/SOC202")
Filter for our target country Italy, and clean the variables 1) outcome variable: life satisfaction 2) predictor: whether belongs to a religion, 3) the interactive variable: subjective health.
italy_data <- ess %>%
filter(cntry == "IT") %>%
mutate(
stflife = ifelse(stflife %in% c(77, 88, 99), NA, stflife),
rlgblg = ifelse(rlgblg %in% c(7, 8, 9), NA, rlgblg),
health = ifelse(health %in% c(7, 8, 9), NA, health)
)
italy_data <- italy_data %>% filter(!is.na(stflife))
italy_data <- italy_data %>% filter(!is.na(rlgblg))
italy_data <- italy_data %>% filter(!is.na(health))
Create a data summary table. Rename the variables for clarity, and save this table.
italy_data_table <- italy_data %>%
rename(
`Satisfaction with Life` = stflife,
`Belonging to a Religion` = rlgblg,
`Subjective General Health` = health
)
summary_table <- datasummary_skim (italy_data_table %>% dplyr::select(`Satisfaction with Life`, `Belonging to a Religion`, `Subjective General Health`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Satisfaction with Life | 11 | 0 | 6.9 | 2.1 | 0.0 | 7.0 | 10.0 |
Belonging to a Religion | 2 | 0 | 1.2 | 0.4 | 1.0 | 1.0 | 2.0 |
Subjective General Health | 5 | 0 | 2.2 | 0.9 | 1.0 | 2.0 | 5.0 |
flextable::save_as_docx(summary_table, path = "summary_table.docx",
width = 7.0, height = 7.0)
recode rlgblg
italy_data <- italy_data %>%
mutate( rlgblg = case_when(
rlgblg == 1 ~ "Yes",
rlgblg == 2 ~ "No"))
Section 1 Null Hypothesis Testing
Step 1: Calculate the Test Statistic
test_stat <- italy_data %>%
specify(explanatory = rlgblg,
response = stflife) %>%
hypothesize(null = "independence") %>%
calculate(stat = "t")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "No" - "Yes", or divided in the order "No" / "Yes" for ratio-based
## statistics. To specify this order yourself, supply `order = c("No", "Yes")` to
## the calculate() function.
print(test_stat$stat)
## t
## -4.218662
Choice of test statistics: We use the two sample T test because our explanaratory variable (belong to religion Yes or No) is a categorical variable with two categories, and our reponse variable (life satisfaction from 0-10) is one numerical variable.
Interpretation: The t-statistic measures the difference between the observed sample mean and the population mean in units of standard error. The larger the magnitude of t, the greater the evidence against the null hypothesis.
A t-statistic of 4.22 is not small, indicating a slight difference in life satisfaction between those belonging to a religion and those not belonging to a religion. It suggests that the mean level of life satisfaction is slightly different for the two groups.
Step 2: Simulate the Null Distribution
null_distribution <- italy_data %>%
specify(explanatory = rlgblg,
response = stflife) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "t")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "No" - "Yes", or divided in the order "No" / "Yes" for ratio-based
## statistics. To specify this order yourself, supply `order = c("No", "Yes")` to
## the calculate() function.
Step 3: Calculate the p-value
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
Step 4: Visualize
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
null_distribution
## Response: stflife (numeric)
## Explanatory: rlgblg (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.775
## 2 2 -0.126
## 3 3 -0.150
## 4 4 1.72
## 5 5 -0.737
## 6 6 -0.340
## 7 7 -1.19
## 8 8 2.53
## 9 9 -0.0108
## 10 10 0.560
## # ℹ 990 more rows
Interpretation: This graoh visualizes the null distribution (grey blocks) with a 95% confidence interval (green area). The redline on the left shows the observed test statistic, which lies faraway from the confidence interval area of null distribution. Therefore, under assumptions of frequentist hypothesis testing, null hypothesis can be rejected.
Section 2: Modeling
Linear Regression Equation of rlgblg and stflife (model 1)
model1 <- lm(stflife ~ rlgblg, data = italy_data)
coefficients <- coef(model1)
print(coefficients)
## (Intercept) rlgblgYes
## 6.7162570 0.2019502
equatiomatic::extract_eq(model1, use_coefs = TRUE)
\[ \operatorname{\widehat{stflife}} = 6.72 + 0.2(\operatorname{rlgblg}_{\operatorname{Yes}}) \] Interpretation:
(Intercept) 6.72:
The intercept represents the predicted value of life satisfaction when the categorical variable (rlgblg) is at their reference level. When looking at the category that’s NOT “rlgblgYes” (thus, not belonging to a religion), the expected value of stlife is approximately 6.72.
rlgblgYes 0.20:
This coefficient represents the difference in the predicted value of life satisfaction between the religious category and the reference category (not religious) for the rlgblg variable. Specifically, belonging to a religion is associated with an increase of approximately 0.20 in the predicted value of life satisfaction, compared to not belonging to a religion.
In summary: If a person does NOT belong to a religion, their predicted value of life satisfaction is 6.72. If a person belongs to a religion, their predicted value of life satisfaction is 6.72+0.20=6.92.
modelsummary(
list(model1),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | |
|---|---|
| rlgblgYes | 0.2 (0.0)*** |
| Num.Obs. | 9838 |
| R2 | 0.002 |
| R2 Adj. | 0.002 |
| AIC | 42163.7 |
| BIC | 42185.3 |
| Log.Lik. | −21078.852 |
| RMSE | 2.06 |
The coefficient is statistically significant (indicated by ***) but the fit is not great. The R-squared of 0.002 is really small. It indicates that only 0.2% of the variability in the dependent variable is explained by the model. This suggest that while there are significant differences in life satisfaction between religous and non-religious groups, the overall explanatory power of the model is very low, suggesting that religious differences, although statistically significant, are not strong predictors of life satisfaction by themselves.
Moedl 2: Multivariate model
model2 <- lm(stflife ~ `rlgblg` + `health`, data = italy_data)
modelsummary(
list(model2),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | |
|---|---|
| rlgblgYes | 0.3 (0.0)*** |
| health | −0.7 (0.0)*** |
| Num.Obs. | 9838 |
| R2 | 0.082 |
| R2 Adj. | 0.081 |
| AIC | 41344.6 |
| BIC | 41373.4 |
| Log.Lik. | −20668.301 |
| RMSE | 1.98 |
The coefficients are also statistically significant (indicated by the ***s). The fit gets better, indicated by the increase of R-squared from 0.002 to 0.082, now indicating that 8.2% of the variability in the dependent variable is explained by the model. R-sqaured adjusted is 0.081, which means that taking into account of number of predictors, 8.1% of the variability is explained. Since this is close to the R-squared value, it suggests the model isn’t being penalized much for the number of predictors. Overall, it suggests that this multivariate model (adding the variable of subjective health to the predictor) explains better the differences in life satisfaction among Italians.
Equation of multivariate model for clearer interpretation
remotes::install_github("datalorax/equatiomatic")
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## xfun (0.40 -> 0.41 ) [CRAN]
## rlang (1.1.1 -> 1.1.2 ) [CRAN]
## cli (3.6.1 -> 3.6.2 ) [CRAN]
## sass (0.4.7 -> 0.4.8 ) [CRAN]
## lifecycle (1.0.3 -> 1.0.4 ) [CRAN]
## jsonlite (1.8.7 -> 1.8.8 ) [CRAN]
## htmltools (0.5.6 -> 0.5.7 ) [CRAN]
## later (1.3.1 -> 1.3.2 ) [CRAN]
## vctrs (0.6.3 -> 0.6.5 ) [CRAN]
## utf8 (1.2.3 -> 1.2.4 ) [CRAN]
## withr (2.5.0 -> 2.5.2 ) [CRAN]
## fansi (1.0.4 -> 1.0.6 ) [CRAN]
## stringi (1.7.12 -> 1.8.3 ) [CRAN]
## cpp11 (0.4.6 -> 0.4.7 ) [CRAN]
## stringr (1.5.0 -> 1.5.1 ) [CRAN]
## dplyr (1.1.3 -> 1.1.4 ) [CRAN]
## yaml (2.3.7 -> 2.3.8 ) [CRAN]
## evaluate (0.21 -> 0.23 ) [CRAN]
## bslib (0.5.1 -> 0.6.1 ) [CRAN]
## httpuv (1.6.11 -> 1.6.13) [CRAN]
## knitr (1.43 -> 1.45 ) [CRAN]
## shiny (1.7.5 -> 1.8.0 ) [CRAN]
## Installing 22 packages: xfun, rlang, cli, sass, lifecycle, jsonlite, htmltools, later, vctrs, utf8, withr, fansi, stringi, cpp11, stringr, dplyr, yaml, evaluate, bslib, httpuv, knitr, shiny
##
## The downloaded binary packages are in
## /var/folders/2q/l02cfm956w53rnw4_vqqy9h40000gn/T//RtmpWDU8ZL/downloaded_packages
## ── R CMD build ─────────────────────────────────────────────────────────────────
##
checking for file ‘/private/var/folders/2q/l02cfm956w53rnw4_vqqy9h40000gn/T/RtmpWDU8ZL/remotes109e063fba895/datalorax-equatiomatic-39e8c3e/DESCRIPTION’ ...
✔ checking for file ‘/private/var/folders/2q/l02cfm956w53rnw4_vqqy9h40000gn/T/RtmpWDU8ZL/remotes109e063fba895/datalorax-equatiomatic-39e8c3e/DESCRIPTION’
##
─ preparing ‘equatiomatic’: (405ms)
##
checking DESCRIPTION meta-information ...
✔ checking DESCRIPTION meta-information
##
─ checking for LF line-endings in source and make files and shell scripts
##
─ checking for empty or unneeded directories
##
─ building ‘equatiomatic_0.3.1.tar.gz’
##
##
equatiomatic::extract_eq(model2, use_coefs = TRUE)
\[ \operatorname{\widehat{stflife}} = 8.1 + 0.34(\operatorname{rlgblg}_{\operatorname{Yes}}) - 0.69(\operatorname{health}) \] The healthier one is, the more satisfied one will be with life. Specifically, with an increase of one on the health scale (which means WORSEN of health state), life satisfaction will drop by 0.69. For religious people, their life satisfaction ranges from 4.99 (8.1+0.34-0.695 for very bad health) to 7.75 (8.1+0.34-0.691 for very good health). For non-religious people, their life satisfaction ranges from 4.65 (8.1-0.695 for very bad health) to 7.41 (8.1-0.691 for very good health).
Model 3: Interaction Model
model3 <- lm(stflife ~ `rlgblg` + `health` + `rlgblg`*`health`, data = italy_data)
modelsummary(
list(model3),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | |
|---|---|
| rlgblgYes | 0.5 (0.1)*** |
| health | −0.6 (0.1)*** |
| rlgblgYes × health | −0.1 (0.1) |
| Num.Obs. | 9838 |
| R2 | 0.082 |
| R2 Adj. | 0.082 |
| AIC | 41344.6 |
| BIC | 41380.6 |
| Log.Lik. | −20667.316 |
| RMSE | 1.98 |
Let’s put together to see how the interaction model adds to the explanation.
Model Summary
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c("{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_rename = c("rlgblgYes" = "Belonging to a Relgion"),
title = 'Table 2. Regression models predicting life satisfaction (scale 0 to 10)')
| (1) | (2) | (3) | |
|---|---|---|---|
| (Intercept) | 6.7 (0.0)*** | 8.1 (0.1)*** | 8.0 (0.1)*** |
| Belonging to a Relgion | 0.2 (0.0)*** | 0.3 (0.0)*** | 0.5 (0.1)*** |
| health | −0.7 (0.0)*** | −0.6 (0.1)*** | |
| Belonging to a Relgion:health | −0.1 (0.1) | ||
| Num.Obs. | 9838 | 9838 | 9838 |
| R2 | 0.002 | 0.082 | 0.082 |
| R2 Adj. | 0.002 | 0.081 | 0.082 |
| AIC | 42163.7 | 41344.6 | 41344.6 |
| BIC | 42185.3 | 41373.4 | 41380.6 |
| Log.Lik. | −21078.852 | −20668.301 | −20667.316 |
| RMSE | 2.06 | 1.98 | 1.98 |
From model2 to model3, the values of R-squared (and R^2 adjusted) do not show significant increase. It means that adding the interaction of two predictors does not explain the variability of outcome any better.
To further examine the interaction effect, we can use the Rule of Thumb to visualize.
interaction_plot <- effect("rlgblg*health", model3, na.rm=TRUE)
plot(interaction_plot,
main="Interaction effect plot",
xlab="health",
ylab="Life Satisfaction")
Two lines across groups are parallel, showing that there is NO interaction effect. In our context, it means that the subjective health has no relation to the association between belonging to a religion and life satisfaction.
Overall, we reject the null hypothesis. For the alternative hypothesis, though our model is statistically significant, the explanaratory power is not ver strong, indicating that whether belonging to a religion is not a strong predictor of life satisfaction of Italians. For the interaction hypothesis, there is no interaction effect, showing that subjective health does not influence the strength of relationship between religion and life satisfaction.