fvisgam: Visualization of nonlinear interactions, summed effects.

View source: R/inspect.R

fvisgamR Documentation

Visualization of nonlinear interactions, summed effects.


Produces perspective or contour plot views of gam model predictions of the additive effects interactions. The code is based on the script for vis.gam, but allows to cancel random effects.


  view = NULL,
  cond = list(),
  n.grid = 30,
  too.far = 0,
  col = NA,
  color = "terrain",
  contour.col = NULL,
  add.color.legend = TRUE,
  se = -1,
  sim.ci = FALSE,
  plot.type = "contour",
  xlim = NULL,
  ylim = NULL,
  zlim = NULL,
  nCol = 50,
  rm.ranef = TRUE,
  print.summary = getOption("itsadug_print"),
  transform = NULL,
  transform.view = NULL,
  hide.label = FALSE,
  dec = NULL,
  show.diff = FALSE,
  col.diff = 1,
  alpha.diff = 0.5,
  f = 1.96,



A gam object, produced by gam or bam.


A two-value vector containing the names of the two main effect terms to be displayed on the x and y dimensions of the plot. 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.


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


Plot grid nodes that are too far from the points defined by the variables given in view can be excluded from the plot. too.far determines what is too far. The grid is scaled into the unit square along with the view variables and then grid nodes more than too.far from the predictor variables are excluded.


The colors for the facets of the plot.


The color scheme to use for plots. One of 'topo', 'heat', 'cm', 'terrain', 'gray' or 'bw'. Alternatively a vector with some colors can be provided for a custom color palette (see examples).


sets the color of contours when using plot.


Logical: whether or not to add a color legend. Default is TRUE. If FALSE (omitted), one could use the function gradientLegend to add a legend manually at any position.


If less than or equal to zero then only the predicted surface is plotted, but if greater than zero, then 3 surfaces are plotted, one at the predicted values minus se standard errors, one at the predicted values and one at the predicted values plus se standard errors.


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: https://fromthebottomoftheheap.net/2016/12/15/simultaneous-interval-revisited/. 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.


one of 'contour' or 'persp' (default is 'contour').


A two item array giving the lower and upper limits for the x- axis scale. NULL to choose automatically.


A two item array giving the lower and upper limits for the y- axis scale. NULL to choose automatically.


A two item array giving the lower and upper limits for the z- axis scale. NULL to choose automatically.


The number of colors to use in color schemes.


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


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


Function for transforming the fitted values. Default is NULL.


List with two functions for transforming the values on the x- and y-axis respectively. If one of the axes need to be transformed, set the other to NULL (no transformation). See examples below.


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


Numeric: number of decimals for rounding the color legend. When NULL, no rounding (default). If -1, automatically determined. Note: if value = -1, rounding will be applied also when zlim is provided.


Logical: whether or not to indicate the regions that are significantly different from zero. Note that these regions are just an indication and dependent on the value of n.grid. Defaults to FALSE.


Color to shade the nonsignificant areas.


Level of transparency to mark the nonsignificant areas.


Scaling factor to determine the CI from the se, for marking the difference with 0. Only applies when se is smaller or equal to zero and show.diff is set to TRUE.


other options to pass on to persp, image or contour. In particular ticktype='detailed' will add proper axes labeling to the plots.


When the argument show.diff is set to TRUE a shading area indicates where the confidence intervals include zero. Or, in other words, the areas that are not significantly different from zero. Be careful with the interpretation, however, as the precise shape of the surface is dependent on model constraints such as the value of choose.k and the smooth function used, and the size of the confidence intervals are dependent on the model fit and model characteristics (see vignette('acf', package='itsadug')). In addition, the value of n.grid determines the precision of the plot.


Jacolien van Rij and Martijn Wieling. Modification of vis.gam from package mgcv of Simon N. Wood.

See Also

vis.gam, plot.gam

Other Functions for model inspection: dispersion(), gamtabs(), inspect_random(), plot_data(), plot_parametric(), plot_smooth(), 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)

# Plot summed effects:
vis.gam(m1, view=c('Time', 'Trial'), plot.type='contour', color='topo')
# Same plot:
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=FALSE)
# Without random effects included:
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE)

# Notes on the color legend:
# Labels can easily fall off the plot, therefore the numbers can be
# automatically rounded.
# To do the rounding, set dec=-1:
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE,
# For custom rounding, set dec to a value:
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE,
# To increase the left marging of the plot (so that the numbers fit):
oldmar <- par()$mar
par(mar=oldmar + c(0,0,0,1) ) # add one line to the right
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE,
par(mar=oldmar) # restore to default settings

# changing the color palette:
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE,
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE,
    color=c('blue', 'white', 'red'), col=1)

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

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

# too.far: 
n <- which(simdat$Time > 1500 & simdat$Trial > 5)
simdat[n,]$Y <- NA
simdat[simdat$Trial == -3,]$Y <- NA
m1 <- bam(Y ~ te(Time, Trial)+s(Time, Subject, bs='fs', m=1),
    data=simdat, discrete=TRUE)
fvisgam(m1, view=c('Time', 'Trial'), rm.ranef=TRUE, too.far=.03)

## End(Not run)
# see the vignette for examples:
vignette('inspect', package='itsadug')

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