View source: R/cv-mixed-models.R
cv.merMod | R Documentation |
cv()
methods for mixed-effect models of class "merMod"
, fit
by the lmer()
and glmer()
functions
in the lme4 package; for models of class "lme"
fit by the lme()
function in the nlme
package; and for models of class "glmmTMB"
fit by the
glmmTMB()
function in the glmmTMB package.
## S3 method for class 'merMod'
cv(
model,
data = insight::get_data(model),
criterion = mse,
k = NULL,
reps = 1L,
seed,
details = NULL,
ncores = 1L,
clusterVariables,
...
)
## S3 method for class 'lme'
cv(
model,
data = insight::get_data(model),
criterion = mse,
k = NULL,
reps = 1L,
seed,
details = NULL,
ncores = 1L,
clusterVariables,
...
)
## S3 method for class 'glmmTMB'
cv(
model,
data = insight::get_data(model),
criterion = mse,
k = NULL,
reps = 1L,
seed,
details = NULL,
ncores = 1L,
clusterVariables,
...
)
model |
a mixed-effects model object for which a |
data |
data frame to which the model was fit (not usually necessary). |
criterion |
cross-validation ("cost" or lack-of-fit) criterion function of form |
k |
perform k-fold cross-validation; |
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 |
ncores |
number of cores to use for parallel computations
(default is |
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. |
... |
for |
For mixed-effects models, cross-validation can be done by "clusters" or by individual observations. If the former, predictions are based only on fixed effects; if the latter, predictions include the random effects (i.e., are the best linear unbiased predictors or "BLUPS").
The methods cv.merMod()
, cv.lme()
, and cv.glmmTMB()
,
return objects of class "cv"
, or,
if reps > 1
, of class "cvList"
(see cv()
).
cv(merMod)
: cv()
method for lmer()
and
glmer()
models from the lme4 package.
cv(lme)
: cv()
method for lme()
models from the nlme package.
cv(glmmTMB)
: cv()
method for glmmTMB()
models from the glmmTMB package.
cv
, lmer
, glmer
,
lme
, glmmTMB
library("lme4")
# from ?lmer:
(fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy))
summary(cv(fm1, clusterVariables="Subject")) # LOO CV of clusters
summary(cv(fm1, seed=447)) # 10-fold CV of cases
summary(cv(fm1, clusterVariables="Subject", k=5,
seed=834, reps=3)) # 5-fold CV of clusters, repeated 3 times
library("nlme")
# from ?lme
(fm2 <- lme(distance ~ age + Sex, data = Orthodont,
random = ~ 1))
summary(cv(fm2)) # LOO CV of cases
summary(cv(fm2, clusterVariables="Subject",
k=5, seed=321)) # 5-fold CV of clusters
library("glmmTMB")
# from ?glmmTMB
(m1 <- glmmTMB(count ~ mined + (1|site),
zi=~mined,
family=poisson, data=Salamanders))
summary(cv(m1, seed=97816, k=5,
clusterVariables="site")) # 5-fold CV of clusters
summary(cv(m1, seed=34506, k=5)) # 5-fold CV of cases
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.