Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created December 2, 2011 01:15
Show Gist options
  • Save ramhiser/1421185 to your computer and use it in GitHub Desktop.
Save ramhiser/1421185 to your computer and use it in GitHub Desktop.
Randomly subsample a matrix or data frame (Useful with the 'boot' package in R)
#' Randomly subsample a matrix or data frame.
#'
#' To randomly subsample, we are using the parametric option in the boot function
#' within the boot package. The 'parametric' option requires the specification of
#' a 'ran.gen' function to generate observations based on the original data and a list of
#' maximum likelihood estimators (MLE). We utilize this method, but instead of the
#' MLE, we instead pass the subsample_size.
#'
#' As noted in the boot documentation: Use of sim = "parametric" with a suitable ran.gen
#' allows the user to implement any types of nonparametric resampling which are not
#' supported directly.
#'
#' TODO: If 'x' and 'subsample_size' break, it may be necessary to use
#' 'data' and 'mle' instead.
#'
#' @param x the data matrix
#' @param subsample_size the number of observations (rows) to select at random from x.
#' @return a random subsample of the data matrix, x.
#' @export
#' @examples
#' TODO
boot_subsample <- function(x, subsample_size) {
x[sample(x = seq_len(nrow(x)), size = subsample_size), ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment