Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

ScatterplotSmoothers

Smoothers to Draw Lines on Scatterplots


Description

These smoothers are used to draw nonparametric-regression lines on scatterplots produced by the scatterplot, scatterplotMatrix, and several other car functions. The functions are not meant to be called directly by the user, although the user can supply options via the smoother.args argument, the contents of which vary by the smoother (see Details below). The gamLine smoother uses the gam function in the mgcv package, the loessLine smoother uses the loess function in the stats package, and the quantregLine smoother uses the rqss function in the quantreg package.

Usage

gamLine(x, y, col=carPalette()[1], log.x=FALSE, log.y=FALSE, var=FALSE, spread=var,
    smoother.args=NULL, draw=TRUE, offset=0)

loessLine(x, y, col=carPalette()[1], log.x=FALSE, log.y=FALSE, var=FALSE, spread=var,
    smoother.args=NULL, draw=TRUE, offset=0)

quantregLine(x, y, col=carPalette()[1], log.x=FALSE, log.y=FALSE, var=FALSE, spread=var,
    smoother.args=NULL, draw=TRUE, offset=0)

Arguments

x

horizontal coordinates of points.

y

vertical coordinates of points.

col

line color.

log.x

should be set to TRUE (default is FALSE) if the horizontal axis is logged.

log.y

should be set to TRUE (default is FALSE) if the vertical axis is logged.

spread, var

the default is to plot only an estimated mean or median function. If either of these arguments is TRUE, then a measure of variability is also plotted.

smoother.args

additional options accepted by the smoother, in the form of a list of named values (see Details below).

draw

if TRUE, the default, draw the smoother on the currently active graph. If FALSE, return a list with coordinates x and y for the points that make up the smooth and if requested x.pos, y.pos, x.neg, y.neg for the spread smooths.

offset

For use when spread=TRUE, the vertical axis is sqrt(offset^2 + variance smooth).

Details

The loessLine function is a re-implementation of the loess smoother that was used in car prior to September 2012. The main enhancement is the ability to set more options through the smoother.args argument.

The gamLine function is more general than loessLine because it supports fitting a generalized spline regression model, with user-specified error distribution and link function.

The quantregLine function fits a model using splines with estimation based on L1 regression for the median and quantile regression the (optional) spread. It is likely to be more robust than the other smoothers.

The smoother.args argument is a list of named elements used to pass additional arguments to the smoother. As of November, 2016, the smoother is evaluated by default at an equally spaced grid of 50 points in the range of the horizontal variable. With any of the smoothers, you can change to, say, 100 evaluation points via the argument smoother.args=list(evaluation=100). As of version 3.0-1, the smoother.args elements col.var, lty.var, and lwd.var are equivalent to col.spread, lty.spread, and lwd.spread, respectively.

For loessLine, the default is smoother.args=list(lty.smooth=1, lwd.smooth=2, lty.spread=4, lwd.spread=2, span=2/3, degree=1, family="symmetric", iterations=4). (Prior to November 2016, the default span was 1/2.) The elements lty.smooth, lwd.smooth, and col.spread are the line type, line width, and line color, respectively of the mean or median smooth; lty.spread, lwd.spread, and col.spread are the line type, width, and color of the spread smooths, if requested. The elements span, degree, and family are passed as arguments to the loess function, along with iterations robustness iterations.

For gamLine, the default is smoother.args=list(lty.smooth=1, lwd.smooth=2, lty.spread=4, lwd.spread=2, k=-1, bs="tp", family="gaussian", link=NULL, weights=NULL). The first four elements are as for loessLine. The next two elements are passed to the gam function to control smoothing: k=-1 allows gam to choose the number of splines in the basis function; bs="tp" provides the type of spline basis to be used, with "tp" for the default thin-plate splines. The last three arguments specify a distributional family, link function, and weights as in generalized linear models. See the examples below. The spread element is ignored unless family="gaussian" and link=NULL.

For quantregLine, the default is smoother.args=list(lty.smooth=1, lwd.smooth=2, lty.spread=4, lwd.spread=2, lambda=IQR(x)). The first four elements are as for loessLine. The last element is passed to the qss function in quantreg. It is a smoothing parameter, by default a robust estimate of the scale of the horizontal axis variable. This is an arbitrary choice, and may not work well in all circumstances.

Author(s)

John Fox jfox@mcmaster.ca and Sanford Weisberg sandy@umn.edu.

See Also

Examples

scatterplot(prestige ~ income, data=Prestige)
scatterplot(prestige ~ income, data=Prestige, smooth=list(smoother=gamLine))
scatterplot(prestige ~ income, data=Prestige,
    smooth=list(smoother=quantregLine))

scatterplot(prestige ~ income | type, data=Prestige)
scatterplot(prestige ~ income | type, data=Prestige,
    smooth=list(smoother=gamLine))
scatterplot(prestige ~ income | type, data=Prestige,
    smooth=list(smoother=quantregLine))
scatterplot(prestige ~ income | type, data=Prestige, smooth=FALSE)

scatterplot(prestige ~ income | type, data=Prestige, spread=TRUE)
scatterplot(prestige ~ income | type, data=Prestige,
    smooth=list(smoother=gamLine), spread=TRUE)
scatterplot(prestige ~ income | type, data=Prestige,
    smooth=list(smoother=quantregLine), spread=TRUE)

scatterplot(weight ~ repwt | sex, spread=TRUE, data=Davis,
    smooth=list(smoother=loessLine))
scatterplot(weight ~ repwt | sex, spread=TRUE, data=Davis,
    smooth=list(smoother=gamLine)) # messes up
scatterplot(weight ~ repwt | sex, spread=TRUE, data=Davis,
    smooth=list(smoother=quantregLine)) #  robust

set.seed(12345)
w <- 1 + rpois(100, 5)
x <- rnorm(100)
p <- 1/(1 + exp(-(x + 0.5*x^2)))
y <- rbinom(100, w, p)
scatterplot(y/w ~ x, smooth=list(smoother=gamLine, family="binomial",
    weights=w))
scatterplot(y/w ~ x, smooth=list(smoother=gamLine, family=binomial,
    link="probit", weights=w))
scatterplot(y/w ~ x, smooth=list(smoother=loessLine), reg=FALSE)

y <- rbinom(100, 1, p)
scatterplot(y ~ x, smooth=list(smoother=gamLine, family=binomial))

car

Companion to Applied Regression

v3.0-10
GPL (>= 2)
Authors
John Fox [aut, cre], Sanford Weisberg [aut], Brad Price [aut], Daniel Adler [ctb], Douglas Bates [ctb], Gabriel Baud-Bovy [ctb], Ben Bolker [ctb], Steve Ellison [ctb], David Firth [ctb], Michael Friendly [ctb], Gregor Gorjanc [ctb], Spencer Graves [ctb], Richard Heiberger [ctb], Pavel Krivitsky [ctb], Rafael Laboissiere [ctb], Martin Maechler [ctb], Georges Monette [ctb], Duncan Murdoch [ctb], Henric Nilsson [ctb], Derek Ogle [ctb], Brian Ripley [ctb], William Venables [ctb], Steve Walker [ctb], David Winsemius [ctb], Achim Zeileis [ctb], R-Core [ctb]
Initial release
2020-09-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.