Skip to content
This repository was archived by the owner on Feb 8, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions examples/howto_examples/gam-tract-profiles-example.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: "AFQ-GAMS-Tutorial"
author: "Jason Yeatman"
date: "2024-03-30"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## GAMs Tutorial

First we'll pull the Sarica data and merge nodes.csv (diffusion data) with subjects.csv (participant meta data)

```{r, warning=FALSE, message=FALSE}
library(dplyr)
library(mgcv)
library(gratia)
library(ggplot2)
df <- read.csv('https://raw.githubusercontent.com/yeatmanlab/Sarica_2017/gh-pages/data/nodes.csv')
df <- inner_join(df, read.csv('https://raw.githubusercontent.com/yeatmanlab/Sarica_2017/gh-pages/data/subjects.csv'))
mutate(df, class = factor(class), subjectID = factor(subjectID))-> df

```

```{r, message=FALSE, warning=FALSE}
# Fit a simple GAM with a random effect of subject and smoothers that
# differ by class (control or ALS)
g1 <- gam(md ~ s(nodeID,class, bs='fs') + s(subjectID, bs= 're'), data=df)
# Get smooth estimates
dfs <- smooth_estimates(g1) %>% add_confint(coverage=.68)

# Remove random effects and plot
filter(dfs, type != 'Random effect') %>%
ggplot(aes(x=nodeID,y=est, color=class)) +
geom_line() +
geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )

```

```{r, message=FALSE, warning=FALSE}
# The above plot looks good but now let's let the curves vary across # subjects too. What I notice here is that it dramatically effects the standard error
g2 <- gam(md ~ s(nodeID,class, bs='fs') + s(nodeID, subjectID, bs= 're'), data=df)
# Get smooth estimates
dfs <- smooth_estimates(g2) %>% add_confint(coverage=.68)

# Remove random effects and plot
filter(dfs, type != 'Random effect') %>%
ggplot(aes(x=nodeID,y=est, color=class)) +
geom_line() +
geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )
```

```{r, message=FALSE, warning=FALSE}
# Yet another way to do the same thing and I don't get the difference
g3 <- gam(md ~ s(nodeID,by=class, bs='fs', k=10) + s(subjectID, bs= 're'), data=df)
# Get smooth estimates
dfs <- smooth_estimates(g3) %>% add_confint(coverage=.68)

# Remove random effects and plot
filter(dfs, type != 'Random effect') %>%
ggplot(aes(x=nodeID,y=est, color=class)) +
geom_line() +
geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )
```

Now calculate differences between the smooths for the different factors and plot out the difference an 95% CI

```{r}
ds<-difference_smooths(g3, smooth="s(nodeID)")
ggplot(ds,aes(x=nodeID,y=diff)) +
geom_line() +
geom_ribbon(aes(ymin = lower, ymax=upper),alpha = 0.2 ) +
geom_hline(yintercept=0)
```

Now look at continuous measures along the tract profile

```{r}
# Yet another way to do the same thing and I don't get the difference
g4 <- gam(md ~ s(nodeID,ALSFRSbulbar, bs='fs', k=10) + s(subjectID, bs= 're'), data=df)
dfs <- smooth_estimates(g4) %>% add_confint(coverage=.68)
#filter(dfs,smooth=='s(nodeID)') %>%
# ggplot(aes(x=nodeID,y=est)) +
# geom_line() +
# geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )

# 2d plot showing how tract profiles vary as a function of AFS bulbar
filter(dfs,smooth=='s(nodeID,ALSFRSbulbar)') %>%
ggplot(aes(x=nodeID, y=ALSFRSbulbar))+
geom_raster(aes(fill=est))

# Now plot separate tract profiles by bulbar
dfs %>% mutate(ALSFRSbulbar.factor=factor(ALSFRSbulbar)) %>% filter(ALSFRSbulbar== sort(unique(dfs$ALSFRSbulbar))[2] | ALSFRSbulbar== sort(unique(dfs$ALSFRSbulbar))[99]) %>%
filter(smooth=='s(nodeID,ALSFRSbulbar)') %>%
ggplot(aes(x=nodeID,y=est,color=ALSFRSbulbar.factor)) +
geom_line(show.legend = FALSE) +
geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )

```
482 changes: 482 additions & 0 deletions examples/howto_examples/gam-tract-profiles-example.html

Large diffs are not rendered by default.