Save log likelihoods of k-fold cross-validation for sdmTMB models

sdmTMB_cv(formula, data, spde, time = NULL, k_folds = 10, fold_ids = NULL, ...)

Arguments

formula

Model formula.

data

A data frame.

spde

Output from make_mesh().

time

The name of the time column. Leave as NULL if this is only spatial data.

k_folds

Number of folds.

fold_ids

Optional input name of column containing user fold ids.

...

All other arguments required to run sdmTMB() model with the exception of weights, which are used to define the folds.

Examples

spde <- make_mesh(pcod, c("X", "Y"), cutoff = 25) # library(future) # for parallel processing # plan(multisession) # for parallel processing m_cv <- sdmTMB_cv( density ~ 0 + depth_scaled + depth_scaled2, data = pcod, spde = spde, family = tweedie(link = "log"), k_folds = 2 ) m_cv$fold_loglik
#> [1] -3318.609 -3436.644
m_cv$sum_loglik
#> [1] -6755.254
head(m_cv$data)
#> # A tibble: 6 x 15 #> year X Y depth density present lat lon depth_mean depth_sd #> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 2003 446. 5793. 201 113. 1 52.3 -130. 5.16 0.445 #> 2 2003 446. 5800. 212 41.7 1 52.3 -130. 5.16 0.445 #> 3 2003 449. 5802. 220 0 0 52.4 -130. 5.16 0.445 #> 4 2003 437. 5802. 197 15.7 1 52.4 -130. 5.16 0.445 #> 5 2003 421. 5771. 256 0 0 52.1 -130. 5.16 0.445 #> 6 2003 418. 5772. 293 0 0 52.1 -130. 5.16 0.445 #> # … with 5 more variables: depth_scaled <dbl>, depth_scaled2 <dbl>, #> # cv_fold <int>, cv_predicted <dbl>, cv_loglik <dbl>
m_cv$models[[1]]
#> Spatial model fit by ML ['sdmTMB'] #> Formula: formula #> Time column: time #> SPDE: spde #> Data: data #> Family: tweedie(link = 'log') #> coef.est coef.se #> depth_scaled -1.58 0.18 #> depth_scaled2 -1.48 0.11 #> #> Matern range parameter: 150.77 #> Dispersion parameter: 13.63 #> Spatial SD (sigma_O): 2.48 #> Spatiotemporal SD (sigma_E): not estimated #> ML criterion at convergence: 3291.025 #> #> See ?tidy.sdmTMB to extract these values as a data frame.
m_cv$max_gradients
#> [1] 0.0004420159 0.0001048449