R/preprocess.R

Defines functions detrend

Documented in detrend

detrend <- function(y,demean=TRUE,det=FALSE,norm=FALSE, smooth=FALSE,...){

  # Description:
  #   Preprocessors for a time series
  #
  # Usage:
  #
  #   detrend(y,demean=TRUE,det=FALSE,norm=FALSE,smooth=FALSE,...)
  #
  # Arguments:
  #
  #   y:      a vector containing a time series of data
  #
  #   demean: Logical.If TRUE, time series y will be centered. 
  #
  #   det: 	  Logical.If TRUE, then time trend is removed by fitting a local weighted
  #           regression (loess) to the time series y
  #
  #   smooth: Logical.If TRUE, then time trend is removed by a local fitting regression(loess) 
  #			  to the time series y
  #
  #   ...:    Additional arguments passed to loess
  #
  # Value:
  # 
  #   Vector with the detrended data.
  #
  # Author:
  #
  #   Patrick Crutcher
  # 
  # Examples:
  #
  #      ts <- rnorm(100,25,2)
  #      detrend(ts)
  #      detrend(ts,smooth=TRUE)
  #      detrend(ts,smooth=TRUE,f=1/3)
	


	if(demean){
		
		dt<- y - mean(y)
		return(dt)
	}	
	
	if(norm){
		
		dt<- (y - mean(y))/var(y)
		return(dt)
	}

	if(det){
		
		x<- seq(1:length(y))
		dt<- lm(y ~ x)$res
		return(dt)
		}
	if(smooth){
	
		dt<- y-loess(y,...)$y
		return(dt)
	}
}

Try the ssa package in your browser

Any scripts or data that you put into this service are public.

ssa documentation built on May 2, 2019, 5:54 p.m.