smoother: Smooth noisy data

View source: R/smoother.R

smootherR Documentation

Smooth noisy data

Description

Simple smoothing function with several methods.

Usage

smoother(x, n = NULL, method = "movav", plot = TRUE, panel = "all")

Arguments

x

numeric. A numeric vector.

n

numeric. Smoothing factor. See Details.

method

string. Smoothing method: "movav", "loess", "spline", or "supersmooth".

plot

logical. Plots the result.

panel

Which panel(s) of the plot to show. Default is "all", other options are 1, 2 or 3. See Details.

Details

Smooths a vector using one of four methods. The n input controls the degree of smoothing. If n is between 0 and 1 it represents a proportion of the data length (i.e. 0.1 is a window of 10% of the total data). If n is above 1 it specifies a window of a number of data values (i.e. n = 20 in a dataset of 200 values would represent a window of 10% of the data length). The same n value will have radically different results with different methods.

For all methods a vector of equal length as x containing the smoothed values is returned.

method = "movav"

A moving average (rolling mean). Good for data which is noisy, but which does not oscillate sharply. n controls the window size for the moving average. For each data value, half the window width forward and half backward are averaged to produce the smoothed values. At the start and end of the vector where n/2 would extend beyond the start or end, the available data points are averaged.

method = "loess"

Locally estimated scatterplot smoothing. Good for data which oscillates in a non-predictable manner. The n input is parsed to be the span operator of the loess function. For more control over smoothing using this method, see loess().

method = "spline"

Cubic smoothing spline. Good for data which oscillates in a somewhat regular manner, such as a sinusoidal wave. The n input is parsed to be the spar operator of the smooth.spline function. If n = NULL, the smooth.spline 'cross-validation' option is used to estimate the 'best' spar value between 0 and 1, though this is not returned and results may vary. For more control over smoothing using this method, see smooth.spline().

method = "supersmooth"

Friedman's SuperSmoother. Good for data which oscillates in a somewhat regular manner, such as a sinusoidal wave. The n input is parsed to be the span operator of the supsmu function. If n = NULL, the supsmu 'cross-validation' option is used to estimate the 'best' span value between 0 and 1, though this is not returned and results may vary. Smoothing performs well at much lower n values in comparison to other methods. See examples. For more control over smoothing using this method, see supsmu().

Value

Returns a vector of smoothed values of the same length as the input.

Plotting

If plot = TRUE a three panel plot is produced of the original data values, the smoothed data values, and the smoothed data plotted as a line over the original data. Each single panel can be plotted alone using the panel argument (e.g. panel = 3).

Author(s)

Nicholas Carey - nicholascarey@gmail.com

Examples

## Moving average smoothing
## Using a 10% rolling window
smoother(resp_noisy.rd, n = 0.1, method = "movav")
## Same size window expressed as number of data values
smoother(resp_noisy.rd, n = 94, method = "movav")

## Loess smoothing
## Good fit to the data, but doesn't capture the sinusoidal structure that well
smoother(sine_noisy.rd, n = 0.1, method = "loess")
## Higher 'n' works much better in this case
smoother(sine_noisy.rd, n = 0.2, method = "loess")
## But too high and range is lost
smoother(sine_noisy.rd, n = 0.4, method = "loess")

## Spline smoothing
smoother(swim_y.rd, n = 0.4, method = "spline")

## Supersmooth
## Same 'n' as above is much too high
smoother(swim_y.rd, n = 0.4, method = "supersmooth")
## Much smaller values work better with this method
smoother(swim_y.rd, n = 0.01, method = "supersmooth")

## Change plot output
## Plot all panels
smoother(swim_y.rd, n = 0.01, method = "supersmooth",
       plot = TRUE, panel = "all")
## Only plot third panel
smoother(swim_y.rd, n = 0.01, method = "supersmooth",
       plot = TRUE, panel = 3)


nicholascarey/caRey documentation built on Sept. 30, 2023, 3:47 a.m.