Study #1: Neurodivergence Code

Below is the code I used to perform the analysis on Enneagram and neurodivergence. 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 neurodivergence and the Enneagram.
# (generated and assessed by Danielle, owner 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
summary(as.factor(raw$Neurodivergence)) #shows 25 NA Types
raw_nd <- raw[!is.na(raw$Neurodivergence),]
 ###check that the 'raw_nd' dataframe has 25 less than 'raw', or 1714

# 2. Install and load required packages
install.packages("chisq.posthoc.test") 
library(chisq.posthoc.test) #needed for ChiSq post hoc
install.packages("rstatix")
library(rstatix) #needed for row-wise Fisher test

####DIAGNOSIS####

# 3. Assess NeuroPos (has at least one neurodivergence diagnosis) ####
## Run descriptives  
addmargins(table(raw_nd$NeuroPos)) #shows 471 of 1714 report a diagnosis
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$NeuroPos)) #no cells <5 - Chi-Squared
## Test for significance
chisq.test(raw_nd$Type,raw_nd$NeuroPos) #X2(8,1714)=67.806,p=1.342e-11
chisq.posthoc.test(table(raw_nd$Type,raw_nd$NeuroPos),method="bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_nd, aes(x=NeuroPos,fill=factor(NeuroPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Neurodivergent Diagnosis", x=NULL,y="Count")
ggplot(data=raw_nd, aes(fill=NeuroPos, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Diagnosis by Type", x=NULL, y="Proportion (%)")

# 4. Assess ASDPos (reported an Autism diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$ASDPos)) #shows 64 of 1714 report a diagnosis
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$ASDPos)) #cells <5 - Fisher's Exact
## Test for significance
fisher.test(raw_nd$Type,raw_nd$ASDPos,simulate.p.value=TRUE) #significant
print(row_wise_fisher_test(table(raw_nd$Type,raw_nd$ASDPos),
                           p.adjust.method = "bonferroni"))
## Create visualizations of above descriptives (optional)
ggplot(data=raw_nd, aes(x=ASDPos,fill=factor(ASDPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Autism Diagnosis", x=NULL,y="Count")
ggplot(data=subset(raw_nd,raw_nd$ASDPos=="ASD diagnosis"), 
       aes(fill=ASDPos, x=Type)) +
  geom_bar(stat='count',
           fill=c("#1D8296","#e5bcd7","#f4623a","#f8ad6d","#5e8dda","#e0a526",
                  "#8a7b19","#be5500","#f17f70")) +
  theme_minimal() +
  labs(title="Autism Diagnosis by Type", x=NULL, y="Count")

# 5. Assess ADHDPos (reported an ADHD diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$ADHDPos)) #shows 333 of 1714 report a diagnosis
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$ADHDPos)) #no cells <5 - Chi-Squared
## Test for significance
chisq.test(raw_nd$Type,raw_nd$ADHDPos) #X2(8,1714)=65.432, p=3.964e-11
chisq.posthoc.test(table(raw_nd$Type,raw_nd$ADHDPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=raw_nd, aes(x=ADHDPos,fill=ADHDPos)) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="ADHD Diagnosis", x=NULL,y="Count")
ggplot(data=raw_nd, aes(fill=ADHDPos, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Diagnosis by Type", x=NULL, y="Proportion (%)")

# 6. Assess OCDPos (reported an OCD diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$OCDPos)) #shows 81 of 1714 report a diagnosis
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$OCDPos)) #one cell <5 - Fisher's Exact
## Test for significance
fisher.test(raw_nd$Type,raw_nd$OCDPos, simulate.p.value=TRUE) #insignificant
print(row_wise_fisher_test(table(raw_nd$Type,raw_nd$OCDPos),
                           p.adjust.method = "bonferroni"))
## Create visualizations of above descriptives (optional)
ggplot(data=raw_nd, aes(x=OCDPos,fill=factor(OCDPos))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="OCD Diagnosis", x=NULL,y="Count")
ggplot(data=subset(raw_nd,raw_nd$OCDPos=="OCD diagnosis"), 
       aes(fill=OCDPos, x=Type)) +
  geom_bar(stat='count',
           fill = c("#1D8296","#e5bcd7","#f4623a","#f8ad6d","#5e8dda","#e0a526",
                    "#8a7b19","#be5500","#f17f70")) +
  theme_minimal() +
  labs(title="OCD Diagnosis by Type", x=NULL, y="Count")

# 7. Assess DyslexPos (reported a Dyslexia diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$DyslexPos)) #shows 36 of 1714 report a diagnosis
  ### Only moving forward on analysis for samples of 45 (avg 5 per Type) or more

# 8. Assess DyscalPos (reported a Dyscalculia diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$DyscalPos)) #shows 26 of 1714 report a diagnosis
### Only moving forward on analysis for samples of 45 (avg 5 per Type) or more

# 9. Assess DysgraPos (reported a Dysgraphia diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$DysgraPos)) #shows 4 of 1714 report a diagnosis
### Only moving forward on analysis for samples of 45 (avg 5 per Type) or more

# 10. Assess DyspraPos (reported a Dyspraxia diagnosis) ####
## Run descriptives
addmargins(table(raw_nd$DyspraPos)) #shows 5 of 1714 report a diagnosis
### Only moving forward on analysis for samples of 45 (avg 5 per Type) or more

####TREATMENT####

# 11. Assess Treatment Methods ####
## Run descriptives
addmargins(table(raw_nd$NeuroManage)) #shows 445 of 471 NeuroPos answered
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$NeuroManage)) #no cell <5 - Chi-Squared
#Test of significance
chisq.test(raw_nd$Type,raw_nd$NeuroManage) #X2(16,445)=19.349, p=.251
chisq.posthoc.test(table(raw_nd$Type,raw_nd$NeuroManage), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroManage)), 
       aes(x=NeuroManage, fill=factor(NeuroManage))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Neurodivergence Management (past 30 days)", x=NULL,y="Count")
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroManage)), 
       aes(fill=NeuroManage, x=Type)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Neurodivergence Management by Type (past 30 days)", x=NULL, 
       y="Proportion (%)")

# 12. Assess Presence of Recent Treatment ####
## Create new variable to combine two Yes levels
raw_nd$neurotreat[raw_nd$NeuroManage == "Y-meds"] <- "Yes"
raw_nd$neurotreat[raw_nd$NeuroManage == "Y-no meds"] <- "Yes"
raw_nd$neurotreat[raw_nd$NeuroManage == "N"] <- "No"
## Run descriptives
addmargins(table(raw_nd$neurotreat)) #shows 82 No/363 Yes of 445 responses
## Determine appropriate significance test (if no cells <5, ChiSq; else, Fishers)
ftable(table(raw_nd$Type,raw_nd$neurotreat)) #three cells <5 - Fisher's Exact
#Test of significance
fisher.test(raw_nd$Type,raw_nd$neurotreat, simulate.p.value=TRUE) #insignificant
print(row_wise_fisher_test(table(raw_nd$Type,raw_nd$neurotreat),
                           p.adjust.method = "bonferroni"))
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$neurotreat)), 
       aes(x=neurotreat, fill=factor(neurotreat))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Neurodivergence Management (past 30 days)", x=NULL,y="Count")
ggplot(data=subset(raw_nd,!is.na(raw_nd$neurotreat)), 
       aes(fill=neurotreat, x=Type)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Neurodivergence Management by Type (past 30 days)", x=NULL, 
       y="Proportion (%)")

####EGO####

# 13. Assess Diagnosis Status by Ego Level ####
## Run descriptives
addmargins(table(raw_nd$Ego,raw_nd$NeuroPos)) 
  #shows 1650 responded to both Neuro and Ego questions
  #shows no cell <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Ego,raw_nd$NeuroPos) #X2(2,1650)=5.9388, p=.05133
chisq.posthoc.test(table(raw_nd$Ego,raw_nd$NeuroPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$Ego)), 
       aes(x=Ego, fill=factor(Ego))) +
  geom_bar(fill="#005151") +
  theme_minimal() +
  labs(title="Ego Level (past 30 days)", x=NULL,y="Count")
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroPos)&!is.na(raw_nd$Ego)), 
       aes(fill=NeuroPos, x=Ego)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Ego Level by Neurodivergence", x=NULL, y="Proportion (%)")

# 14. Assess Recent Treatment Method by Ego Level ####
## Run descriptives
addmargins(table(raw_nd$Ego,raw_nd$NeuroManage)) 
  #shows 433 answered both Treatment and Ego questions
  #shows no cell <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Ego,raw_nd$NeuroManage) #X2(4,433)=12.496, p=.01402
chisq.posthoc.test(table(raw_nd$Ego,raw_nd$NeuroManage), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroManage)&!is.na(raw_nd$Ego)), 
       aes(fill=NeuroManage, x=Ego)) +
  geom_bar(position='dodge', stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Ego Level by Management Method", x="Ego", y="Proportion (%)") +
  guides(fill=guide_legend(title="Management Method"))
  
# 15. Assess Presence of Recent Treatment by Ego Level ####
## Run descriptives
addmargins(table(raw_nd$Ego,raw_nd$neurotreat)) 
  #shows 433 answered both Treatment and Ego questions
  #shows no cell <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Ego,raw_nd$neurotreat) #X2(2,433)=3.7935, p=.1501
chisq.posthoc.test(table(raw_nd$Ego,raw_nd$neurotreat), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$neurotreat)&!is.na(raw_nd$Ego)), 
       aes(fill=neurotreat, x=Ego)) +
  geom_bar(position='dodge', stat='count') +
  scale_fill_manual(values=c("#f2e9db","#005151")) +
  theme_minimal() +
  labs(title="Ego Level by Neurodivergence Treatment", x="Ego", 
       y="Proportion (%)",legend="Method") +
  guides(fill=guide_legend(title="Management"))

####MINDSET####

# 16. Assess Diagnosis by Growth/Stress Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_nd$Arrow,raw_nd$NeuroPos)) 
  #shows 1713 answered both Diagnosis and Arrow questions
  #shows no cell <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Arrow,raw_nd$NeuroPos) #X2(2,1713)=2.7531, p=.2524
chisq.posthoc.test(table(raw_nd$Arrow,raw_nd$NeuroPos), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroPos)&!is.na(raw_nd$Arrow)), 
       aes(fill=NeuroPos, x=Arrow)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#f2e9db")) +
  theme_minimal() +
  labs(title="Neurodivergence by Recent Mindset", x="Recent Mindset", 
       y="Proportion (%)",legend="Method") +
  guides(fill=guide_legend(title="Diagnosis"))

# 17. Assess Recent Treatment Method by Growth/Stress Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_nd$Arrow,raw_nd$NeuroManage)) 
  #shows 444 answered both Treatment & Arrow questions
  #shows no cell <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Arrow,raw_nd$NeuroManage) #X2(4,444)=9.7467, p=.04492
chisq.posthoc.test(table(raw_nd$Arrow,raw_nd$NeuroManage), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$NeuroManage)&!is.na(raw_nd$Arrow)), 
       aes(fill=NeuroManage, x=Arrow)) +
  geom_bar(position=position_fill(reverse = TRUE), stat='count') +
  scale_fill_manual(values=c("#005151","#c39367","#f2e9db")) +
  theme_minimal() +
  labs(title="Recent Mindset by Neurodivergence Treatment", x="Recent Mindset", 
       y="Proportion (%)",legend="Method") +
  guides(fill=guide_legend(title="Treatment Method"))

# 18. Assess Presence of Recent Treatment by Growth/Stress Mindset (Arrow) ####
## Run descriptives
addmargins(table(raw_nd$Arrow,raw_nd$neurotreat)) 
  #shows 444 answered both Treatment & Arrow questions
  #shows no cells <5 - Chi-Squared test
## Test for significance
chisq.test(raw_nd$Arrow,raw_nd$neurotreat) #X2(2,444)=8.6336, p=.01334
chisq.posthoc.test(table(raw_nd$Arrow,raw_nd$neurotreat), method = "bonferroni")
## Create visualizations of above descriptives (optional)
ggplot(data=subset(raw_nd,!is.na(raw_nd$neurotreat)&!is.na(raw_nd$Arrow)), 
       aes(fill=neurotreat, x=Arrow)) +
  geom_bar(position='fill', stat='count') +
  scale_fill_manual(values=c("#f2e9db","#005151")) +
  theme_minimal() +
  labs(title="Recent Mindset by Neurodivergence Treatment", x="Recent Mindset", 
       y="Proportion (%)",legend="Method") +
  guides(fill=guide_legend(title="Management"))
Previous
Previous

Study #1: Initial Setup Code

Next
Next

Study #1: Mental Health Code