diff options
author | Suraj Yerramilli | 2015-03-22 20:29:25 +0530 |
---|---|---|
committer | Suraj Yerramilli | 2015-03-22 20:29:25 +0530 |
commit | 6f034a37fc7b35f94b9eaa1738bec4f5b2e2561b (patch) | |
tree | 1c5b723d88734b352b15ca87281fefde3c9b5bab | |
parent | bbb54481dee34289f132a6c8960da996525f2319 (diff) | |
download | SysID-R-code-6f034a37fc7b35f94b9eaa1738bec4f5b2e2561b.tar.gz SysID-R-code-6f034a37fc7b35f94b9eaa1738bec4f5b2e2561b.tar.bz2 SysID-R-code-6f034a37fc7b35f94b9eaa1738bec4f5b2e2561b.zip |
Added function to mean-center the data
-rw-r--r-- | R/demean.R | 70 | ||||
-rw-r--r-- | R/detrend.R | 2 |
2 files changed, 71 insertions, 1 deletions
diff --git a/R/demean.R b/R/demean.R new file mode 100644 index 0000000..9f1f436 --- /dev/null +++ b/R/demean.R @@ -0,0 +1,70 @@ +#' Mean-Center the data +#' +#' Mean Centers the input and output matrices. +#' +#' @param data an object of class \code{idframe} +#' +#' @return +#' A list containing the following elements +#' +#' \tabular{ll}{ +#' \code{fitted.values} \tab \code{idframe} object with mean-centered variables \cr +#' \code{output.mean} \tab \code{vector} containing means for each output variable \cr +#' \code{input.mean} \tab \code{vector} containing trend fits for each input variable +#' } +#' +#' @examples +#' data(cstr) +#' fit <- detrend.idframe(cstr) +#' cstr_detrend <- predict(fit) +#' +#' @seealso \code{\link{predict.demean}}, \code{\link[stats]{colMeans}} +#' @export +demean <- function(data){ + + data_demean <- data + output.mean <- colMeans(data$output) + input.mean <- colMeans(data$input) + + + data_demean$output <- data$output - output.mean + data_demean$input <- data$input - input + + est <- list(fitted.values=data_detrend,output.mean = output.mean, + input.mean = input.mean) + + class(est) <- "demean" + return(est) +} + +#' Predict the centered values +#' +#' Center an \code{idframe} object based on the training center means +#' +#' @param object an object of class \code{idframe} +#' @param newdata An optional idframe object in whic to look for variables with which +#' to predict. If ommited, the original detrended idframe object is used +#' +#' @return an \code{idframe} object +#' +#' @examples +#' ## Examples for train and test sets +#' data(cstr) +#' splitList <- dataPartition(cstr,p=0.6) +#' train <- splitList$estimation # training set +#' test <- splitList$validation # testing set +#' fit <- detrend.idframe(train) +#' train_detrend <- predict(fit) +#' test_detrend <- predict(fit,newdata=test) +#' @export +predict.demean <- function(object,newdata=NULL,...){ + + if(is.null(newdata)){ + data <- fitted(object) + } else{ + data <- newdata + data$output <- data$output - object$output.mean + data$input <- data$input - object$input.mean + } + return(data) +} diff --git a/R/detrend.R b/R/detrend.R index 7f03753..5c646a2 100644 --- a/R/detrend.R +++ b/R/detrend.R @@ -1,6 +1,6 @@ #' Remove linear trends #' -#' Removes the linear function from the input and output matrices. +#' Removes the linear trends in the input and output matrices. #' #' @param data an object of class \code{idframe} #' |