I am struggling with a problem on how to subset xts objects stored within a list. The subsetting shall happen based on row indexes. The background is that I want to split the objects randomly 80/20 into training and test set. Here is an example:
library(xts)
# Create a sample list with dummy data
series <- list(
A=xts(rnorm(n=200), as.Date("2015-01-01")+1:200),
B=xts(rnorm(n=50), as.Date("2015-04-01")+1:50)
)
Note: the length of these xts objects differ on purpose.
The trainIndex
is a list that contains row numbers that split each xts object on 80/20 basis as per the createDataPartition
function from the caret
package:
# create am index of row numbers for splitting the dataset
library(caret)
trainIndex <- lapply(series, function(x) {createDataPartition(x, p=0.8)})
And this is what I was expecting to work:
series.test <- lapply(series, function(x) x[trainIndex,])
which it didn't.
This works for a 'static' vector (as per here):
trainIndex.simple <- seq(1,50,by=3)
lapply(series, function(x) x[trainIndex.simple,])
And this works on one list element
series$A[trainIndex$A[[1]],]
But how to apply the list of row indices on a list of xts objects? This post might be helpful somehow, but I couldn't translate it to my problem...
Any hint is very much appreciated!