LNCD

Table of Contents

Table of Contents

  • GAM
    • R: gam vs gamm vs bam
    • Knots
    • Random effects in GAM
  • LNCD Home
  • Administration
  • Notebooks
  • Journal Club Presentations
  • Publications
  • Current Projects
  • Completed Projects
  • Current Grants
  • Datasets by Project
  • Brain ROIs and Measures
  • ️Tools And Methods
  • Big Data
  • RA Homepage
  • Recent Changes
  • Maintenance
  • Site Map
  • Random Page
LNCD
Docs » GAM

GAM

Generalized Additive Models (and Generalized Additive *Mixed* Models) for non-linear relationships.

See

  • QCBS R Workshop Series
  • A recipe for accurate estimation of lifespan brain trajectories, distinguishing longitudinal and cohort effects for “various ways of formulating GAMMs for estimation of lifespan trajectories of 12 brain regions, using a large longitudinal dataset”
  • https://peerj.com/articles/6876/ from Dan:
    Hierarchical GAMs. Relevant for some of our nested designs choices
     where we look at ROI x Age interactions across some brain measure (ROI nested in person).

R: gam vs gamm vs bam

  • gam slower but gamm gives lm and gam part.
  • marginal effects doesn't work with gamm (need gam)
  • https://github.com/LabNeuroCogDevel/LNCDR/blob/master/R/growthrate_gam.R

Knots

  • checkgam
  • k.check https://rdrr.io/cran/mgcv/man/k.check.html
  • knots in mgcv
  • scam if monotonic

Random effects in GAM

#In gamm4
gamm4::gamm4(myelin ~ s(age, k = 4, fx = F) + sex, 
             REML = TRUE,
             random=~(1|subject_id), #random intercept
             family = gaussian(link = "identity"), 
             data = df) 
 
#In mgcv:gamm
mgcv::gamm(myelin ~ s(age, k = 4, fx = F) + sex, 
           method = c("REML"),
           random = list(subject_id=~1), #random intercept
           family = gaussian(link = "identity"),
           data = df)
 
#In mgcv:gam
mgcv:gam(myelin ~ s(age, k = 4, fx = F) + sex + 
         method = c("REML"),
         s(subject_id, bs = 're'), #random intercept, subject_id *must* be a factor
         family = gaussian(link = "identity"),
         data = df)
 
#### Random intercept + slopes in GAM ####
 
#In mgcv:gamm 
mgcv::gamm(myelin ~ s(age, k = 4, fx = F) + sex, 
           method = c("REML"),
           random=list(subject_id=~1, subject_id=~0+age), #uncorrelated random intercepts and slopes
           family = gaussian(link = "identity"),
           data = df)
 
mgcv::gamm(myelin ~ s(age, k = 4, fx = F) + sex, 
           method = c("REML"),
           random=list(subject_id=~1+age), #correlated random intercepts and slopes
           family = gaussian(link = "identity"),
           data = df)
 
#In mgcv:gam 
mgcv:gam(myelin ~ s(age, k = 4, fx = F) + sex,
      s(subject_id, bs = 're') +
      s(subject_id, age, bs = 're'),
    data = myelin.glasser.7T$projfrac0.3, method = 'REML')
Previous Next