plot_smooth: Visualization of smooths.

View source: R/inspect.R

plot_smoothR Documentation

Visualization of smooths.


Plots a smooth from a gam or bam model based on predictions. In contrast with the default plot.gam, this function plots the summed effects and optionally removes the random effects.


  view = NULL,
  cond = list(),
  plot_all = NULL,
  rm.ranef = TRUE,
  n.grid = 30,
  rug = NULL,
  add = FALSE,
  se = 1.96, = FALSE,
  shade = TRUE,
  eegAxis = FALSE,
  col = NULL,
  lwd = NULL,
  lty = NULL,
  print.summary = getOption("itsadug_print"),
  main = NULL,
  xlab = NULL,
  ylab = NULL,
  xlim = NULL,
  ylim = NULL,
  h0 = 0,
  v0 = NULL,
  transform = NULL,
  transform.view = NULL,
  legend_plot_all = NULL,
  hide.label = FALSE,



A gam object, produced by gam or bam.


Text string containing the name of the smooth to be displayed. Note that variables coerced to factors in the model formula won't work as view variables.


A named list of the values to use for the other predictor terms (not in view). Used for choosing between smooths that share the same view predictors.


A vector with a name / names of model predictors, for which all levels should be plotted.


Logical: whether or not to remove random effects. Default is TRUE.


The number of grid nodes in each direction used for calculating the plotted surface.


Logical: when TRUE then the covariate to which the plot applies is displayed as a rug plot at the foot of each plot. By default set to NULL, which sets rug to TRUE when the dataset size is <= 10000 and FALSE otherwise. Setting to FALSE will speed up plotting for large datasets.


Logical: whether or not to add the lines to an existing plot, or start a new plot (default).


If less than or equal to zero then only the predicted surface is plotted, but if greater than zero, then the predicted values plus confidence intervals are plotted. The value of se will be multiplied with the standard error (i.e., 1.96 results in 95%CI and 2.58). Default is set to 1.96 (95%CI).

Logical: Using simultaneous confidence intervals or not (default set to FALSE). The implementation of simultaneous CIs follows Gavin Simpson's blog of December 15, 2016: This interval is calculated from simulations based. Please specify a seed (e.g., set.seed(123)) for reproducable results. Note: in contrast with Gavin Simpson's code, here the Bayesian posterior covariance matrix of the parameters is uncertainty corrected (unconditional=TRUE) to reflect the uncertainty on the estimation of smoothness parameters.


Logical: Set to TRUE to produce shaded regions as confidence bands for smooths (not avaliable for parametric terms, which are plotted using termplot).


Logical: whether or not to reverse the y-axis, plotting the negative amplitudes upwards as traditionally is done in EEG research. If eeg.axes is TRUE, labels for x- and y-axis are provided, when not provided by the user. Default value is FALSE.


The colors for the lines and the error bars of the plot.


The line width for the lines of the plot.


The line type for the lines of the plot.


Logical: whether or not to print summary. Default set to the print info messages option (see infoMessages).


Changing the main title for the plot, see also title.


Changing the label for the x axis, defaults to a description of x.


Changing the label for the y axis, defaults to a description of y.


the x limits of the plot.


the y limits of the plot.


A vector indicating where to add solid horizontal lines for reference. By default no values provided.


A vector indicating where to add dotted vertical lines for reference. By default no values provided.


Function for transforming the fitted values. Default is NULL.


Function for transforming the values on the x-axis. Defaults to NULL (no transformation).


Legend location. This could be a keyword from the list 'bottomright', 'bottom', 'bottomleft', 'left', 'topleft', 'top', 'topright', 'right' and 'center', or a list with x and y coordinate (e.g., list(x=0,y=0)).


Logical: whether or not to hide the label (i.e., 'fitted values'). Default is FALSE.


other options to pass on to lines and plot, see par


This function plots the summed effects, including intercept and other predictors. For plotting partial effects, see the function plot.gam, or see the examples with get_modelterm for more flexibility (e.g., plotting using the lattice package or ggplots).


Jacolien van Rij and Martijn Wieling.

See Also

plot.gam, plot_diff

Other Functions for model inspection: dispersion(), fvisgam(), gamtabs(), inspect_random(), plot_data(), plot_parametric(), plot_topo(), pvisgam()



## Not run: 
# Model with random effect and interactions:
m1 <- bam(Y ~ te(Time, Trial)+s(Time, Subject, bs='fs', m=1),
    data=simdat, discrete=TRUE)

# Default plot produces only surface of Time x Trial:
plot(m1, select=1)
# Only the Time component:
plot_smooth(m1, view='Time')
# Note the summary that is printed.

# without random effects:
plot_smooth(m1, view='Time', rm.ranef=TRUE)

# Plot summed effects:, height=4) # use x11(,8,4) on Linux
fvisgam(m1, view=c('Time', 'Trial'), 
    plot.type='contour', color='topo', main='interaction',
arrows(x0=0, x1=2200, y0=-5, y1=-5, col='red', 
    code=2, length=.1, lwd=2, xpd=TRUE)
plot_smooth(m1, view='Time', cond=list(Trial=-5),
    main='Trial=-5', rm.ranef=TRUE)

# Model with random effect and interactions:
m2 <- bam(Y ~ Group + s(Time, by=Group)
    +s(Time, Subject, bs='fs', m=1),
    data=simdat, discrete=TRUE)

# Plot all levels of a predictor:
plot_smooth(m2, view='Time', plot_all='Group',
# It also possible to combine predictors in plot_all.
# Note: this is not a meaningfull plot, because Subjects
# fall in only one group. Just for illustration purposes!
plot_smooth(m2, view='Time', plot_all=c('Group', 'Subject'))
# Clearly visible difference in confidence interval, because  
# a01 does not occur in Group 'Children':
# (Note that this plot generates warning)
plot_smooth(m2, view='Time', plot_all=c('Group', 'Subject'), cond=list(Subject='a01'))

# Using simultaneous CI instead of pointwise CI, height=4) # use x11(,8,4) on Linux
plot_smooth(m2, view='Time', plot_all='Group', rm.ranef=TRUE)
plot_smooth(m2, view='Time', rm.ranef=TRUE, plot_all='Group',, 
    add=TRUE, shade=FALSE, xpd=TRUE)
plot_smooth(m2, view='Time', rm.ranef=TRUE,, col='red')

# Using transform
# Plot log-transformed dependent predictor on original scale:
plot_smooth(m1, view='Time', rm.ranef=TRUE, transform=exp)

# Notes on transform.view: 
# This will generate an error, because x-values <= 0 will result in NaN:
plot_smooth(m1, view='Time', rm.ranef=TRUE, transform.view=log)
# adjusting the x-axis helps:
plot_smooth(m1, view='Time', rm.ranef=TRUE, transform.view=log,

## End(Not run)

# and for a quick overview of plotfunctions:
vignette('overview', package='itsadug')

itsadug documentation built on June 17, 2022, 5:05 p.m.