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

Description Usage Arguments Value Examples

View source: R/plot_nls.R

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, and grouped non-linear regression (with nlsList), in which case one fitted curve for each group is drawn on the same plot. For local regression models fitted with loess, use the plot_loess function which additionally adds a confidence interval around the fitted curve.

Usage

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

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.

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)

...

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

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