diff options
-rw-r--r-- | NAMESPACE | 2 | ||||
-rw-r--r-- | R/preprocess.R | 44 | ||||
-rw-r--r-- | man/detrend.Rd | 34 | ||||
-rw-r--r-- | man/predict.detrend.Rd | 30 |
4 files changed, 43 insertions, 67 deletions
@@ -65,7 +65,7 @@ export(residplot) export(sim) export(spa) export(step) -export(trendInfo) +export(trInfo) import(bitops) import(ggplot2) import(polynom) diff --git a/R/preprocess.R b/R/preprocess.R index 429328b..c8435db 100644 --- a/R/preprocess.R +++ b/R/preprocess.R @@ -1,33 +1,35 @@ #' Remove offsets and linear trends #' -#' Removes the offsets or linear trends in each of the input and output matrices. -#' +#' Removes offsets or trends from data #' @param x an object of class \code{idframe} -#' @param type trend type - "constant" or "linear". (Default: \code{"constant"}) +#' @param type argument indicating the type of trend to be removed (Default=\code{0}) +#' \itemize{ +#' \item type=\code{0}: Subtracts mean value from each signal +#' \item type=\code{1}: Subtracts a linear trend (least-squres fit) +#' \item type=\code{trInfo} object: Subtracts a trend specified by the object +#' } #' #' @return -#' A list containing the following elements -#' \item{fitted.values}{\code{idframe} object with detrended variables} -#' \item{output_trend}{\code{list} containing trend fits for each output -#' variable} -#' \item{input_trend}{\code{list} containing trend fits for each input -#' variable} -#' +#' A list containing two objects: the detrended data and the trend information +#' +#' @details +#' \code{R} by default doesn't allow return of multiple objects. The \code{\%=\%} +#' operator and \code{g} function in this package facillitate this behaviour. See +#' the examples section for more information. #' #' @examples #' data(cstr) -#' fit <- detrend(cstr,type="linear") # remove linear trends -#' Zdetrend <- predict(fit) # get the detrended data -#' -#' demean <- detrend(cstr) # remove offsets -#' Zcent <- predict(demean) # get the centered data +#' datatrain <- dataSlice(cstr,end=4500) +#' datatest <- dataSlice(cstr,4501) +#' g(Ztrain,tr) %=% detrend(datatrain) # Remove means +#' g(Ztest) %=% detrend(datatest,tr) #' -#' @seealso \code{\link{predict.detrend}}, \code{\link[stats]{lm}} +#' @seealso \code{\link[stats]{lm}} #' @export detrend <- function(x,type=0){ z <- x reg <- time(x) - if(class(type)=="trendInfo"){ # remove custom trend + if(class(type)=="trInfo"){ # remove custom trend if(nOutputSeries(x)!=0){ fit <- sweep(sweep(matrix(rep(reg,nOutputSeries(x)),ncol=nOutputSeries(x)), 2,type$OutputSlope,"*"),2,type$OutputOffset,"+") @@ -40,7 +42,7 @@ detrend <- function(x,type=0){ } tinfo <- type } else if(type == 0){ # remove means - tinfo <- trendInfo() + tinfo <- trInfo() if(nOutputSeries(x)!=0){ outputData(z) <- apply(outputData(x),2,scale,T,F) tinfo$OutputOffset <- colMeans(x$output) @@ -62,7 +64,7 @@ detrend <- function(x,type=0){ trend } - tinfo <- trendInfo() + tinfo <- trInfo() if(nOutputSeries(x)!=0){ output_trend <- multilm(outputData(x),formula,reg) outputData(z) <- ts(sapply(output_trend,resid),start=reg[1], @@ -87,11 +89,11 @@ detrend <- function(x,type=0){ } #' @export -trendInfo <- function(InputOffset=numeric(0),OutputOffset=numeric(0), +trInfo <- function(InputOffset=numeric(0),OutputOffset=numeric(0), InputSlope=numeric(0),OutputSlope=numeric(0)){ l <- list(InputOffset=InputOffset,OutputOffset=OutputOffset, InputSlope=InputSlope,OutputSlope=OutputSlope) - class(l) <- "trendInfo" + class(l) <- "trInfo" l } diff --git a/man/detrend.Rd b/man/detrend.Rd index a195171..62b71ab 100644 --- a/man/detrend.Rd +++ b/man/detrend.Rd @@ -4,34 +4,38 @@ \alias{detrend} \title{Remove offsets and linear trends} \usage{ -detrend(x, type = c("constant", "linear")[1]) +detrend(x, type = 0) } \arguments{ \item{x}{an object of class \code{idframe}} -\item{type}{trend type - "constant" or "linear". (Default: \code{"constant"})} +\item{type}{argument indicating the type of trend to be removed (Default=\code{0}) +\itemize{ + \item type=\code{0}: Subtracts mean value from each signal + \item type=\code{1}: Subtracts a linear trend (least-squres fit) + \item type=\code{trInfo} object: Subtracts a trend specified by the object +}} } \value{ -A list containing the following elements -\item{fitted.values}{\code{idframe} object with detrended variables} -\item{output_trend}{\code{list} containing trend fits for each output - variable} -\item{input_trend}{\code{list} containing trend fits for each input - variable} +A list containing two objects: the detrended data and the trend information } \description{ -Removes the offsets or linear trends in each of the input and output matrices. +Removes offsets or trends from data +} +\details{ +\code{R} by default doesn't allow return of multiple objects. The \code{\%=\%} +operator and \code{g} function in this package facillitate this behaviour. See +the examples section for more information. } \examples{ data(cstr) -fit <- detrend(cstr,type="linear") # remove linear trends -Zdetrend <- predict(fit) # get the detrended data - -demean <- detrend(cstr) # remove offsets -Zcent <- predict(demean) # get the centered data +datatrain <- dataSlice(cstr,end=4500) +datatest <- dataSlice(cstr,4501) +g(Ztrain,tr) \%=\% detrend(datatrain) # Remove means +g(Ztest) \%=\% detrend(datatest,tr) } \seealso{ -\code{\link{predict.detrend}}, \code{\link[stats]{lm}} +\code{\link[stats]{lm}} } diff --git a/man/predict.detrend.Rd b/man/predict.detrend.Rd deleted file mode 100644 index e91fb7e..0000000 --- a/man/predict.detrend.Rd +++ /dev/null @@ -1,30 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/preprocess.R -\name{predict.detrend} -\alias{predict.detrend} -\title{Detrend data based on linear trend fits} -\usage{ -\method{predict}{detrend}(model, newdata = NULL, ...) -} -\arguments{ -\item{model}{an object of class \code{detrend}} - -\item{newdata}{An optional idframe object in which to look for variables with -which to predict. If ommited, the original detrended idframe object is used} -} -\value{ -an \code{idframe} object -} -\description{ -Returns detrended \code{idframe} object based on linear trend fit -} -\examples{ -data(cstr) -train <- dataSlice(cstr,end=5000) -test <- dataSlice(cstr,start=6001) -fit <- detrend(train) -Ztrain <- predict(fit) -Ztest <- predict(fit,test) - -} - |