Computes an R^2 value for predicting an outcome measure using a k-fold cross-validation scheme.

crossvalidatedR2(x, y, ngroups=5, covariates=NA, fast=F)

Arguments

x

Input predictor matrix.

y

Target dependent variable.

ngroups

Number of cross-validation folds to use or the fold labels themselves, equal to the length of y. e.g. c(1,1,1,2,2,2...)

covariates

Covariate predictors.

fast

Use low-level lm.fit instead of lm. Much faster, but less error checking.

Value

Matrix of size ngroups by ncol(x), which each row corresponding to one fold and the columns corresponding to the R2 values for each predictor.

Author

Brian B Avants, Benjamin M. Kandel

Examples

set.seed(300)
ncol <- 30
nrow <- 20
covariate <- sin((1:nrow) * 2 * pi / nrow)
x <- matrix(rep(NA, nrow * ncol), nrow = nrow)
xsig <- seq(0, 1, length.out = nrow)
y <- xsig + covariate + rnorm(nrow, sd = 0.5)
for (i in 1:ncol) {
  x[, i] <- xsig + rnorm(nrow, sd = i / ncol)
}
r2 <- crossvalidatedR2(x, y, covariates = covariate)