cvCompute {cv} | R Documentation |
Utility Functions for the cv Package
Description
These functions are primarily useful for writing methods for the
cv()
generic function. They are used internally in the package
and can also be used for extensions (see the vignette "Extending the cv package,
vignette("cv-extend", package="cv")
).
Usage
cvCompute(
model,
data = insight::get_data(model),
criterion = mse,
criterion.name,
k = 10L,
reps = 1L,
seed,
details = k <= 10L,
confint,
level = 0.95,
method = NULL,
ncores = 1L,
type = "response",
start = FALSE,
f,
fPara = f,
locals = list(),
model.function = NULL,
model.function.name = NULL,
...
)
cvMixed(
model,
package,
data = insight::get_data(model),
criterion = mse,
criterion.name,
k,
reps = 1L,
confint,
level = 0.95,
seed,
details,
ncores = 1L,
clusterVariables,
predict.clusters.args = list(object = model, newdata = data),
predict.cases.args = list(object = model, newdata = data),
blups,
fixed.effects,
...
)
cvSelect(
procedure,
data,
criterion = mse,
criterion.name,
model,
y.expression,
k = 10L,
confint = n >= 400,
level = 0.95,
reps = 1L,
save.coef,
details = k <= 10L,
save.model = FALSE,
seed,
ncores = 1L,
...
)
folds(n, k)
fold(folds, i, ...)
## S3 method for class 'folds'
fold(folds, i, ...)
## S3 method for class 'folds'
print(x, ...)
GetResponse(model, ...)
## Default S3 method:
GetResponse(model, ...)
## S3 method for class 'merMod'
GetResponse(model, ...)
## S3 method for class 'lme'
GetResponse(model, ...)
## S3 method for class 'glmmTMB'
GetResponse(model, ...)
## S3 method for class 'modList'
GetResponse(model, ...)
Arguments
model |
a regression model object. |
data |
data frame to which the model was fit (not usually necessary,
except for |
criterion |
cross-validation criterion ("cost" or lack-of-fit) function of form |
criterion.name |
a character string giving the name of the CV criterion function
in the returned |
k |
perform k-fold cross-validation (default is |
reps |
number of times to replicate k-fold CV (default is |
seed |
for R's random number generator; optional, if not supplied a random seed will be selected and saved; not needed for n-fold cross-validation. |
details |
if |
confint |
if |
level |
confidence level (default |
method |
computational method to apply; use by some |
ncores |
number of cores to use for parallel computations
(default is |
type |
used by some |
start |
used by some |
f |
function to be called by |
fPara |
function to be called by |
locals |
a named list of objects that are required in the local environment
of |
model.function |
a regression function, typically for a new |
model.function.name |
the quoted name of the regression function, e.g.,
|
... |
to match generic; passed to |
package |
the name of the package in which mixed-modeling function (or functions) employed resides; used to get the namespace of the package. |
clusterVariables |
a character vector of names of the variables defining clusters for a mixed model with nested or crossed random effects; if missing, cross-validation is performed for individual cases rather than for clusters |
predict.clusters.args |
a list of arguments to be used to predict
the whole data set from a mixed model when performing CV on clusters;
the first two elements should be
|
predict.cases.args |
a list of arguments to be used to predict
the whole data set from a mixed model when performing CV on cases;
the first two elements should be
|
blups |
a function to be used to compute BLUPs for
case-based CV when |
fixed.effects |
a function to be used to compute fixed-effect
coefficients for cluster-based CV when |
procedure |
a model-selection procedure function (see Details). |
y.expression |
normally the response variable is found from the
|
save.coef |
save the coefficients from the selected models? Deprecated
in favor of the |
save.model |
save the model that's selected using the full data set. |
n |
number of cases, for constructed folds. |
folds |
an object of class |
i |
a fold number for an object of class |
x |
a |
Value
The utility functions return various kinds of objects:
-
cvCompute()
returns an object of class"cv"
, with the CV criterion ("CV crit"
), the bias-adjusted CV criterion ("adj CV crit"
), the criterion for the model applied to the full data ("full crit"
), the confidence interval and level for the bias-adjusted CV criterion ("confint"
), the number of folds ("k"
), and the seed for R's random-number generator ("seed"
). Ifdetails=TRUE
, then the returned object will also include a"details"
component, which is a list of two elements:"criterion"
, containing the CV criterion computed for the cases in each fold; and"coefficients"
, regression coefficients computed for the model with each fold deleted. Somecv()
methods callingcvCompute()
may return a subset of these components and may add additional information. Ifreps
>1
, then an object of class"cvList"
is returned, which is literally a list of"cv"
objects. -
cvMixed()
also returns an object of class"cv"
or"cvList"
. -
cvSelect
returns an object of class"cvSelect"
inheriting from"cv"
, or an object of class"cvSelectList"
inheriting from"cvList"
. -
folds()
returns an object of class folds, for which there arefold()
andprint()
methods. -
GetResponse()
returns the (numeric) response variable from the model.The supplied
default
method returns themodel$y
component of the model object, or, ifmodel
is an S4 object, the result returned by theget_response()
function in the insight package. If this result isNULL
, the result ofmodel.response(model.frame(model))
is returned, checking in any case whether the result is a numeric vector.There are also
"lme"
,"merMod"
and"glmmTMB"
methods that convert factor responses to numeric 0/1 responses, as would be appropriate for a generalized linear mixed model with a binary response.
Functions
-
cvCompute()
: used internally bycv()
methods (not for direct use); exported to support newcv()
methods. -
cvMixed()
: used internally bycv()
methods for mixed-effect models (not for direct use); exported to support newcv()
methods. -
cvSelect()
: used internally bycv()
methods for cross-validating a model-selection procedure; may also be called directly for this purpose, but use viacv()
is preferred.cvSelect()
is exported primarily to support new model-selection procedures. -
folds()
: used internally bycv()
methods (not for direct use). -
fold()
: to extract a fold from a"folds"
object. -
fold(folds)
:fold()
method for"folds"
objects. -
print(folds)
:print()
method for"folds"
objects. -
GetResponse()
: function to return the response variable from a regression model. -
GetResponse(default)
: default method. -
GetResponse(merMod)
:"merMod"
method. -
GetResponse(lme)
:"lme"
method. -
GetResponse(glmmTMB)
:"glmmTMB"
method. -
GetResponse(modList)
:"modList"
method.
See Also
Examples
fit <- lm(mpg ~ gear, mtcars)
GetResponse(fit)
set.seed(123)
(ffs <- folds(n=22, k=5))
fold(ffs, 2)