Learn R Programming

itsadug (version 2.0)

plot_smooth: 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 = NULL, n.grid = 30, rug = TRUE, col = NULL, add = FALSE,
  se = 1.96, shade = TRUE, eegAxis = FALSE,
  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 (default) then the covariate to which the plot applies is displayed as a rug plot at the foot of each plot of a 1-d smooth. Setting to FALSE will speed up plotting for large datasets.
col
The colors for the lines and the error bars of the plot.
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
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.
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).

See Also

plot.gam, plot_diff

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

Examples

Run this code
data(simdat)

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

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

# 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, 
# just for illustration purposes!
plot_smooth(m2, view='Time', plot_all=c("Group", "Subject"))

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

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

Run the code above in your browser using DataLab