plot_gam: Plot a generalized additive model

Description Usage Arguments Details Examples

View source: R/plot_gam.R

Description

This is a simple wrapper to fit and plot a basic type of generalized additive model. The fitted model is of the form gam(Y ~ s(X, k)), which can be fitted by a specified grouping variable (using the g argument). Also supported is an optional random effect, in which case the model fitted is gamm(Y ~ s(X, k=k), random = list(R=~1), data=dfr).

Usage

1
2
3
4
5
plot_gam(x, y, g = NULL, data, fittype = c("gam", "lm"), kgam = 4,
  R = NULL, log = "", axes = TRUE, fitoneline = FALSE,
  points.col = NULL, lines.col = NULL, ci.col = "#D3D3D3B3",
  xlab = NULL, ylab = NULL, band = TRUE, plotit = TRUE, add = FALSE,
  npred = 101, lwd = 2, ...)

Arguments

x

Variable for X axis (unquoted)

y

Variable for Y axis (unquoted)

g

Variable for grouping (unquoted); optional

data

Dataframe containing x and y

fittype

Either 'gam' (default), or 'lm' in which case a simple linear model is fit - useful for comparison.

kgam

the k parameter for smooth terms in gam.

R

An optional random effect (quoted)

log

Whether to add log axes for x or y (but no transformations are done).

axes

Logical (default TRUE), whether to add axes to the plot.

fitoneline

Whether to fit only one curve to the entire dataset, regardless of whether a grouping variable was defined. Default FALSE.

points.col

Colours of the points, can be a vector (one value for each group, if present).

lines.col

Colours of the lines, can be a vector (one value for each group, if present).

ci.col

Colour of the confidence band, if plotted. Defaults to a transparent grey colour.

xlab

X-axis label

ylab

Y-axis label

band

Logical. If true, plots the confidence band (as a transparent polygon).

plotit

Logical (default TRUE); if FALSE, suppresses the plot.

add

Logical (default FALSE), if TRUE, adds to an existing plot.

npred

Number of x values to use for prediction

lwd

Line thickness (see par)

...

Further arguments passed to plot or points, for example to change colour of plotting symbols.

Details

In either case, the k parameter necessary for the GAM fit can be set using the kgam argument. Seechoose.k for details on this setting (it is important you don't just use the default value here!).

Examples

1
2
3
4
5
6
7
data(Loblolly)
plot_gam(age, height, data=Loblolly)
plot_gam(age, height, Seed, data=Loblolly, band=FALSE, lines.col="black")
plot_gam(age, height, Seed, data=Loblolly, band=FALSE, lines.col="black", fittype="lm")

data(ChickWeight)
plot_gam(Time, weight, Diet, R="Chick", data=ChickWeight, lines.col=rainbow(4))

Example output

Loading required package: nlme
Loading required package: broom
Loading required package: dplyr

Attaching package: 'dplyr'

The following object is masked from 'package:nlme':

    collapse

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading required package: mgcv
This is mgcv 1.8-28. For overview type 'help("mgcv-package")'.
Loading required package: magicaxis
Warning message:
no DISPLAY variable so Tk is not available 

nlshelper documentation built on May 2, 2019, 10:22 a.m.