R: Detrend a set of observations

detrend {oce} | R Documentation

## Detrend a set of observations

### Description

Detrends `y` by subtracting a linear trend in `x`, to create a vector that is zero for its first and last finite value. If the second parameter (`y`) is missing, then `x` is taken to be `y`, and a new `x` is constructed with `seq_along()`. Any `NA` values are left as-is.

### Usage

    detrend(x, y)

### Arguments

`x` | a vector of numerical values. If `y` is not given, then `x` is taken for `y`.
`y` | an optional vector

### Details

A common application is to bring the end points of a time series down to zero, prior to applying a digital filter. (See examples.)

### Value

A list containing `Y`, the detrended version of `y`, and the intercept `a` and slope `b` of the linear function of `x` that is subtracted from `y` to yield `Y`.

### Author(s)

Dan Kelley

### Examples

    x <- seq(0, 0.9 * pi, length.out=50)
    y <- sin(x)
    y[1] <- NA
    y[10] <- NA
    plot(x, y, ylim=c(0, 1))
    d <- detrend(x, y)
    points(x, d$Y, pch=20)
    abline(d$a, d$b, col='blue')
    abline(h=0)
    points(x, d$Y + d$a + d$b * x, col='blue', pch='+')

[Package oce version 1.7-2 Index]