This report describes FA trajectories for all 18 tracts in ECT001. Session number is temporarily treated as week: ses01 = week 1, ses03 = week 3, and so on. Missing weeks retain their gaps; missing FA values are not interpolated.
# Install once in the R console if needed:
# install.packages(c("lme4", "pbkrtest", "dplyr", "ggplot2", "knitr", "rmarkdown"))
library(lme4)
library(dplyr)
library(ggplot2)
library(knitr)
# Read the CSV. Blank FA values remain missing, not zero.
csv_file <- "/Users/rajesha/Dropbox/WashU/PostDoc/Research/Projects/Active/ECT_TRD/Metrics/Session/20261119_145308/ECT001_SessionTracts_FA_MD_AD_RD.csv"
raw <- read.csv(csv_file, na.strings = c("", "NA"))
# Week is numeric. Tract and Session are categorical identifiers.
# All candidate models use exactly the same observations with available FA.
dat <- raw %>%
filter(Subject == "ECT001", !is.na(FA)) %>%
mutate(Week = as.numeric(sub("^ses", "", Session)),
Session = factor(Session),
Tract = factor(Tract_name, levels = unique(raw$Tract_name))) %>%
arrange(Tract, Week)
kable(data.frame(
Participant = "ECT001", Observations = nrow(dat),
Scans = n_distinct(dat$Session), Tracts = n_distinct(dat$Tract),
First_week = min(dat$Week), Last_week = max(dat$Week)
), caption = "Available FA observations")
| Participant | Observations | Scans | Tracts | First_week | Last_week |
|---|---|---|---|---|---|
| ECT001 | 360 | 20 | 18 | 1 | 28 |
A quadratic trajectory has a starting level, a linear term, and a bending term. Positive bending is upward; negative bending is downward.
| Model | How the tract trajectories differ |
|---|---|
| Linear, tract-specific | Each tract has its own starting level and slope. |
| Quadratic, shared curvature | Tracts have random intercepts and slopes, but all share one quadratic coefficient. |
| Quadratic, tract-specific curvature | Each tract has its own starting level, slope, and quadratic coefficient. |
The shared-curvature model can shift curves vertically and move their turning points, but their amount and direction of bending are shared. The tract-specific model allows, for example, an upward bend in one tract and a downward bend in another.
All three models also include (1 | Session) to represent
deviations shared by tracts measured during the same scan.
# Fit with maximum likelihood (ML) for valid AIC and likelihood comparisons.
# The first two models are our agreed linear and quadratic specifications.
mixed.model_linear <- lme4::lmer(
FA ~ Tract * Week + (1 | Session), data = dat, REML = FALSE
)
mixed.model_quadratic <- lme4::lmer(
FA ~ Tract * (Week + I(Week^2)) + (1 | Session),
data = dat, REML = FALSE
)
# This is the shared-curvature alternative from the earlier workflow.
# Keep the same session adjustment so it is present in both quadratic models.
mixed.model_shared <- lme4::lmer(
FA ~ Week + I(Week^2) + (Week | Tract) + (1 | Session),
data = dat, REML = FALSE,
control = lmerControl(optimizer = "bobyqa")
)
| Assessment | What it tells us |
|---|---|
| AIC | Compares fit while penalizing complexity. Lower is preferred, including when values are negative. |
| Likelihood-ratio test | Compares our tract-specific linear and quadratic models. |
| Kenward–Roger F-test | Provides a small-sample adjustment for that linear-versus-quadratic comparison. |
# AIC compares all three candidate models on the same observations.
aic_table <- data.frame(
Model = c("Linear: tract-specific slopes",
"Quadratic: shared curvature",
"Quadratic: tract-specific curvature"),
AIC = c(AIC(mixed.model_linear), AIC(mixed.model_shared),
AIC(mixed.model_quadratic))
)
kable(aic_table, digits = 2, caption = "AIC comparison: lower is preferred")
| Model | AIC |
|---|---|
| Linear: tract-specific slopes | -1422.54 |
| Quadratic: shared curvature | -1392.16 |
| Quadratic: tract-specific curvature | -1426.28 |
# These two models differ only in the added tract-specific quadratic terms.
# With lmer models, this anova() call is a likelihood-ratio test.
lrt <- anova(mixed.model_linear, mixed.model_quadratic, refit = FALSE)
# KRmodcomp automatically uses REML refits for the Kenward-Roger calculation.
# The original ML fits above remain the basis for AIC comparisons.
kr <- pbkrtest::KRmodcomp(
mixed.model_quadratic, mixed.model_linear
)$test["Ftest", ]
test_table <- data.frame(
Test = c("Likelihood ratio (chi-square)", "Kenward-Roger (F)"),
Statistic = c(lrt$Chisq[2], kr$stat),
Numerator_df = c(lrt$Df[2], kr$ndf),
Denominator_df = c(NA, kr$ddf),
P_value = c(lrt$`Pr(>Chisq)`[2], kr$p.value)
)
kable(test_table, digits = 4,
caption = "Tract-specific linear versus quadratic models")
| Test | Statistic | Numerator_df | Denominator_df | P_value |
|---|---|---|---|---|
| Likelihood ratio (chi-square) | 39.7467 | 18 | NA | 0.0023 |
| Kenward-Roger (F) | 1.9787 | 18 | 269.344 | 0.0111 |
The quadratic mixed model was preferred over the linear mixed model, with a lower AIC and a significant likelihood-ratio comparison (p = 0.0023). The Kenward–Roger test also supported adding curvature (p = 0.0111).
The tract-specific quadratic model had an AIC of -1426.28, compared with -1392.16 for the shared-curvature model. Its improvement in fit outweighed the penalty for estimating more parameters.
We therefore use the tract-specific quadratic model to describe all 18 named tracts. This allows their trajectories to bend differently, which suits the aim of describing each anatomical tract individually.
The two quadratic alternatives differ in both curvature and how tract effects are modeled. Their AIC comparison evaluates the complete models. The tests above apply specifically to the nested linear-versus-quadratic pair.
This interpretation describes the supplied dataset; review it if the input data or model specifications change. Residual diagnostics below remain part of evaluating the selected model.
Each tract receives its own figure from the selected joint quadratic model. Points are observed FA. The line is the estimated mean trajectory, with individual scan offsets excluded from the smooth curve.
Each figure reports a descriptive R² for that tract: one minus the curve’s squared prediction error divided by the total squared deviation from the tract’s mean FA. Predictions exclude session offsets, matching the plotted curve.
The shaded band is a 95% pointwise confidence interval for the mean trajectory, estimated by parametric bootstrap. It is not a prediction interval for an individual scan or a simultaneous band covering the entire curve. The intervals depend on the selected model and its assumptions.
# Re-estimate the selected specification with REML for final estimation.
# This is the same quadratic model, not a separate regression for each tract.
final_model <- update(mixed.model_quadratic, REML = TRUE)
# Evaluate the fitted curve at 100 evenly spaced weeks for each tract.
# Predictions stay within the observed week range.
prediction_grid <- expand.grid(
Week = seq(min(dat$Week), max(dat$Week), length.out = 100),
Tract = levels(dat$Tract)
)
prediction_grid$Tract <- factor(prediction_grid$Tract,
levels = levels(dat$Tract))
# re.form = NA excludes individual session offsets when drawing the curve.
prediction_grid$FA_fit <- predict(final_model, newdata = prediction_grid,
re.form = NA)
# Bootstrap confidence bands:
# 1. Simulate a new dataset from the selected model.
# 2. Refit that same model and predict its smooth trajectories.
# 3. Repeat 1,000 times; use the 2.5th and 97.5th percentiles at each week.
# A fixed seed makes the confidence bands reproducible.
# This step is the slowest part of knitting the report.
curve_boot <- lme4::bootMer(
final_model,
FUN = function(model) predict(model, newdata = prediction_grid, re.form = NA),
nsim = 1000, seed = 20261120, type = "parametric", use.u = FALSE
)
ci <- apply(curve_boot$t, 2, quantile, probs = c(0.025, 0.975))
prediction_grid$Lower <- ci[1, ]
prediction_grid$Upper <- ci[2, ]
# Use the same FA scale across figures so their shapes remain comparable.
fa_limits <- range(c(dat$FA, prediction_grid$Lower, prediction_grid$Upper)) +
c(-0.01, 0.01)
# Clean styling: no gridlines, restrained colors, and readable labels.
report_theme <- theme_classic(base_size = 13) +
theme(plot.title = element_text(face = "bold", size = 17),
plot.subtitle = element_text(colour = "#52616B", size = 11),
plot.caption = element_text(colour = "#52616B", hjust = 0),
axis.title = element_text(face = "bold"),
axis.text = element_text(colour = "#25313C"),
plot.margin = margin(12, 18, 12, 12))
These checks assess the selected quadratic model. Look for changing residual spread, unusually large deviations, and departures from the normal Q-Q line. The starting model assumes common residual variance and no remaining temporal correlation. Its statistical conclusions remain conditional on those assumptions.
# Residuals include the fitted session offsets: observed minus fitted FA.
diagnostics <- dat %>% mutate(Fitted = fitted(final_model),
Residual = residuals(final_model))
ggplot(diagnostics, aes(Fitted, Residual)) +
geom_hline(yintercept = 0, colour = "#9AA6AC", linetype = "dashed") +
geom_point(colour = "#087F8C", alpha = 0.55, size = 2) +
labs(title = "Residuals versus fitted FA", x = "Fitted FA", y = "Residual") +
report_theme
ggplot(diagnostics, aes(sample = Residual)) +
stat_qq(colour = "#087F8C", alpha = 0.6) +
stat_qq_line(colour = "#25313C") +
labs(title = "Normal Q-Q plot", x = "Theoretical normal quantile",
y = "Residual quantile") + report_theme
# Match residuals exactly one assumed week apart within the same tract.
# For example, week 10 and week 13 are not treated as adjacent weeks.
previous <- diagnostics %>%
transmute(Tract, Week = Week + 1, Previous_residual = Residual)
lag_pairs <- inner_join(diagnostics, previous, by = c("Tract", "Week"))
ggplot(lag_pairs, aes(Previous_residual, Residual)) +
geom_point(colour = "#087F8C", alpha = 0.55, size = 2) +
labs(title = "Residuals one week apart", x = "Residual one week earlier",
y = "Current residual",
caption = "Visible association would motivate review of temporal correlation.") +
report_theme
# Identify observations to review. Large residuals are not automatic exclusions.
kable(diagnostics %>% arrange(desc(abs(Residual))) %>%
select(Tract, Session, Week, FA, Fitted, Residual) %>% head(6),
digits = 4, caption = "Largest absolute residuals")
| Tract | Session | Week | FA | Fitted | Residual |
|---|---|---|---|---|---|
| Left cingulum cingulate gyrus | ses24 | 24 | 0.6261 | 0.4734 | 0.1527 |
| Right cingulum cingulate gyrus | ses24 | 24 | 0.5838 | 0.4358 | 0.1481 |
| Right inferior longitudinal fasciculus | ses15 | 15 | 0.4974 | 0.4169 | 0.0805 |
| Right cingulum angular bundle | ses05 | 5 | 0.3028 | 0.3830 | -0.0802 |
| Right cingulum angular bundle | ses24 | 24 | 0.3047 | 0.3819 | -0.0771 |
| Right cingulum angular bundle | ses07 | 7 | 0.4614 | 0.3869 | 0.0745 |
# Record the R and package versions used for the report.
sessionInfo()
## R version 4.6.2 (2026-10-29)
## Platform: aarch64-apple-darwin23
## Running under: macOS Golden Gate 27.2.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/Chicago
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] knitr_1.52 ggplot2_4.0.4 dplyr_1.2.1 lme4_2.1-0 Matrix_1.7-6
##
## loaded via a namespace (and not attached):
## [1] tidyr_1.3.2 sass_0.5.0 generics_0.1.4 lattice_0.23-1 digest_0.6.39
## [6] magrittr_2.0.5 evaluate_1.0.5 grid_4.6.2 RColorBrewer_1.1-3 fastmap_1.2.0
## [11] jsonlite_2.0.0 backports_1.5.1 purrr_1.2.2 scales_1.4.0 jquerylib_0.1.4
## [16] reformulas_0.4.4 Rdpack_2.6.6 cli_3.6.6 rlang_1.3.0 rbibutils_2.4.1
## [21] splines_4.6.2 withr_3.0.3 cachem_1.1.0 yaml_2.3.12 otel_0.3.0
## [26] parallel_4.6.2 tools_4.6.2 pbkrtest_0.5.5 nloptr_2.2.1 minqa_1.2.8
## [31] boot_1.3-32 broom_1.0.13 vctrs_0.7.3 R6_2.6.1 stats4_4.6.2
## [36] lifecycle_1.0.5 MASS_7.3-66 pkgconfig_2.0.3 pillar_1.11.1 bslib_0.12.0
## [41] gtable_0.3.6 glue_1.8.1 Rcpp_1.1.2 xfun_0.62 tibble_3.3.1
## [46] tidyselect_1.2.1 rstudioapi_0.19.0 farver_2.1.2 htmltools_0.5.9 nlme_3.1-171
## [51] labeling_0.4.3 rmarkdown_2.32 compiler_4.6.2 S7_1.0.0