diff options
author | Suraj Yerramilli | 2015-04-01 22:42:30 +0530 |
---|---|---|
committer | Suraj Yerramilli | 2015-04-01 22:42:30 +0530 |
commit | 05eddf7b3f3ce6af739e56ea484aa1a3846860b1 (patch) | |
tree | eb5dcbf9c2507cabf6a3fd0ba0b2091117bafddf /R | |
parent | 4edb150aa3f37f39678603bd7340e7b8393b8a70 (diff) | |
download | SysID-R-code-05eddf7b3f3ce6af739e56ea484aa1a3846860b1.tar.gz SysID-R-code-05eddf7b3f3ce6af739e56ea484aa1a3846860b1.tar.bz2 SysID-R-code-05eddf7b3f3ce6af739e56ea484aa1a3846860b1.zip |
polished the idframe slicing function
Diffstat (limited to 'R')
-rw-r--r-- | R/partition.R | 62 |
1 files changed, 19 insertions, 43 deletions
diff --git a/R/partition.R b/R/partition.R index 7c0af2b..7e1cc01 100644 --- a/R/partition.R +++ b/R/partition.R @@ -1,60 +1,36 @@ #' Subset an idframe data #' -#' Subsetting method for datas of class \code{idframe} +#' Subsetting method for objects of class \code{idframe} #' #' @param data an object of class \code{idframe} -#' @param indices the indices that need to be subsetted +#' @param start the start #' @export -dataSlice <- function(data,indices){ +dataSlice <- function(data,start=NULL,end=NULL,freq=NULL){ # check if the class is correct if(class(data)!='idframe') stop("Not an idframe data") - if(!all(indices %in% seq(to=dim(data$output)[1],by=1))) - stop("Invalid indices") + nin <- dim(data$input)[2]; nout <- dim(data$output)[2] + dataMatrix <- cbind(data$input,data$output) + if(type=="freq"){ + dataMatrix <- cbind(dataMatrix,data$frequencies) + } else { + timeSeq <- seq(from=data$t.start,to=data$t.end,by=data$Ts) + dataMatrix <- cbind(dataMatrix,timeSeq) + } - trim <- data - trim$output <- trim$output[indices,,drop=F] - trim$input <- trim$input[indices,,drop=F] + l <- as.list(dataMatrix) + trimData <- as.data.frame(sapply(l,window,start=start,end=end,deltat=freq)) + + trim <- idframe(output=trimData[,(nin+1):(nin+nout+1)],input=trimData[,1:nin], + type=data$type,Ts=data$tTs,tUnit=data$tUnit) if(trim$type=="freq"){ - trim$frequncies <- trim$frequencies[indices] + trim$frequncies <- trimData[,ncol(trimData)] } else { - trim$t.start <- trim$t.start + trim$Ts*(indices[1]-1) - trim$t.end <- trim$t.start + trim$Ts*(length(indices)-1) + trim$t.start <- trimData[1,ncol(trimData)] + trim$t.end <- trimData[nrow(trimData),ncol(trimData)] } return(trim) -} - -#' Split data into training and validation sets -#' -#' The function splits the data into training and validation sets and returns them bundled -#' as a list. The size of the sets are determined by the parameter \code{p}. -#' -#' @param data an object of class \code{idframe} -#' @param p the percentage of the data that goes to training (Default : \code{0.6}) -#' @return list containing estimation and validation idframe datas -#' -#' @examples -#' data(cstr) -#' splitList <- dataPartition(cstr,p=0.6) -#' train <- splitList$estimation # training set -#' test <- splitList$validation # testing set -#' -#' @export -dataPartition <- function(data,p=0.6){ - # check if the class is correct - if(class(data)!='idframe') - stop("Not an idframe data") - - index <- seq_along(data$output[,1]) - - trainIndex <- index[1:round(p*length(index))] - testIndex <- index[!(index %in% trainIndex)] - - train <- dataSlice(data,trainIndex) - test <- dataSlice(data,testIndex) - - return(list(estimation=train,validation=test)) }
\ No newline at end of file |