Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

detrend

Remove Linear Trends


Description

Removes the mean value or (piecewise) linear trend from a vector or from each column of a matrix.

Usage

detrend(x, tt = 'linear', bp = c())

Arguments

x

vector or matrix, columns considered as the time series.

tt

trend type, ‘constant’ or ‘linear’, default is ‘linear’.

bp

break points, indices between 1 and nrow(x).

Details

detrend computes the least-squares fit of a straight line (or composite line for piecewise linear trends) to the data and subtracts the resulting function from the data.

To obtain the equation of the straight-line fit, use polyfit.

Value

removes the mean or (piecewise) linear trend from x and returns it in y=detrend(x), that is x-y is the linear trend.

Note

Detrending is often used for FFT processing.

See Also

Examples

t <- 1:9
x <- c(0, 2, 0, 4, 4, 4, 0, 2, 0)
x - detrend(x, 'constant')
x - detrend(x, 'linear')

y <- detrend(x, 'linear', 5)
## Not run: 
plot(t, x, col="blue")
lines(t, x - y, col="red")
grid()
## End(Not run)

pracma

Practical Numerical Math Functions

v2.3.3
GPL (>= 3)
Authors
Hans W. Borchers [aut, cre]
Initial release
2021-01-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.