From 81200ff368c25474c26dec309486b18545a54ef9 Mon Sep 17 00:00:00 2001 From: "Jason D. Yeatman" Date: Fri, 29 Mar 2024 19:48:39 -0700 Subject: [PATCH] added difference and CI --- .../gam-tract-profiles-example.Rmd | 18 ++++++++++++++---- .../gam-tract-profiles-example.html | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/examples/howto_examples/gam-tract-profiles-example.Rmd b/examples/howto_examples/gam-tract-profiles-example.Rmd index 31ca1e485..25db99aac 100644 --- a/examples/howto_examples/gam-tract-profiles-example.Rmd +++ b/examples/howto_examples/gam-tract-profiles-example.Rmd @@ -41,9 +41,9 @@ ggplot(aes(x=nodeID,y=est, color=class)) + ```{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) +g2 <- 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) +dfs <- smooth_estimates(g2) %>% add_confint(coverage=.68) # Remove random effects and plot filter(dfs, type != 'Random effect') %>% @@ -54,9 +54,9 @@ ggplot(aes(x=nodeID,y=est, color=class)) + ```{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) +g3 <- 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) +dfs <- smooth_estimates(g3) %>% add_confint(coverage=.68) # Remove random effects and plot filter(dfs, type != 'Random effect') %>% @@ -64,3 +64,13 @@ 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) +``` diff --git a/examples/howto_examples/gam-tract-profiles-example.html b/examples/howto_examples/gam-tract-profiles-example.html index 01d41d9c2..395c1cd3e 100644 --- a/examples/howto_examples/gam-tract-profiles-example.html +++ b/examples/howto_examples/gam-tract-profiles-example.html @@ -381,9 +381,9 @@

GAMs Tutorial

geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )

# 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)
+g2 <- 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)
+dfs <- smooth_estimates(g2) %>% add_confint(coverage=.68)
 
 # Remove random effects and plot
 filter(dfs, type != 'Random effect') %>%
@@ -392,9 +392,9 @@ 

GAMs Tutorial

geom_ribbon(aes(ymin = lower_ci, ymax=upper_ci),alpha = 0.2 )

# 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)
+g3 <- 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)
+dfs <- smooth_estimates(g3) %>% add_confint(coverage=.68)
 
 # Remove random effects and plot
 filter(dfs, type != 'Random effect') %>%
@@ -402,6 +402,14 @@ 

GAMs Tutorial

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

+
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)
+