crossval {bootstrap}R Documentation

K-fold Cross-Validation


See Efron and Tibshirani (1993) for details on this function.


   crossval(x, y,, theta.predict, ..., ngroup=n)



a matrix containing the predictor (regressor) values. Each row corresponds to an observation.


a vector containing the response values

function to be cross-validated. Takes x and y as an argument. See example below.


function producing predicted values for Arguments are a matrix x of predictors and fit object produced by See example below.


any additional arguments to be passed to


optional argument specifying the number of groups formed . Default is ngroup=sample size, corresponding to leave-one out cross-validation.


list with the following components

The cross-validated fit for each observation. The numbers 1 to n (the sample size) are partitioned into ngroup mutually disjoint groups of size "leave.out". leave.out, the number of observations in each group, is the integer part of n/ngroup. The groups are chosen at random if ngroup < n. (If n/leave.out is not an integer, the last group will contain > leave.out observations). Then is applied with the kth group of observations deleted, for k=1, 2, ngroup. Finally, the fitted value is computed for the kth group using theta.predict.


The number of groups


The number of observations in each group


A list of length ngroup containing the indices of the observations in each group. Only returned if leave.out > 1.


The deparsed call


Stone, M. (1974). Cross-validation choice and assessment of statistical predictions. Journal of the Royal Statistical Society, B-36, 111–147.

Efron, B. and Tibshirani, R. (1993) An Introduction to the Bootstrap. Chapman and Hall, New York, London.


# cross-validation of least squares regression
# note that crossval is not very efficient, and being a
#  general purpose function, it does not use the
# Sherman-Morrison identity for this special case
   x <- rnorm(85)  
   y <- 2*x +.5*rnorm(85)                 <- function(x,y){lsfit(x,y)}
   theta.predict <- function(fit,x){
   results <- crossval(x,y,,theta.predict,ngroup=6)  

[Package bootstrap version 2019.6 Index]