Perform k-fold cross validation with options for computationally efficient approximations for big data. Generalizes loocv() (leave-one-out cross validation) to leaving out k folds of (approximately) equal size instead of single observations.

# S3 method for class 'ssn_lm'
kcv(
  object,
  k = 5,
  cv_predict = FALSE,
  se.fit = FALSE,
  local,
  interval = c("none", "prediction"),
  level = 0.95,
  folds_index,
  ...
)

# S3 method for class 'ssn_glm'
kcv(
  object,
  k = 5,
  cv_predict = FALSE,
  type = c("link", "response"),
  se.fit = FALSE,
  delta = FALSE,
  local,
  folds_index,
  ...
)

Arguments

object

A fitted model object from ssn_lm() or ssn_glm().

k

The number of folds. Must be a whole number at least 2 and no more than the sample size (the number of non-missing observations in object). The default is 5. If k equals the sample size, kcv() is equivalent to (and calls) loocv(). Ignored when folds_index is supplied.

cv_predict

A logical indicating whether the k-fold cross validation fitted values should be returned. Defaults to FALSE. If object is from ssn_glm(), the fitted values returned are on the link scale.

se.fit

A logical indicating whether the k-fold cross validation prediction standard errors should be returned. Defaults to FALSE. If object is from ssn_glm(), the standard errors correspond to the fitted values returned on the link scale.

local

A list or logical. If a list, specific list elements described in predict.SSN2() control the big data approximation behavior. If a logical, TRUE chooses default list elements for the list version of local as specified in predict.SSN2(). Defaults to FALSE, which performs exact computations.

interval

For ssn_lm() objects, whether to append empirical coverage at level. The default, "none", retains the legacy 80, 90, and 95 percent coverage columns.

level

Prediction interval level used when interval = "prediction". The default is 0.95.

folds_index

An optional vector, the same length as the number of non-missing observations in object and in the same order, assigning each observation to a fold (at least two distinct values required). When supplied, this fold assignment is used as-is, implying k is ignored and no random fold assignment is performed. The default is NULL, which randomly assigns k (approximately) equally sized folds.

...

Other arguments. Not used (needed for generic consistency).

type

The scale (response or link) of predictions obtained when cv_predict = TRUE and using ssn_lm() or ssn_glm objects.

delta

A logical indicating whether to return delta method standard errors on the response scale when se.fit = TRUE and type = "response". The default is FALSE.

Value

If cv_predict = FALSE and se.fit = FALSE, a fit statistics tibble (with bias, MSPE, RMSPE, and cor2; see Details). If cv_predict = TRUE or se.fit = TRUE, a list with elements: stats, a fit statistics tibble (with bias, MSPE, RMSPE, and cor2; see Details); cv_predict, a numeric vector with k-fold cross validation predictions for each observation (if cv_predict = TRUE); and se.fit, a numeric vector with k-fold cross validation prediction standard errors for each observation (if se.fit = TRUE). When object is from ssn_lm() and interval = "prediction", the fit statistics tibble also has cover.XX columns (e.g. cover.95 for level = 0.95; see Details).

Details

Observations are randomly partitioned into k folds of (approximately) equal size. Each fold is held out from the data set in turn and the remaining data are used to make predictions for the held-out fold. This is compared to the true values of the held-out observations and several fit statistics are (sometimes optionally) computed: bias, mean-squared-prediction error (MSPE), root-mean-squared-prediction error (RMSPE), and the squared correlation (cor2) between the observed data and k-fold cross validation predictions (regarded as a prediction version of r-squared appropriate for comparing across spatial and nonspatial models), , and prediction interval coverage (cover.XX). Generally, bias should be near zero and prediction interval coverage at the intended level for well-fitting models. The lower the MSPE and RMSPE, the better the model fit (according to the k-fold cross validation criterion). The higher the cor2, the better the model fit (according to the k-fold cross validation criterion). cor2 and cover.XX are not returned when object was fit using ssn_glm() because we do not observe the underlying latent mean.

When object is from ssn_lm(), setting interval = "prediction" additionally reports the empirical coverage of the level (e.g. 95\ the proportion of held-out observations whose true value falls within fit +/- qnorm(1 - (1 - level) / 2) * se.fit (the same normal-quantile interval spmodel::predict.spmodel() uses by default). This is only available for ssn_lm() objects, since ssn_glm() have no observed-scale latent mean to compare against.

Examples

# Copy the mf04p .ssn data to a local directory and read it into R
# When modeling with your .ssn object, you will load it using the relevant
# path to the .ssn data on your machine
copy_lsn_to_temp()
temp_path <- paste0(tempdir(), "/MiddleFork04.ssn")
mf <- ssn_import(temp_path, overwrite = TRUE)
fit <- ssn_lm(Summer_mn ~ ELEV_DEM, mf,
  tailup_type = "exponential", additive = "afvArea"
)
set.seed(1)
kcv(fit, k = 3)
#> Error in kcv(fit, k = 3): could not find function "kcv"