Skip to Content
R in a Nutshell, 2nd Edition
book

R in a Nutshell, 2nd Edition

by Joseph Adler
October 2012
Beginner to intermediate
721 pages
21h 38m
English
O'Reilly Media, Inc.
Content preview from R in a Nutshell, 2nd Edition

Smoothing

This section describes a number of functions for fitting piecewise smooth curves to data. Functions in this section are particularly useful for plotting charts; there are even convenience functions for using these functions to show fitted values in some graphics packages.

Splines

One method for fitting a function to source data is with splines. With a linear model, a single line is fitted to all the data. With spline methods, a set of different polynomials is fitted to different sections of the data.

You can compute simple cubic splines with the spline function in the stats package:

spline(x, y = NULL, n = 3 * length(x), method = "fmm", xmin = min(x),
    xmax = max(x), xout, ties = mean)

Here is a description of the arguments to smooth.spline.

ArgumentDescriptionDefault
xA vector specifying the predictor variable, or a two-column matrix specifying both the predictor and the response variables. 
yIf x is a vector, then y is a vector containing the response variable.NULL
nIf xout is not specified, then interpolation is done at n equally spaced points between xmin and xmax.3*length(x)
methodSpecifies the type of spline. Allowed values include "fmm", "natural", "periodic", and "monoH.FC"."fmm"
xminLowest x value for interpolations.min(x)
xmaxHighest x value for interpolations.max(x)
xoutAn optional vector of values specifying where interpolation should be done. 
tiesA method for handling ties. Either the string "ordered" or a function that returns a single numeric value.mean

To return a function ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R in a Nutshell

R in a Nutshell

Joseph Adler
The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book

The R Book

Michael J. Crawley
R Packages

R Packages

Hadley Wickham

Publisher Resources

ISBN: 9781449358204Errata Page