plot_smooth: Visualization of smooths.

View source: R/inspect.R

plot_smoothR Documentation

Visualization of smooths.

Description

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.

Usage

plot_smooth(
  x,
  view = NULL,
  cond = list(),
  plot_all = NULL,
  rm.ranef = TRUE,
  n.grid = 30,
  rug = NULL,
  add = FALSE,
  se = 1.96,
  sim.ci = 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,
  ...
)

Arguments

x

A gam object, produced by gam or bam.

view

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.

cond

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.

plot_all

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

rm.ranef

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

n.grid

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

rug

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.

add

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

se

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

sim.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: 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.

shade

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

eegAxis

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.

col

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

lwd

The line width for the lines of the plot.

lty

The line type for the lines of the plot.

print.summary

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

main

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

xlab

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

ylab

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

xlim

the x limits of the plot.

ylim

the y limits of the plot.

h0

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

v0

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

transform

Function for transforming the fitted values. Default is NULL.

transform.view

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

legend_plot_all

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

hide.label

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

Notes

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

Author(s)

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

Examples

data(simdat)

## 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:
dev.new(width=8, height=4) # use x11(,8,4) on Linux
par(mfrow=c(1,2))
fvisgam(m1, view=c('Time', 'Trial'), 
    plot.type='contour', color='topo', main='interaction',
    rm.ranef=TRUE)
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',
    rm.ranef=TRUE)
# 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 sim.ci: simultaneous CI instead of pointwise CI
dev.new(width=8, height=4) # use x11(,8,4) on Linux
par(mfrow=c(1,2))
plot_smooth(m2, view='Time', plot_all='Group', rm.ranef=TRUE)
plot_smooth(m2, view='Time', rm.ranef=TRUE, plot_all='Group', sim.ci=TRUE, 
    add=TRUE, shade=FALSE, xpd=TRUE)
plot_smooth(m2, view='Time', rm.ranef=TRUE, sim.ci=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,
   xlim=c(1,2000))


## 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.