detrend <- function(y,smooth=F,...){ # Description: # # Removes time trend from a time series # # Usage: # # detrend(y,smooth=FALSE,...) # # Arguments: # # y: a vector containing a time series of data # # smooth: Logical. If FALSE (the default) removes linear time trend by # fitting ordinary least squares regression to the time series y. # If TRUE, then time trend is removed by fitting a local weighted # regression (lowess) to the time series y # # ...: Additional arguments passed to lowess # # Value: # # Vector with the detrended data. # # Author: # # Caio Coelho 9 Jan 2006 # # Examples: # # ts <- rnorm(100,25,2) # detrend(ts) # detrend(ts,smooth=TRUE) # detrend(ts,smooth=TRUE,f=1/3) if(!smooth){ x<-seq(1:length(y)) lm(y ~ x)$res } else{ y-lowess(y,...)$y } }