Skip to content

Commit

Permalink
initial commit of mgcv GAM tract profile tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeatman committed Mar 30, 2024
1 parent 1456a8d commit 72c6ef8
Show file tree
Hide file tree
Showing 2 changed files with 519 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/howto_examples/gam-tract-profiles-example.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
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
g1 <- gam(md ~ s(nodeID,class, bs='fs') + s(nodeID, 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}
# Yet another way to do the same thing and I don't get the difference
g1 <- gam(md ~ s(nodeID,by=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 )
```
453 changes: 453 additions & 0 deletions examples/howto_examples/gam-tract-profiles-example.html

Large diffs are not rendered by default.

0 comments on commit 72c6ef8

Please sign in to comment.