detrend(x, tt = 'linear', bp = c())
nrow(x)
.x
and returns it
in y=detrend(x)
, that is x-y
is the linear trend.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
.
polyfit
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)
plot(t, x, col="blue")
lines(t, x - y, col="red")
grid()
Run the code above in your browser using DataLab