Study #1: Mental Health Code

Below is the code I used to conduct analysis on Enneagram vs mental health diagnoses. There are undoubtedly more sophisticated ways to go about it, but I’m not a professional coder and my rudimentary methods seemed to do the trick. Feel free to double-check my work! The raw data file is available for download on Google Drive if you’d like to play around with it on your own or rerun my code in RStudio.

# This script contains all the tests performed to assess the dataset for
# relationships between mental health disorders and the Enneagram.
# Generated and assessed by Danielle F. of The Scientific Enneagram

#
##
### DO NOT RUN THIS CODE UNLESS YOU HAVE ALREADY RUN 'INITIAL SETUP.R'
##
#

# 1. Create dataframe with only those who answered the neurodivergent question
raw_mh <- raw[!is.na(raw$MentalHealth),]
  ### check there were 1721 observations and 93 variables

# 2. Load packages necessary for analysis
library(chisq.posthoc.test) #Needed for ChiSq post hoc
library(rstatix) #Needed for row-wise Fisher test
library(ggplot2) #Needed for charts
library(rcompanion) #Needed for Pairwise Nominal Independence test

####DIAGNOSIS####

# 3. Assess MHPos (has at least one mental health diagnosis) ####
## Run descriptives  
addmargins(table(raw_mh$MHPos)) #shows 952 of 1721 report a diagnosis
addmargins(table(raw_mh$Type,raw_mh$MHPos)) #shows breakout by Type
## Test for significance
chisq.test(raw_mh$Type,raw_mh$MHPos) #X2(8,1721)=55.4,p=3.691e-09
chisq.posthoc.test(table(raw_mh$Type,raw_mh$MHPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_mh, aes(x=MHPos,fill=factor(MHPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Mental Health Diagnosis", x=NULL,y="Count")
ggplot(data=raw_mh, aes(fill=MHPos, x=Type)) +
  geom_bar(position=position_fill(reverse=TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Diagnosis by Type", x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

# 4. Assess AnxPos (has anxiety diagnosis) ####
## Run descriptives  
addmargins(table(raw_mh$AnxPos)) #shows 772 of 1721 report a diagnosis
addmargins(table(raw_mh$Type,raw_mh$AnxPos)) #shows breakout by Type
## Test for significance
chisq.test(raw_mh$Type,raw_mh$AnxPos) #X2(8,1721)=59.769, p=5.173e-10
chisq.posthoc.test(table(raw_mh$Type,raw_mh$AnxPos), method = "bonferroni")
## Test for significance within Type 6s
chisq.test(raw_mh$Type6version,raw_mh$AnxPos) #X2(2,121)=3.0393, p=.2188
chisq.posthoc.test(table(raw_mh$Type6version,raw_mh$AnxPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_mh, aes(x=AnxPos,fill=factor(AnxPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Anxiety Diagnosis", x=NULL,y="Count")
ggplot(data=raw_mh, aes(fill=AnxPos, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Anxiety Diagnosis by Type", x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

# 5. Assess DepPos (has depression diagnosis) ####
## Run descriptives  
addmargins(table(raw_mh$DepPos)) #shows 577 of 1721 report a diagnosis
addmargins(table(raw_mh$Type,raw_mh$DepPos)) #shows breakout by Type
# Test for significance
chisq.test(raw_mh$Type,raw_mh$DepPos) #X2(8,1721)=45.272, p=3.268e-07
chisq.posthoc.test(table(raw_mh$Type,raw_mh$DepPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_mh, aes(x=DepPos,fill=factor(DepPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Depression Diagnosis", x=NULL,y="Count")
ggplot(data=raw_mh, aes(fill=DepPos, x=Type)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Depression Diagnosis by Type", x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

## 6. Assess BPPos (has bipolar diagnosis) ####
## Run descriptives  
addmargins(table(raw_mh$BPDPos)) #shows 31 of 1721 report a diagnosis
  ### Only moving forward on analysis for samples of 45 (avg 5 per Type) or more
## Create visualizations of above descriptives (optional)
ggplot(data=raw_mh, aes(fill=BPDPos, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Bipolar Diagnosis by Type", x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

# 7. Assess PTSDPos (has PTSD diagnosis) ####
## Run descriptives  
addmargins(table(raw_mh$PTSDPos)) #shows 189 of 1721 report a diagnosis
addmargins(table(raw_mh$Type,raw_mh$PTSDPos)) #shows breakout by Type
# Test for significance
chisq.test(raw_mh$Type,raw_mh$PTSDPos) #X2(8,1721)=40.743, p=2.329e-06
chisq.posthoc.test(table(raw_mh$Type,raw_mh$PTSDPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_mh, aes(x=PTSDPos,fill=factor(PTSDPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="PTSD Diagnosis", x=NULL,y="Count")
ggplot(data=raw_mh, aes(fill=PTSDPos, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="PTSD Diagnosis by Type", x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

####TREATMENT####

# 8. Assess Recent Treatment Methods ####
## Run descriptives & check significance
addmargins(table(raw_mh$MentalHealthManage)) #shows 932 of 952 MHPos answered
addmargins(table(raw_mh$Type, raw_mh$MentalHealthManage))
#Test of significance
chisq.test(raw_mh$Type,raw_mh$MentalHealthManage) #X2(16,932)=17.84,p=.3334
chisq.posthoc.test(table(raw_mh$Type,raw_mh$MentalHealthManage), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$MentalHealthManage,raw_mh$Type),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$MentalHealthManage)), 
       aes(x=MentalHealthManage, fill=factor(MentalHealthManage))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Mental Healath Management (past 30 days)", x=NULL,y="Count")
ggplot(data=subset(raw_mh,!is.na(raw_mh$MentalHealthManage)), 
       aes(fill=MentalHealthManage, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Management by Type (past 30 days)", x=NULL, 
       y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))

# 9. Assess Presence of Recent Treatment ####
## Create new variable to combine two Yes levels
raw_mh$mhtreat[raw_mh$MentalHealthManage == "Yes, meds"] <- "Yes" #Combine the two Yes levels
raw_mh$mhtreat[raw_mh$MentalHealthManage == "Yes, no meds"] <- "Yes"
raw_mh$mhtreat[raw_mh$MentalHealthManage == "No"] <- "No"
## Run descriptives
addmargins(table(raw_mh$mhtreat)) #shows 104 No/828 Yes of 932 responses
addmargins(table(raw_mh$Type, raw_mh$mhtreat)) #shows breakout by Type
## Test of significance
chisq.test(raw_mh$Type,raw_mh$mhtreat) #X2(8,932)=6.4754, p=.5941
chisq.posthoc.test(table(raw_mh$Type,raw_mh$mhtreat), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$mhtreat)), 
       aes(x=mhtreat, fill=factor(mhtreat))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Mental Healath Treatment (past 30 days)", x=NULL,y="Count")
ggplot(data=subset(raw_mh,!is.na(raw_mh$mhtreat)), 
       aes(fill=mhtreat, x=Type)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#f2e9db","#005151")) +
  theme_minimal() +
  labs(title="Mental Health Treatment by Type (past 30 days)", x=NULL, 
       y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))


####EGO####

# 10. Assess Diagnosis Status by Ego Level ####
## Run descriptives
addmargins(table(raw_mh$Ego)) #shows 1655 of 1721 responded to Ego question
addmargins(table(raw_mh$Ego,raw_mh$MHPos)) #show breakout by diagnosis 
## Test of significance
chisq.test(raw_mh$Ego,raw_mh$MHPos) #X2(2,1655)=15.281, p=.0004807
chisq.posthoc.test(table(raw_mh$Ego,raw_mh$MHPos), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$Ego,raw_mh$MHPos),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Ego)), 
       aes(x=Ego, fill=factor(Ego))) +
  geom_bar() +
  scale_fill_SciEn(palette="stoplight") +
  theme_minimal() +
  labs(title="Ego Level", x=NULL,y="Count") +
  theme(legend.position = "none")
ggplot(data=subset(raw_mh,!is.na(raw_mh$Ego)), aes(fill=MHPos, x=Ego)) +
  geom_bar(position=position_fill(reverse=TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Diagnosis by Relationship with Ego", x=NULL, 
       y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

# 11. Assess Treatment Y/N by Ego Level ####
## Run descriptives
addmargins(table(raw_mh$Ego,raw_mh$mhtreat)) #shows 894 answered ego and treatment
## Test of significance
chisq.test(raw_mh$Ego,raw_mh$mhtreat) #X2(2,894)=9.0049, p=.01108
chisq.posthoc.test(table(raw_mh$Ego,raw_mh$mhtreat), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$Ego,raw_mh$mhtreat),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Ego)&!is.na(raw_mh$mhtreat)),
       aes(fill=mhtreat, x=Ego)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#f2e9db","#005151")) +
  theme_minimal() +
  labs(title="Treatment Y/N by Ego Level", x="Ego Level", y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))

# 12. Assess Treatment Method by Ego Level ####
## Run descriptives
addmargins(table(raw_mh$Ego,raw_mh$MentalHealthManage)) #shows 894 answered ego and treatment
## Test of significance
chisq.test(raw_mh$Ego,raw_mh$MentalHealthManage) #X2(4,894)=18.019, p=.001224
chisq.posthoc.test(table(raw_mh$Ego,raw_mh$MentalHealthManage), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$MentalHealthManage,raw_mh$Ego),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Ego)&!is.na(raw_mh$MentalHealthManage)),
       aes(fill=MentalHealthManage, x=Ego)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Management by Ego Level (past 30 days)",
       x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))

####MINDSET####

# 13. Assess Diagnosis by Growth/Stress Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_mh$Arrow)) #shows 1720 answered diagnosis and arrow question
addmargins(table(raw_mh$Arrow,raw_mh$MHPos)) #shows breakout by diagnosis
## Test of significance
chisq.test(raw_mh$Arrow,raw_mh$MHPos) #X2(2,1720)=24.492, p=4.805e-06
chisq.posthoc.test(table(raw_mh$Arrow,raw_mh$MHPos), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$Arrow,raw_mh$MHPos),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)

## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Arrow)), 
       aes(x=Arrow, fill=factor(Arrow))) +
  geom_bar() +
  scale_fill_SciEn(palette="stoplight") +
  theme_minimal() +
  labs(title="Recent Stress/Growth Mindset", x=NULL,y="Count") +
  theme(legend.position = "none")
ggplot(data=subset(raw_mh,!is.na(raw_mh$Arrow)), aes(fill=MHPos, x=Arrow)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Diagnosis by Recent Stress/Growth Mindset", 
       x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Diagnosis"))

# 14. Assess Treatment Y/N by Growth/Stress Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_mh$Arrow,raw_mh$mhtreat)) #shows 931 answered arrow and treatment
## Test for significance
chisq.test(raw_mh$Arrow,raw_mh$mhtreat) #X2(2,931)=10.274, p=.005876
chisq.posthoc.test(table(raw_mh$Arrow,raw_mh$mhtreat), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$Arrow,raw_mh$mhtreat),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Arrow)&!is.na(raw_mh$mhtreat)),
       aes(fill=mhtreat, x=Arrow)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#f2e9db","#005151")) +
  theme_minimal() +
  labs(title="Treatment Y/N by Recent Mindset", x="Recent Mindset",
       y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))

# 15. Assess Treatment Method by Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_mh$Arrow,raw_mh$MentalHealthManage)) #shows 931 answered arroe and treatment
## Test of significance
chisq.test(raw_mh$Arrow,raw_mh$MentalHealthManage) #X2(4,931)=18.985, p=.0007912
chisq.posthoc.test(table(raw_mh$Arrow,raw_mh$MentalHealthManage), method = "bonferroni")
pairwiseNominalIndependence(table(raw_mh$MentalHealthManage,raw_mh$Arrow),method="bonferroni",
                            fisher=FALSE, gtest=FALSE, stats=TRUE)
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_mh,!is.na(raw_mh$Arrow)&!is.na(raw_mh$MentalHealthManage)),
       aes(fill=MentalHealthManage, x=Arrow)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Mental Health Management by Recent Mindset (last 30 days)", 
       x=NULL, y="Proportion (%)") +
  guides(fill=guide_legend(title="Treatment"))
Previous
Previous

Study #1: Neurodivergence Code

Next
Next

Study #1: Birth Order Code