1 + 1[1] 2
2 + 2[1] 4
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Need to have R Markdown
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
2 + 2[1] 4
You can add options to executable code like this
# echo: false
2 * 2[1] 4
The echo: false option disables the printing of code (only output is displayed).
Set up your mirror and import your data:
options(repos = c(CRAN = "https://cran.rstudio.com"))
install.packages("readxl")
The downloaded binary packages are in
/var/folders/4w/gljslth537l5fz1ys3jw1xj40000gn/T//RtmpXfGqY6/downloaded_packages
library(readxl)
d <- read_excel("moral_injury.xlsx",
sheet = "Structural Analysis FINAL")New names:
• `Scan_ID` -> `Scan_ID...2`
• `26_associated_int_curr` -> `26_associated_int_curr...131`
• `26_associated_int_curr` -> `26_associated_int_curr...220`
• `Scan_ID` -> `Scan_ID...229`
• `LThickness` -> `LThickness...238`
• `RThickness` -> `RThickness...239`
• `LSurfArea` -> `LSurfArea...240`
• `RSurfArea` -> `RSurfArea...241`
• `ICV` -> `ICV...242`
• `Scan_ID` -> `Scan_ID...243`
• `LThickness` -> `LThickness...252`
• `RThickness` -> `RThickness...253`
• `LSurfArea` -> `LSurfArea...254`
• `RSurfArea` -> `RSurfArea...255`
• `ICV` -> `ICV...256`
• `Scan_ID` -> `Scan_ID...257`
• `LThickness` -> `LThickness...266`
• `RThickness` -> `RThickness...267`
• `LSurfArea` -> `LSurfArea...268`
• `RSurfArea` -> `RSurfArea...269`
• `ICV` -> `ICV...270`
#There was a typo for a subject, so i'm tossing anyone with a total score of over a 400
d <- d[d$CAPS_total_life <= 400, ]
d_ptsd <- d[d$Dx <= "PTSD", ]Install packages
install.packages("ggplot2")
The downloaded binary packages are in
/var/folders/4w/gljslth537l5fz1ys3jw1xj40000gn/T//RtmpXfGqY6/downloaded_packages
install.packages("dplyr")
The downloaded binary packages are in
/var/folders/4w/gljslth537l5fz1ys3jw1xj40000gn/T//RtmpXfGqY6/downloaded_packages
install.packages("tidyverse")
The downloaded binary packages are in
/var/folders/4w/gljslth537l5fz1ys3jw1xj40000gn/T//RtmpXfGqY6/downloaded_packages
Load your library
library(ggplot2)
library(dplyr)
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(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ lubridate 1.9.2 ✔ tibble 3.1.8
✔ purrr 1.0.1 ✔ tidyr 1.3.0
✔ readr 2.1.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
Variables that we want:
- Moral Injury Total Score and Subscales
- PTSD Dx
- CAPS Total Score
- Age
- Gender
- ROI level:
Inferior Parietal
Precuneus
Insula
Posterior Cingulate Cortex
#View data
View(d)
#Clinical / Demographics
sex <- d$Gender
age <- d$Age
mies <- d$MIES_total_9item
m_trans_other <- d$MIES_transgressions_others
m_trans_self <- d$MIES_transgressions_self
m_betrayal <- d$MIES_betrayal
dx <- d$Dx
caps_c <- d$CAPS_total_curr
caps_l <- d$CAPS_total_life
#Structural Level Stats
lh_ip_thick <- d$L_inferiorparietal_thickavg
rh_ip_thick <- d$R_inferiorparietal_thickavg
lh_pre_thick <- d$L_precuneus_thickavg
rh_pre_thick <- d$R_precuneus_thickavg
lh_ins_thick <- d$L_insula_thickavg
rh_ins_thick <- d$R_insula_thickavg
lh_pcc_thick <- d$L_posteriorcingulate_thickavg
rh_pcc_thick <- d$R_posteriorcingulate_thickavgLets look at the distribution of some of our clinical measures
#Show me a table of men and women in the sample
table(sex)sex
0 1
47 4
#Show me summary stats of our age distribution
summary(age) Min. 1st Qu. Median Mean 3rd Qu. Max.
32.00 38.50 49.00 47.45 55.00 67.00
sd(age)[1] 10.40829
#Histogram of age
hist(age)#PTSD Dx
table(dx)dx
Control PTSD
34 17
#CAPS Scores
hist(caps_c, col = "blue", breaks = "Sturges", main = "Gabbi Insula")hist(caps_l, col = "red")#There was a typo in the CAPS_L scores, i'm not sure what the real score was supposed to be so I tossed the subject
#MIES
summary(mies) Min. 1st Qu. Median Mean 3rd Qu. Max.
9.00 11.00 18.00 19.88 27.00 48.00
sd(mies)[1] 10.30659
hist(mies, col = "pink")hist(m_trans_other)hist(m_trans_self)hist(m_betrayal)#MIES and CAPS Scores
ggplot(d, aes(x = mies, y = caps_c, color = factor(dx))) +
geom_point() +
xlab("MIES scores") +
ylab("CAPS scores") +
ggtitle("Scatterplot of MIES scores and Current CAPS scores") +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
ggplot(d, aes(x = mies, y = caps_l, color = factor(dx))) +
geom_point() +
xlab("MIES scores") +
ylab("CAPS scores") +
ggtitle("Scatterplot of MIES scores and Lifetime CAPS scores") +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
ggplot(d, aes(x = m_trans_other, y = caps_c, color = factor(dx))) +
geom_point() +
xlab("MIES scores") +
ylab("CAPS scores") +
ggtitle("Scatterplot of MIES Transgression (Other) scores and Current CAPS scores") +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
ggplot(d, aes(x = m_trans_self, y = caps_c, color = factor(dx))) +
geom_point() +
xlab("MIES scores") +
ylab("CAPS scores") +
ggtitle("Scatterplot of MIES Transgression (Self) scores and Current CAPS scores") +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
ggplot(d, aes(x = m_betrayal, y = caps_c, color = factor(dx))) +
geom_point() +
xlab("MIES scores") +
ylab("CAPS scores") +
ggtitle("Scatterplot of MIES Betrayal and Current CAPS scores") +
geom_smooth(method = "lm", se = FALSE)`geom_smooth()` using formula = 'y ~ x'
#Using ggplot here, i defined my x and y axis, added PTSD diagnosis as a factor and colored the points based off diagnoses, and I added two separate trendlines for PTSD and non-PTSD groups Correlation matrix
cor_matrix <- cor(d[, c("MIES_total_9item", "MIES_transgressions_others", "MIES_transgressions_self", "MIES_betrayal", "CAPS_total_curr", "CAPS_total_life", "Age")])
print(cor_matrix) MIES_total_9item MIES_transgressions_others
MIES_total_9item 1.00000000 0.81417836
MIES_transgressions_others 0.81417836 1.00000000
MIES_transgressions_self 0.85569465 0.54144517
MIES_betrayal 0.81536098 0.57673020
CAPS_total_curr 0.44306652 0.46753868
CAPS_total_life 0.41004099 0.50607687
Age -0.01795292 0.02965426
MIES_transgressions_self MIES_betrayal
MIES_total_9item 0.855694649 0.81536098
MIES_transgressions_others 0.541445169 0.57673020
MIES_transgressions_self 1.000000000 0.49007610
MIES_betrayal 0.490076105 1.00000000
CAPS_total_curr 0.219593066 0.47293311
CAPS_total_life 0.162091764 0.42797669
Age -0.001464101 -0.06934986
CAPS_total_curr CAPS_total_life Age
MIES_total_9item 0.4430665 0.41004099 -0.017952920
MIES_transgressions_others 0.4675387 0.50607687 0.029654261
MIES_transgressions_self 0.2195931 0.16209176 -0.001464101
MIES_betrayal 0.4729331 0.42797669 -0.069349863
CAPS_total_curr 1.0000000 0.90118678 0.038518502
CAPS_total_life 0.9011868 1.00000000 0.042169776
Age 0.0385185 0.04216978 1.000000000
Visualize the matrix
# Load the corrplot package
install.packages("corrplot")
The downloaded binary packages are in
/var/folders/4w/gljslth537l5fz1ys3jw1xj40000gn/T//RtmpXfGqY6/downloaded_packages
library(corrplot)corrplot 0.92 loaded
# Visualize the correlation matrix
corrplot(cor_matrix, method = "circle", type = "upper", tl.col = "black", tl.srt = 45)Modeling Function
Function to predict thickness from PTSD-Current
thickness_ptsd <- function(d, region) {
library(broom)
# create formula for linear regression
formula <- as.formula(paste(region, " ~ caps_c + age + sex"))
# fit the linear regression model
model <- lm(formula = formula, data = d)
# tidy the model results
results <- tidy(model)
# return the model results
return(results)
}Apply the function to our ROIs
thickness_ptsd(d, "lh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.77 0.0757 36.6 3.44e-36
2 caps_c -0.000421 0.000525 -0.802 4.27e- 1
3 age -0.00668 0.00153 -4.35 7.19e- 5
4 sex 0.0299 0.0590 0.506 6.15e- 1
thickness_ptsd(d, "rh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.75 0.0710 38.7 2.59e-37
2 caps_c -0.000808 0.000493 -1.64 1.08e- 1
3 age -0.00459 0.00144 -3.19 2.55e- 3
4 sex -0.0189 0.0554 -0.341 7.35e- 1
thickness_ptsd(d, "lh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.65 0.0734 36.1 6.25e-36
2 caps_c -0.000428 0.000509 -0.841 4.04e- 1
3 age -0.00373 0.00149 -2.51 1.55e- 2
4 sex -0.0709 0.0572 -1.24 2.21e- 1
thickness_ptsd(d, "rh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.70 0.0722 37.3 1.40e-36
2 caps_c -0.000655 0.000501 -1.31 1.98e- 1
3 age -0.00373 0.00146 -2.55 1.42e- 2
4 sex -0.0358 0.0563 -0.636 5.28e- 1
thickness_ptsd(d, "lh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.11 0.117 26.5 7.00e-30
2 caps_c -0.000690 0.000814 -0.848 4.01e- 1
3 age -0.00394 0.00238 -1.66 1.04e- 1
4 sex -0.105 0.0915 -1.15 2.56e- 1
thickness_ptsd(d, "rh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.14 0.0864 36.4 4.49e-36
2 caps_c -0.00140 0.000599 -2.34 2.36e- 2
3 age -0.00432 0.00175 -2.47 1.72e- 2
4 sex -0.163 0.0673 -2.42 1.92e- 2
thickness_ptsd(d, "lh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.81 0.116 24.2 3.27e-28
2 caps_c -0.000966 0.000803 -1.20 2.35e- 1
3 age -0.00462 0.00235 -1.97 5.46e- 2
4 sex -0.0157 0.0902 -0.174 8.62e- 1
thickness_ptsd(d, "rh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.80 0.0984 28.5 2.65e-31
2 caps_c 0.000236 0.000682 0.346 7.31e- 1
3 age -0.00528 0.00199 -2.65 1.10e- 2
4 sex 0.0464 0.0767 0.606 5.47e- 1
PTSD-Lifetime
thickness_ptsd_l <- function(d, region) {
library(broom)
# create formula for linear regression
formula <- as.formula(paste(region, " ~ caps_l + age + sex"))
# fit the linear regression model
model <- lm(formula = formula, data = d)
# tidy the model results
results <- tidy(model)
# return the model results
return(results)
}thickness_ptsd_l(d, "lh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.77 0.0766 36.2 5.78e-36
2 caps_l -0.000290 0.000440 -0.660 5.13e- 1
3 age -0.00669 0.00154 -4.35 7.32e- 5
4 sex 0.0284 0.0595 0.477 6.36e- 1
thickness_ptsd_l(d, "rh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.75 0.0732 37.5 1.10e-36
2 caps_l -0.000356 0.000420 -0.847 4.01e- 1
3 age -0.00463 0.00147 -3.15 2.85e- 3
4 sex -0.0178 0.0569 -0.314 7.55e- 1
thickness_ptsd_l(d, "lh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.65 0.0744 35.6 1.23e-35
2 caps_l -0.000221 0.000427 -0.518 6.07e- 1
3 age -0.00375 0.00149 -2.51 1.56e- 2
4 sex -0.0710 0.0578 -1.23 2.25e- 1
thickness_ptsd_l(d, "rh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.70 0.0729 37.1 1.90e-36
2 caps_l -0.000560 0.000418 -1.34 1.87e- 1
3 age -0.00372 0.00146 -2.55 1.42e- 2
4 sex -0.0402 0.0566 -0.711 4.81e- 1
thickness_ptsd_l(d, "lh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.10 0.119 26.0 1.58e-29
2 caps_l -0.000223 0.000685 -0.325 7.47e- 1
3 age -0.00399 0.00239 -1.66 1.03e- 1
4 sex -0.103 0.0927 -1.11 2.73e- 1
thickness_ptsd_l(d, "rh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.15 0.0876 36.0 7.24e-36
2 caps_l -0.00113 0.000503 -2.25 2.91e- 2
3 age -0.00432 0.00176 -2.46 1.76e- 2
4 sex -0.171 0.0680 -2.52 1.52e- 2
thickness_ptsd_l(d, "lh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.80 0.118 23.7 9.04e-28
2 caps_l -0.000408 0.000679 -0.602 5.50e- 1
3 age -0.00467 0.00237 -1.97 5.50e- 2
4 sex -0.0142 0.0918 -0.154 8.78e- 1
thickness_ptsd_l(d, "rh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.80 0.0993 28.2 4.19e-31
2 caps_l 0.000199 0.000570 0.349 7.29e- 1
3 age -0.00528 0.00199 -2.65 1.09e- 2
4 sex 0.0480 0.0771 0.622 5.37e- 1
MIES Score
thickness_mies <- function(d, region) {
library(broom)
# create formula for linear regression
formula <- as.formula(paste(region, " ~ mies + age + sex"))
# fit the linear regression model
model <- lm(formula = formula, data = d)
# tidy the model results
results <- tidy(model)
# return the model results
return(results)
}thickness_mies(d, "lh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.76 0.0822 33.6 1.63e-34
2 mies -0.0000101 0.00156 -0.00649 9.95e- 1
3 age -0.00672 0.00154 -4.35 7.20e- 5
4 sex 0.0339 0.0593 0.572 5.70e- 1
thickness_mies(d, "rh_ip_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.76 0.0782 35.3 1.72e-35
2 mies -0.00126 0.00148 -0.847 4.01e- 1
3 age -0.00470 0.00147 -3.20 2.48e- 3
4 sex -0.0140 0.0564 -0.247 8.06e- 1
thickness_mies(d, "lh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.64 0.0797 33.1 3.00e-34
2 mies -0.0000785 0.00151 -0.0519 9.59e- 1
3 age -0.00378 0.00150 -2.52 1.50e- 2
4 sex -0.0669 0.0575 -1.16 2.50e- 1
thickness_mies(d, "rh_pre_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.69 0.0791 34.0 9.11e-35
2 mies -0.000598 0.00150 -0.398 6.92e- 1
3 age -0.00381 0.00149 -2.56 1.38e- 2
4 sex -0.0309 0.0571 -0.540 5.92e- 1
thickness_mies(d, "lh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.16 0.125 25.4 4.50e-29
2 mies -0.00345 0.00237 -1.46 1.52e- 1
3 age -0.00409 0.00234 -1.74 8.78e- 2
4 sex -0.107 0.0900 -1.18 2.42e- 1
thickness_mies(d, "rh_ins_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 3.15 0.0976 32.2 1.09e-33
2 mies -0.00161 0.00185 -0.867 3.91e- 1
3 age -0.00450 0.00183 -2.45 1.80e- 2
4 sex -0.153 0.0704 -2.18 3.46e- 2
thickness_mies(d, "lh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.82 0.126 22.4 1.01e-26
2 mies -0.00175 0.00239 -0.732 4.68e- 1
3 age -0.00476 0.00237 -2.01 5.03e- 2
4 sex -0.0104 0.0909 -0.115 9.09e- 1
thickness_mies(d, "rh_pcc_thick")# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.79 0.106 26.3 8.90e-30
2 mies 0.000950 0.00201 0.472 6.39e- 1
3 age -0.00524 0.00199 -2.63 1.15e- 2
4 sex 0.0464 0.0764 0.607 5.47e- 1
Plot
#Reshaping the data to make this code work
d_regions <- data.frame(lh_ip_thick,
lh_pre_thick,
rh_pre_thick,
lh_ins_thick,
rh_ins_thick,
lh_pcc_thick,
rh_pcc_thick,
mies,
caps_c,
caps_l,
dx)# reshape data into tidy format
d_tidy <- d_regions %>%
pivot_longer(cols = contains("thick"),
names_to = "region",
values_to = "thickness")
# create scatterplot with linear regression line for each region
ggplot(d_tidy, aes(x = mies, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Moral Injury Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'
CAPS-Current Plot
ggplot(d_tidy, aes(x = caps_c, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Current PTSD Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'
CAPS-Lifetime
ggplot(d_tidy, aes(x = caps_l, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Lifetime PTSD Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'
Ok, looks like lots of people have a 0 for current PTSD. lets see what the plot looks like when we toss people with a 0:
# Subset the data frame to exclude rows where caps_c == 0
d_regions_nonzeo <- subset(d_tidy, caps_c != 0)
ggplot(d_regions_nonzeo, aes(x = caps_c, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Current PTSD Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'
table(dx)dx
Control PTSD
34 17
# Only PTSD cases now
d_regions_ptsd <- filter(d_tidy, dx == "PTSD")
ggplot(d_regions_ptsd, aes(x = caps_c, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Current PTSD Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'
# Only PTSD cases now
d_regions_control <- filter(d_tidy, dx == "Control")
ggplot(d_regions_control, aes(x = caps_c, y = thickness)) +
geom_point() +
facet_wrap(~region, scales = "free_y") +
geom_smooth(method = "lm", se = FALSE) +
xlab("Current PTSD Scores") +
ylab("Cortical Thickness")`geom_smooth()` using formula = 'y ~ x'