Reorganize code - unfinished: some functions not exported yet
[agghoo.git] / R / utils.R
diff --git a/R/utils.R b/R/utils.R
new file mode 100644 (file)
index 0000000..fa3a9df
--- /dev/null
+++ b/R/utils.R
@@ -0,0 +1,28 @@
+get_testIndices <- function(n, CV, v, shuffle_inds) {
+  if (CV$type == "vfold") {
+    # Slice indices (optionnally shuffled)
+    first_index = round((v-1) * n / CV$V) + 1
+    last_index = round(v * n / CV$V)
+    test_indices = first_index:last_index
+    if (!is.null(shuffle_inds))
+      test_indices <- shuffle_inds[test_indices]
+  }
+  else
+    # Monte-Carlo cross-validation
+    test_indices = sample(n, round(n * CV$test_size))
+  test_indices
+}
+
+splitTrainTest <- function(data, target, testIdx) {
+  dataTrain <- data[-testIdx,]
+  targetTrain <- target[-testIdx]
+  dataTest <- data[testIdx,]
+  targetTest <- target[testIdx]
+  # [HACK] R will cast 1-dim matrices into vectors:
+  if (!is.matrix(dataTrain) && !is.data.frame(dataTrain))
+    dataTrain <- as.matrix(dataTrain)
+  if (!is.matrix(dataTest) && !is.data.frame(dataTest))
+    dataTest <- as.matrix(dataTest)
+  list(dataTrain=dataTrain, targetTrain=targetTrain,
+       dataTest=dataTest, targetTest=targetTest)
+}