folds: Make folds for k-fold partitioning

View source: R/kfold.R

foldsR Documentation

Make folds for k-fold partitioning

Description

k-fold partitioning of a data set for model testing purposes. Each record in a matrix (or similar data structure) is randomly assigned to a group. Group numbers are between 1 and k. The function assures that each fold has the same size (or as close to that as possible).

Usage

folds(x, k=5, by)

Arguments

x

a vector, matrix, data.frame, or Spatial object

k

number of groups

by

Optional argument. A vector or factor with sub-groups (e.g. species). Its length should be the same as the number of records in x

Value

a vector with group assignments

Author(s)

Robert J. Hijmans

Examples


library(disdat)
train <- disPo("NSW")
## a single species
srsp1 <- subset(train, spid=="nsw01")
folds(srsp1, k = 5)

## all species
k = folds(train, k=5, by=train$spid)

## each group has the same number of records 
##(except for adjustments if the number of records 
## divided by k is not an integer) 

table(k[train$spid=="nsw01"])

rspatial/predicts documentation built on July 11, 2024, 4:35 p.m.