cv.modList | R Documentation |
A cv()
method for an object of class "modlist"
,
created by the models()
function. This cv()
method simplifies
the process of cross-validating several models on the same set of CV folds
and may also be used for meta CV, where CV is used to select one from among
several models. models()
performs some
"sanity" checks, warning if the models are of different classes, and
reporting an error if they are fit to apparently different data sets or
different response variables.
## S3 method for class 'modList'
cv(
model,
data,
criterion = mse,
k,
reps = 1L,
seed,
quietly = TRUE,
meta = FALSE,
...
)
models(...)
## S3 method for class 'cvModList'
print(x, ...)
## S3 method for class 'cvModList'
summary(object, ...)
## S3 method for class 'cvModList'
plot(
x,
y,
spread = c("range", "sd"),
confint = TRUE,
xlab = "",
ylab,
main,
axis.args = list(labels = names(x), las = 3L),
col = palette()[2L],
lwd = 2L,
grid = TRUE,
...
)
## S3 method for class 'cvModList'
as.data.frame(x, row.names = NULL, optional = TRUE, ...)
model |
a list of regression model objects,
created by |
data |
(required) the data set to which the models were fit. |
criterion |
the CV criterion ("cost" or lack-of-fit) function, defaults to
|
k |
the number of CV folds; may be omitted, in which case the value
will depend on the default for the |
reps |
number of replications of CV for each model (default is 1). |
seed |
(optional) seed for R's pseudo-random-number generator, to be used to create the same set of CV folds for all of the models; if omitted, a seed will be randomly generated and saved. Not used for leave-one-out CV. |
quietly |
if |
meta |
if |
... |
for For For the For the |
x |
an object of class |
object |
an object to summarize. |
y |
the name of the element in each |
spread |
if |
confint |
if |
xlab |
label for the x-axis (defaults to blank). |
ylab |
label for the y-axis (if missing, a label is constructed). |
main |
main title for the graph (if missing, a label is constructed). |
axis.args |
a list of arguments for the |
col |
color for the line and points, defaults to the second
element of the color palette; see |
lwd |
line width for the line (defaults to 2). |
grid |
if |
row.names |
optional row names for the result,
defaults to |
optional |
to match the |
models()
returns a "modList"
object, the
cv()
method for which returns a "cvModList"
object,
or, when meta=TRUE
, an object of class c("cvSelect", "cv")
.
cv(modList)
: cv()
method for "modList"
objects.
models()
: create a list of models.
print(cvModList)
: print()
method for "cvModList"
objects.
summary(cvModList)
: summary()
method for "cvModList"
objects.
plot(cvModList)
: plot()
method for "cvModList"
objects.
as.data.frame(cvModList)
: as.data.frame()
method for
"cvModList"
objects.
cv
, cv.merMod
,
selectModelList
.
if (requireNamespace("carData", quietly=TRUE)){
withAutoprint({
data("Duncan", package="carData")
m1 <- lm(prestige ~ income + education, data=Duncan)
m2 <- lm(prestige ~ income + education + type, data=Duncan)
m3 <- lm(prestige ~ (income + education)*type, data=Duncan)
(cv.models <- cv(models(m1=m1, m2=m2, m3=m3),
data=Duncan, seed=7949, reps=5))
D.cv.models <- as.data.frame(cv.models)
head(D.cv.models)
summary(D.cv.models, criterion ~ model + rep, include="folds")
plot(cv.models)
(cv.models.ci <- cv(models(m1=m1, m2=m2, m3=m3),
data=Duncan, seed=5963, confint=TRUE, level=0.50))
# nb: n too small for accurate CIs
plot(cv.models.ci)
(cv.models.meta <- cv(models(m1=m1, m2=m2, m3=m3),
data=Duncan, seed=5963,
meta=TRUE, save.model=TRUE))
cvInfo(cv.models.meta, "selected model")
})
} else {
cat("install the 'carData' package to run these examples\n")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.