detrend(x [,Degree]), REAL vector or matrix x, integer scalar or vector Degree |

y1 <- detrend(y, k, time:tt) detrends each column of REAL vector or matrix by subtracting a degree k polynomial P_k(tt). Each column of y, which can have no MISSING values, is considered as a time series observed at time points tt[1], tt[2], ..., tt[N], where N = nrows(y). tt is a REAL vector of length N and with no MISSING values. y1 has the same size and shape as y. k is usually an integer scalar but can be a length ny = ncols(y) vector of integers, in which case column j is detrended by a degree k[j] polynomial. When k = 1, a linear trend is removed. When k = 0, only the sample mean is subtracted. When k < 0, nothing is subtracted and y1 = y. Each polynomial is fit by least squares. You can find the trend values themselves by y - detrend(y,i,time:tt). detrend(y, time:tt) is equivalent to detrend(y, 1, time:tt), subtracting least squares regression lines a + b*tt from columns of y. detrend(y [,k]) is equivalent to detrend(y [,k], time:run(nrows(y))). This is what you should use with discrete parameter time series observed at equally spaced time points. Caution: Some early versions of detrend() used k = 0 as the default, so that detrend(y) removed only the column means. It was changed because it seemed inconsistent with the usual meaning of "detrend". Examples: Compute residuals from linear trend: Cmd> detrended <- detrend(y) # or detrend(y,1,time:run(nrows(y))) Plot a discrete parameter monthly time series starting January 1995 together with cubic trend: Cmd> tsplot(hconcat(y,y-detrend(y,3)),1995,1/12,title:"Time series") Detrend y[,1] linearly, y[,2] using a cubic polynomial and y[,3] not at all: Cmd> y1 <- detrend(y,vector(1,3,-1)) See also regress(), orthopoly(), tsplot().

Gary Oehlert 2003-01-15