The table present the different plot functions in the packages mgcv** [@Wood_2006; @Wood_2011] and **itsadug for visualizing GAMM models.


Partial effect Sum of all effects Sum of “fixed” effects1
surface plot.gam() vis.gam()
pvisgam() fvisgam()
smooth plot.gam() plot_smooth()
group estimates plot.gam()2 plot_parametric()
random smooths get_random(), inspect_random()

1: include rm.ranef=TRUE to zero all random effects.

2: include all.terms=TRUE to visualize parametric terms.


Examples

library(itsadug)
library(mgcv)
data(simdat)

## Not run: 
# Model with random effect and interactions:
m1 <- bam(Y ~ Group + te(Time, Trial, by=Group)
    + s(Time, Subject, bs='fs', m=1, k=5),
    data=simdat)
# Simple model with smooth:
m2 <-  bam(Y ~ Group + s(Time, by=Group)
    + s(Subject, bs='re')
    + s(Subject, Time, bs='re'),
    data=simdat)

Summary model m1:

gamtabs(m1, type='html')
A. parametric coefficients Estimate Std. Error t-value p-value
(Intercept) 1.3376 0.4861 2.7517 0.0059
GroupAdults 2.4505 0.7013 3.4944 0.0005
B. smooth terms edf Ref.df F-value p-value
te(Time,Trial):GroupChildren 22.1689 22.3328 1617.2033 < 0.0001
te(Time,Trial):GroupAdults 22.8091 22.9460 1396.6762 < 0.0001
s(Time,Subject) 177.0111 178.0000 5805.5604 < 0.0001

Summary model m2:

gamtabs(m2, type='html')
A. parametric coefficients Estimate Std. Error t-value p-value
(Intercept) 2.0719 2.9511 0.7021 0.4826
GroupAdults 3.1895 4.1735 0.7642 0.4447
B. smooth terms edf Ref.df F-value p-value
s(Time):GroupChildren 8.6203 8.9584 1630.0224 < 0.0001
s(Time):GroupAdults 8.8371 8.9921 8798.1162 < 0.0001
s(Subject) 33.9912 34.0000 125335342.9139 < 0.0001
s(Subject,Time) 33.9878 34.0000 125318454.9383 < 0.0001

a. Surfaces

Plotting the partial effects of te(Time,Trial):GroupAdults and te(Time,Trial):GroupChildren.

pvisgam()

par(mfrow=c(1,2), cex=1.1)

pvisgam(m1, view=c("Time", "Trial"), select=1,
        main="Children", zlim=c(-12,12))
pvisgam(m1, view=c("Time", "Trial"), select=2,
        main="Adults", zlim=c(-12,12))

plot of chunk unnamed-chunk-5

Notes:

fvisgam()

Plotting the fitted effects of te(Time,Trial):GroupAdults and te(Time,Trial):GroupChildren.

par(mfrow=c(1,2), cex=1.1)

fvisgam(m1, view=c("Time", "Trial"), cond=list(Group="Children"),
        main="Children", zlim=c(-12,12), rm.ranef=TRUE)
fvisgam(m1, view=c("Time", "Trial"), cond=list(Group="Adults"),
        main="Adults", zlim=c(-12,12), rm.ranef=TRUE)

plot of chunk unnamed-chunk-6

Notes:

b. Smooths

plot.gam()

Plotting the partial effects of s(Time):GroupAdults and s(Time):GroupChildren.

par(mfrow=c(1,2), cex=1.1)

plot(m2, select=1, shade=TRUE, rug=FALSE, ylim=c(-15,10))
abline(h=0)
plot(m2, select=2, shade=TRUE, rug=FALSE, ylim=c(-15,10))
abline(h=0)

plot of chunk unnamed-chunk-7

Notes:

Alternatively:

par(mfrow=c(1,1), cex=1.1)

# Get model term data:
st1 <- get_modelterm(m2, select=1)
st2 <- get_modelterm(m2, select=2)

# plot model terms:
emptyPlot(2000, c(-15,10), h=0,
    main='s(Time)')
plot_error(st1$terms, st1$fit, st1$se.fit, shade=TRUE)
plot_error(st2$terms, st2$fit, st2$se.fit, shade=TRUE, col='red', lty=5, lwd=2)

# add legend:
legend('bottomleft',
  legend=c('Children', 'Adults'),
  fill=c(alpha('black'), alpha('red')),
  bty='n')

plot of chunk unnamed-chunk-8

plot_smooth()

Plotting the fitted effects of te(Time,Trial):GroupAdults and te(Time,Trial):GroupChildren i.e., the plot includes intercept and estimates for the other modelterms.

par(mfrow=c(1,2), cex=1.1)

plot_smooth(m1, view="Time", cond=list(Group="Children"),
    rm.ranef=TRUE, ylim=c(-6,10))
plot_smooth(m1, view="Time", cond=list(Group="Adults"),
    col="red", rug=FALSE, add=TRUE,
    rm.ranef=TRUE)

# or alternatively:
plot_smooth(m1, view="Time", plot_all="Group",
    rm.ranef=TRUE)

plot of chunk unnamed-chunk-9

Notes:

c. Group estimates

plot.gam()

Plotting the partial effect of grouping predictors such as Group:

par(mfrow=c(1,1), cex=1.1)

plot.gam(m1, select=4, all.terms=TRUE, rug=FALSE)

plot of chunk unnamed-chunk-10

Alternatively, use get_coefs() to extract the coefficients and plot these:

coefs <- get_coefs(m1)
coefs

par(mfrow=c(1,2), cex=1.1)

b <- barplot(coefs[,1], beside=TRUE, 
             main="Parametric terms",
             ylim=c(0,5))
errorBars(b, coefs[,1], coefs[,2], xpd=TRUE)

# Note that the effect of Group is a *difference* estimate
# between intercept (=GroupChildren) and Group Adults

b2 <- barplot(coefs[1,1], beside=TRUE, 
             main="Estimate for Group",
             ylim=c(0,5), xlim=c(0.1,2.5))
mtext(row.names(coefs), at=b, side=1, line=1)
abline(h=coefs[1,1], lty=2)
rect(b[2]-.4, coefs[1,1], b[2]+.4, coefs[1,1]+coefs[2,1],
     col='gray')
errorBars(b, coefs[,1]+c(0,coefs[1,1]), coefs[,2], xpd=TRUE)

plot of chunk unnamed-chunk-11

##             Estimate Std. Error
## (Intercept) 1.337552  0.4860860
## GroupAdults 2.450473  0.7012568

Notes:

plot_parametric()

Plotting the fitted effects of grouping predictors such as Group:

pp <- plot_parametric(m1, pred=list(Group=c("Children", "Adults")) )

plot of chunk unnamed-chunk-12

pp
## $fv
##      Group    Time Trial Subject       fit        CI
## 1 Children 989.899     0     a01 0.7700983 1.7731153
## 2   Adults 989.899     0     a01 6.4457358 0.1681991

Random effects

get_random()

For extracting the random effects coefficients (random adjustments of intercept and slopes):

par(mfrow=c(1,2), cex=1.1)
plot(m2, select=3)
plot(m2, select=4)

plot of chunk unnamed-chunk-13

Or alternatively:

pp <- get_random(m2)

emptyPlot(range(pp[[1]]), range(pp[[2]]), h=0,v=0,
     xlab='Random intercepts', ylab='Random slopes',
     main='Correlation')

text(pp[[1]], pp[[2]], labels=names(pp[[1]]), 
     col='steelblue', xpd=TRUE)

plot of chunk unnamed-chunk-14

inspect_random()

For plotting and extracting the random smooths:

par(mfrow=c(1,2), cex=1.1)

inspect_random(m1, select=3, main='s(Time, Subject)')

children <- unique(simdat[simdat$Group=="Children", "Subject"])
adults   <- unique(simdat[simdat$Group=="Adults", "Subject"])

inspect_random(m1, select=3, main='Averages', 
      fun=mean, 
      cond=list(Subject=children))
inspect_random(m1, select=3, 
      fun=mean, cond=list(Subject=adults),
      add=TRUE, col='red', lty=5)

# add legend:
legend('bottomleft',
  legend=c('Children', 'Adults'),
  col=c('black', 'red'), lty=c(1,5),
  bty='n')

plot of chunk unnamed-chunk-15

References