Perform leave-one-out cross validation with options for computationally efficient approximations for big data.

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

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

Arguments

object

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

cv_predict

A logical indicating whether the leave-one-out 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 leave-one-out 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

Whether to report coverage at a requested prediction interval level. "none" preserves the legacy coverage columns; "prediction" additionally reports coverage at level. Only available for ssn_lm() objects.

level

The requested prediction interval coverage level. Defaults to 0.95.

...

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

type

The scale (response or link) of predictions obtained when cv_predict = TRUE and using 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 leave-one-out predictions for each observation (if cv_predict = TRUE); and se.fit, a numeric vector with leave-one-out prediction standard errors for each observation (if se.fit = TRUE). When object is from splm() or spautor() and interval = "prediction", the fit statistics tibble also has cover.XX columns (e.g. cover.95 for level = 0.95; see Details).

Details

Each observation is held-out from the data set and the remaining data are used to make a prediction for the held-out observation. This is compared to the true value of the observation 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 leave-one-out 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 leave-out-out criterion). The higher the cor2, the better the model fit (according to the leave-out-out criterion). cor2 and cover.XX are not returned when object was fit using ssn_glm() because we do not observe the underlying latent mean.

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")
mf04p <- ssn_import(temp_path, overwrite = TRUE)

ssn_mod <- ssn_lm(
  formula = Summer_mn ~ ELEV_DEM,
  ssn.object = mf04p,
  tailup_type = "exponential",
  additive = "afvArea"
)
loocv(ssn_mod)
#> # A tibble: 1 × 10
#>     bias std.bias  MSPE RMSPE std.MSPE   RAV  cor2 cover.80 cover.90 cover.95
#>    <dbl>    <dbl> <dbl> <dbl>    <dbl> <dbl> <dbl>    <dbl>    <dbl>    <dbl>
#> 1 0.0449   0.0456 0.273 0.522     1.23 0.418 0.930      0.8    0.889    0.911