# Load necessary libraries
library(ggplot2) # For data visualization
library(brms) # For Bayesian regression
## Loading required package: Rcpp
## Loading 'brms' package (version 2.19.0). Useful instructions
## can be found by typing help('brms'). A more detailed introduction
## to the package is available through vignette('brms_overview').
##
## Attaching package: 'brms'
## The following object is masked from 'package:stats':
##
## ar
library(posterior) # For posterior analysis
## This is posterior version 1.4.1
##
## Attaching package: 'posterior'
## The following objects are masked from 'package:stats':
##
## mad, sd, var
## The following objects are masked from 'package:base':
##
## %in%, match
library(bayesplot) # For posterior plots
## This is bayesplot version 1.10.0
## - Online documentation and vignettes at mc-stan.org/bayesplot
## - bayesplot theme set to bayesplot::theme_default()
## * Does _not_ affect other ggplot2 plots
## * See ?bayesplot_theme_set for details on theme setting
##
## Attaching package: 'bayesplot'
## The following object is masked from 'package:posterior':
##
## rhat
## The following object is masked from 'package:brms':
##
## rhat
library(tidybayes) # For tidyverse-style summaries of posterior draws
##
## Attaching package: 'tidybayes'
## The following objects are masked from 'package:brms':
##
## dstudent_t, pstudent_t, qstudent_t, rstudent_t
library(ggdist) # For plotting posteriors with ggplot2
##
## Attaching package: 'ggdist'
##
## The following objects are masked from 'package:brms':
##
## dstudent_t, pstudent_t, qstudent_t, rstudent_t
library(bayestestR) # For Bayesian hypothesis testing
##
## Attaching package: 'bayestestR'
## The following object is masked from 'package:ggdist':
##
## hdi
## The following object is masked from 'package:tidybayes':
##
## hdi
library(dplyr) #For data frame manipulation
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr) #For data tidying and reshaping
library(HDInterval) # For calculating HDI
##
## Attaching package: 'HDInterval'
## The following object is masked from 'package:bayestestR':
##
## hdi
## The following object is masked from 'package:ggdist':
##
## hdi
## The following object is masked from 'package:tidybayes':
##
## hdi
# The data
df <- read.csv("/Users/adiiore/Desktop/selfesteem.csv")
df <- df[, c("frequency_of_use", "gender", "self_esteem")] #Exclude the "age" variable from the dataset
# Self-esteem as a function of gender: visualize the relationship between gender and self-esteem.
ggplot(df, aes(x = gender, y = self_esteem)) +
geom_point() +
labs(title = "Relationship between Gender and Self-Esteem",
x = "Gender",
y = "Self-Esteem")

# Self-esteem as a function of gender and number of hours:
ggplot(df, aes(factor(frequency_of_use), self_esteem)) +
geom_boxplot() +
labs(title = "Relationship between Frequency of Use and Self-Esteem",
x = "Frequency of Use",
y = "Self-Esteem")

# Self-esteem as a function of gender and number of hours, with facets:
ggplot(df, aes(factor(frequency_of_use), self_esteem)) +
geom_boxplot() +
facet_grid(. ~ gender) +
labs(title = "Relationship between Frequency of Use and Self-Esteem (Grouped by Gender)",
x = "Frequency of Use",
y = "Self-Esteem")

# Set Priors
## Get the default priors for the model
get_prior(
self_esteem ~ gender + frequency_of_use,
family = gaussian(),
data = df
)
## prior class coef group resp dpar nlpar lb ub
## (flat) b
## (flat) b frequency_of_use
## (flat) b genderMale
## student_t(3, 24.8, 4.2) Intercept
## student_t(3, 0, 4.2) sigma 0
## source
## default
## (vectorized)
## (vectorized)
## default
## default
## Define the hyper parameters for the normal priors
priors <-
set_prior("normal(-4, 2)", class = "b", coef = "frequency_of_use") +
set_prior("student_t(3, 0.5, 0.5)", class = "b", coef = "genderMale") +
set_prior("student_t(3, 24.8, 4.2)", class = "Intercept") +
set_prior("student_t(3, 0, 4.2)", class = "sigma")
## validate priors
validate_prior(
self_esteem ~ gender + frequency_of_use,
family = gaussian(),
data = df,
prior = priors
)
## prior class coef group resp dpar nlpar lb ub
## (flat) b
## normal(-4, 2) b frequency_of_use
## student_t(3, 0.5, 0.5) b genderMale
## student_t(3, 24.8, 4.2) Intercept
## student_t(3, 0, 4.2) sigma 0
## source
## default
## user
## user
## user
## user
# Posterior Model ------------------------------------------------
fit <- brm(
self_esteem ~ gender + frequency_of_use,
family = gaussian(),
data = df,
prior = priors,
backend = "cmdstanr",
seed = 1234
)
## Warning in readLines(hpp_path): incomplete final line found on
## '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T//RtmprLbE27/model-501676e3b5b2.hpp'
## Warning in readLines(private$hpp_file_): incomplete final line found on
## '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T//RtmprLbE27/model-501676e3b5b2.hpp'
## Start sampling
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 1 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 1
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 2 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 2
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 3 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 3
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
## Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
## Chain 4 Exception: normal_id_glm_lpdf: Scale vector is inf, but must be positive finite! (in '/var/folders/fy/8zbj5_zd18z5fkghsc48m2bc0000gn/T/RtmprLbE27/model-501676e3b5b2.stan', line 37, column 4 to column 62)
## Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
## Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
## Chain 4
# Check and validate the fit of the posterior distribution
fit
## Family: gaussian
## Links: mu = identity; sigma = identity
## Formula: self_esteem ~ gender + frequency_of_use
## Data: df (Number of observations: 10000)
## Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup draws = 4000
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept 28.62 0.16 28.32 28.94 1.00 4589 3210
## genderMale 0.04 0.08 -0.13 0.20 1.00 3896 2766
## frequency_of_use -0.96 0.04 -1.04 -0.89 1.00 4776 3144
##
## Family Specific Parameters:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma 4.01 0.03 3.96 4.07 1.00 5507 2941
##
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
# I have a proper fit of the posterior distribution - all the RHats are 1.00 (smaller than 1.01) and all the ESS values are greater than 1000
# the estimate for `frequency_of_use` indicates that there is a negative relationship between the frequency of use and the self-esteem variable.
#the estimate of 'gender' suggests that, on average, there is a slight increase in self-esteem for individuals classified as Male compared to those classified as Female.
#However, it's important to note that this effect is relatively small, as the estimate is close to zero.
mcmc_trace(fit, pars = c("b_Intercept", "b_genderMale", "b_frequency_of_use", "sigma"))

# The trace plot looks good, the chains are mix well with each other
# Posterior expected value distributions of females and males with frequency value of 8 hours
grid <- data.frame(gender = c("Female", "Male"),
frequency_of_use = c(8, 8))
#posterior expected value distributions of females and males
grid <- grid |> add_epred_rvars(fit)
grid
## # A tibble: 2 × 3
## gender frequency_of_use .epred
## <chr> <dbl> <rvar[1d]>
## 1 Female 8 21 ± 0.17
## 2 Male 8 21 ± 0.16
# Create the graph
ggplot(grid, aes(y = interaction(gender, frequency_of_use))) +
stat_slab(aes(xdist = .epred), fill = "#6495ED", alpha = 0.7, color = "black") +
labs(title = "Posterior Expected Value Distributions of Self-Esteem",
x = "Expected Value of Self-Esteem",
y = "Gender and Frequency of Use") +
theme_minimal() +
theme(plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10))

# Central measures
post_theta <- grid$.epred # Extract the posterior draws of the expected values
post_theta
## rvar<4000>[2] mean ± sd:
## [1] 21 ± 0.17 21 ± 0.16
CM <- point_estimate(post_theta)
names(post_theta) <- grid$gender
CM_long <- CM %>%
mutate(gender = names(post_theta)) %>%
select(-Parameter) %>%
pivot_longer(-gender, names_to = "Measure")
CM_long
## # A tibble: 6 × 3
## gender Measure value
## <chr> <chr> <dbl>
## 1 Female Median 20.9
## 2 Female Mean 20.9
## 3 Female MAP 20.9
## 4 Male Median 21.0
## 5 Male Mean 21.0
## 6 Male MAP 21.0
p <- ggplot(grid, aes(y = gender)) +
stat_slab(aes(xdist = .epred), fill = "#FFA500", alpha = 0.7, color = "black") +
geom_point(aes(x = value, color = Measure, shape = Measure),
data = CM_long, size = 4, alpha = 0.6) +
labs(title = "Posterior Predictive Distribution: Expected Values",
x = "Expected Value",
y = "Gender") +
theme_minimal() +
theme(plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10))
p

# High Density Interval
describe_posterior(post_theta, ci_method = "HDI", test = NULL)
## Summary of Posterior Distribution
##
## Parameter | Median | 95% CI
## -----------------------------------
## x[Female] | 20.92 | [20.60, 21.24]
## x[Male] | 20.96 | [20.65, 21.28]
ggplot(grid, aes(y = gender)) +
stat_slabinterval(aes(xdist = .epred, fill = after_stat(level)),
point_interval = "median_hdci",
.width = c(0.5, 0.8, 0.95, 1))

# The differences between males and females
# The posterior expected value distribution of the differences in self esteem score estimate
grid <- data.frame(gender = c("Female", "Male"),
frequency_of_use = c(8, 8))
grid <- grid |> add_epred_rvars(fit) |>
mutate(
gender_diff = .epred[gender == "Male"] - .epred[gender == "Female"]
)
grid
## # A tibble: 2 × 4
## gender frequency_of_use .epred gender_diff
## <chr> <dbl> <rvar[1d]> <rvar[1d]>
## 1 Female 8 21 ± 0.17 0.035 ± 0.082
## 2 Male 8 21 ± 0.16 0.035 ± 0.082
#p-direction & Estimate ROPE (in range of -0.5, 0.5)
p_direction <- grid$gender_diff[grid$frequency_of_use == 8] |>
describe_posterior(rope_range = c(-0.5, 0.5), ci_method = "HDI") |>
mutate(Parameter = grid$gender[grid$frequency_of_use == 8])
p_direction
## Summary of Posterior Distribution
##
## Parameter | Median | 95% CI | pd | ROPE | % in ROPE
## -----------------------------------------------------------------------
## Female | 0.04 | [-0.13, 0.19] | 66.45% | [-0.50, 0.50] | 100%
## Male | 0.04 | [-0.13, 0.19] | 66.45% | [-0.50, 0.50] | 100%
#p-direction - If indeed the probability that women have higher Self-Esteem than men or the opposite
#p-direction is 66.45% - The probability of difference between men and women is 66.45%
# Visualization of the distributions and ROPE
p <- grid %>%
filter(frequency_of_use == 8) %>%
ggplot(aes(y = gender)) +
stat_slabinterval(aes(xdist = gender_diff), fill = "#FFA500", alpha = 0.7, color = "black") +
geom_vline(xintercept = 0, linewidth = 1, color = "red") +
geom_vline(xintercept = -0.5, linewidth = 1, linetype = "dashed", color = "red") +
geom_vline(xintercept = 0.5, linewidth = 1, linetype = "dashed", color = "red") +
geom_rect(aes(xmin = -0.5, xmax = 0.5, ymin = -Inf, ymax = Inf), fill = "red", alpha = 0.01) +
labs(title = "Distribution of Difference in Self-Esteem Score (Female - Male) for Frequency of Use = 8",
x = "Difference of Self-Esteem Score",
y = "Gender") +
theme_minimal() +
theme(plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10))
p

#PPD for the difference between females and males
ppd_grid <- expand.grid(gender = unique(df$gender),
frequency_of_use = unique(df$frequency_of_use)) |>
add_predicted_rvars(fit) |>
group_by(frequency_of_use)|>
mutate(
PPD_gender_diff = .prediction[gender == "Male"] - .prediction[gender == "Female"]
)
ppd_grid <- ppd_grid |> add_predicted_rvars(fit)
ppd_grid
## # A tibble: 18 × 4
## # Groups: frequency_of_use [9]
## gender frequency_of_use .prediction PPD_gender_diff
## <fct> <int> <rvar[1d]> <rvar[1d]>
## 1 Male 2 27 ± 4.0 0.172 ± 5.6
## 2 Female 2 27 ± 4.0 0.172 ± 5.6
## 3 Male 5 24 ± 4.0 -0.057 ± 5.7
## 4 Female 5 24 ± 4.1 -0.057 ± 5.7
## 5 Male 4 25 ± 4.0 0.141 ± 5.8
## 6 Female 4 25 ± 4.0 0.141 ± 5.8
## 7 Male 3 26 ± 4.0 -0.052 ± 5.7
## 8 Female 3 26 ± 4.0 -0.052 ± 5.7
## 9 Male 6 23 ± 4.0 0.029 ± 5.8
## 10 Female 6 23 ± 4.1 0.029 ± 5.8
## 11 Male 7 22 ± 4.0 -0.027 ± 5.7
## 12 Female 7 22 ± 4.1 -0.027 ± 5.7
## 13 Male 1 28 ± 4.0 0.032 ± 5.6
## 14 Female 1 28 ± 4.0 0.032 ± 5.6
## 15 Male 0 29 ± 4.0 0.056 ± 5.8
## 16 Female 0 29 ± 4.0 0.056 ± 5.8
## 17 Male 8 21 ± 4.0 0.169 ± 5.7
## 18 Female 8 21 ± 4.0 0.169 ± 5.7
# Create the graph
ggplot(ppd_grid, aes(y = interaction(gender, frequency_of_use))) +
stat_slab(aes(xdist = .prediction), fill = "#FFA500", alpha = 0.7, color = "black") +
labs(title = "PPD: Difference in Self-Esteem Score (Female - Male)",
x = "Predicted Difference of Self-Esteem Score",
y = "Gender and Frequency of Use") +
theme_minimal() +
theme(plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10))

head(draws_of(ppd_grid$.prediction))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## 1 19.81114 15.57781 20.83548 25.97023 24.79667 17.65533 21.49863 23.56747
## 2 25.41497 21.51149 27.49719 19.01283 29.31428 26.80742 28.75477 27.22252
## 3 22.97957 29.46957 28.10791 22.58126 21.03200 20.46802 26.74519 20.86827
## 4 34.22053 26.96244 27.10999 29.89255 23.67283 27.57326 27.34525 25.43280
## 5 32.46556 21.50356 23.37857 18.01418 21.25810 15.67288 30.05083 28.06351
## 6 27.44182 29.80860 32.26763 22.19200 24.25186 22.34003 25.95862 21.15095
## [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
## 1 28.98023 19.37649 21.02497 18.30447 28.87307 24.53692 31.00055 27.93969
## 2 16.77133 22.08745 21.57384 18.92635 23.55564 24.59859 26.85086 31.52122
## 3 15.43611 23.35273 21.10050 19.88689 27.88333 25.67981 32.23063 28.77353
## 4 15.20655 22.59453 21.38996 28.08607 24.33577 32.67765 21.98575 23.84738
## 5 17.44936 26.12277 24.82225 23.13381 22.12887 28.53657 34.35246 28.47059
## 6 15.19138 21.24672 25.77932 30.63993 31.23214 32.57614 25.80958 32.06888
## [,17] [,18]
## 1 32.04294 20.64260
## 2 19.29293 18.43557
## 3 21.87455 21.77306
## 4 14.80722 21.92853
## 5 15.86010 22.25258
## 6 20.09792 24.65349
# probability for a random female-male pair to have the female exhibit
# a better score across different levels of frequency_of_use.
prob_female_better <- mean(ppd_grid$PPD_gender_diff > 0)
prob_female_better
## [1] 0.51075 0.51075 0.49150 0.49150 0.51400 0.51400 0.49150 0.49150 0.50175
## [10] 0.50175 0.49575 0.49575 0.49950 0.49950 0.50500 0.50500 0.50050 0.50050
#the average probability across all levels of frequency_of_use combined
prob_female_better <- mean(ppd_grid$PPD_gender_diff > 0)
prob_female_better_combined <- mean(prob_female_better)
prob_female_better_combined
## [1] 0.5011389