Learn R Programming

rare (version 0.1.2)

rarefit.cv: Perform K-fold cross validation

Description

The function does K-fold cross validaton (CV) to choose an optimal pair of (lambda, alpha) on which the model performs best according to the chosen error metric: mean squared error or mean absolute error.

Usage

rarefit.cv(fitObj, y, X, errtype = "mean-squared-error", nfolds = 5,
  ...)

Value

folds

A length-nfolds list with the kth element being elements in the kth fold.

errs

A nlam-by-nalpha-by-nfolds 3-dimensional array of errors. errs[i,j,k] is error incurred in using lambda[i] and alpha[j] on the kth fold.

m

A nlam-by-nalpha matrix for storing CV error (i.e., mean error across folds). m[i,j] is CV error incurred in using lambda[i] and alpha[j].

se

A nlam-by-nalpha matrix for storing standard error across folds. se[i,j] is standard error incurred in using lambda[i] and alpha[j].

ibest

Indices of pair of (lambda, alpha) minimizing CV error.

lambda.best

Value of lambda minimizing CV error.

alpha.best

Value of alpha minimizing CV error.

Arguments

fitObj

Output of rarefit

y

Response variable.

X

nobs-by-nvars input matrix: each row is an observation vector and each column stores a count covariate.

errtype

Type of error metric used in cross validation. Available choices are mean-squared-error (default) and mean-absolute-error.

nfolds

Number of folds (default is 5)

...

Other arguments that can be passed to rarefit

See Also

rarefit, rarefit.predict

Examples

Run this code
if (FALSE) {
# See vignette for more details.
set.seed(100)
ts <- sample(1:length(data.rating), 400) # Train set indices
# Fit the model on train set
ourfit <- rarefit(y = data.rating[ts], X = data.dtm[ts, ], hc = data.hc, lam.min.ratio = 1e-6,
                  nlam = 20, nalpha = 10, rho = 0.01, eps1 = 1e-5, eps2 = 1e-5, maxite = 1e4)
# Cross validation
ourfit.cv <- rarefit.cv(ourfit, y = data.rating[ts], X = data.dtm[ts, ],
                        rho = 0.01, eps1 = 1e-5, eps2 = 1e-5, maxite = 1e4)
}

Run the code above in your browser using DataLab