From 05eddf7b3f3ce6af739e56ea484aa1a3846860b1 Mon Sep 17 00:00:00 2001 From: Suraj Yerramilli Date: Wed, 1 Apr 2015 22:42:30 +0530 Subject: polished the idframe slicing function --- R/partition.R | 62 ++++++++++++++++++----------------------------------------- 1 file changed, 19 insertions(+), 43 deletions(-) (limited to 'R') 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 -- cgit