plot_nls: Plot a non-linear or non-parametric regression model

Description Usage Arguments Value Examples

Description

Convenient function for adding curves to an existing plot, or to plot the data with the fitted curve. For non-linear regression plotting (plot_nls), works for simple non-linear regression models fit with nls, grouped non-linear regression (with nlsList), and non-linear quantile regression fit with nlrq from the quantreg package. When plotting an nlsList object, plot_nls plots the fitted curve for each of the groups specified in the model, and sets colours with lines.col and points.col arguments. For local regression models fitted with loess, use the plot_loess function, which additionally adds a confidence interval around the fitted curve (unless band=FALSE).

Usage

1
2
3
4
5
6
plot_nls(object, col = NULL, band = TRUE, plotdata = TRUE,
  extrapolate = FALSE, lines.col = palette(), points.col = palette(),
  ci.col = "#BEBEBEB3", lwd = 1, lty = 1, add = FALSE, xlab = NULL,
  ylab = NULL, coverage = 0.95, xlim = NULL, ...)

plot_loess(object, ...)

Arguments

object

The object returned by nls, nlsList or loess

col

Colour to be used for the data symbols and the fitted line, unless lines.col and points.col are provided

band

For plot_loess, whether to add a confidence band. Not yet implemented for plot_nls

plotdata

Logical. Whether to add the data points to the plot.

extrapolate

Logical (default FALSE). If TRUE, extends the fitted curve beyond the data, based on the x-axis limits specified.

lines.col

Colour(s) for the fitted lines. When plotting a nlsList object, can be a vector that represents colours for each group.

points.col

Colour(s) for the data symbols. When plotting a nlsList object, can be a vector that represents colours for each group.

ci.col

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

lwd

Thickness of the line (see par)

lty

Line type (see par)

add

Logical. Whether to add to current plot (default FALSE).

xlab

Label for x-axis

ylab

Label for y-axis

coverage

If confidence band to be plotted, the coverage (e.g. for 95% confidence interval, use 0.95)

xlim

X-axis limits (optional).

...

Further arguments passed to plot

Value

Returns the predicted values used in plotting (invisibly), as a dataframe with columns 'predvar' (regularly spaced predictor values), and 'fit' (fitted values). For plot_loess also returns confidence intervals, standard error, and df of the residual.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Plot an nls object
chick <- as.data.frame(ChickWeight)
fit0 <- nls(weight ~ a*Time^b, data=chick, start=list(a=10, b=1.1))
plot_nls(fit0)

# Plot a grouped nls object
library(nlme)
fit1 <- nlsList(weight ~ a*Time^b|Diet, data=chick, start=list(a=10, b=1.1))
plot_nls(fit1)

# Plot a local regression object, with confidence interval
l <- loess(wt ~ disp, data=mtcars)
plot_loess(l)

# To plot behind the data:
with(mtcars, plot(disp, wt, pch=19,
 panel.first=plot_loess(l, plotdata=FALSE)))

RemkoDuursma/nlshelper documentation built on May 9, 2019, 9:39 a.m.