You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'waywiser' seems potentially useful, and I put together the example below (based on their vignette). The key steps here are: 1. Users need to select some kind of test/train split
we already have this implemented in sdmTMB_cv
2. pass the object to waywiser
I wrote a little function below that automates most of the sf stuff for this
library(sdmTMB)
library(waywiser)
library(sf)
# Build a mesh to implement the SPDE approach:
# Fit a Tweedie spatial random field GLMM with a smoother for depth:
data("pcod_2011")
mesh <- make_mesh(pcod, c("X", "Y"), cutoff = 25)
# use random folds (n = 8) but can modify
m_cv <- sdmTMB_cv(
density ~ 0 + depth_scaled + depth_scaled2,
data = pcod, mesh = mesh,
family = tweedie()
)
# function to take a sdmTMB_cv object and convert to sf for waywiser
cv_to_waywiser <- function(object, ll_names = c("lon", "lat")) {
formulas <- formula(object$models[[1]])
resp_var <- all.vars(formulas)[1] # non-delta
if(class(formulas) == "list") resp_var <- all.vars(formulas[[1]])[1] # delta
point_df <- data.frame(
truth = object$data[[resp_var]],
estimate = object$data$cv_predicted,
X = object$data$X,
Y = object$data$Y
)
# convert to sf
points <- sf::st_as_sf(point_df,
coords = object$models[[1]]$spde$xy_cols,
crs = get_crs(pcod_2011[, ll_names],
ll_names = ll_names))
return(points)
}
pcod_points <- cv_to_waywiser(m_cv, ll_names = c("lon","lat"))
pcod_multi_scale <- ww_multi_scale(
pcod_points,
truth,# name of column, can be unquoted
estimate,# name of column, can be unquoted
n = list(c(5, 5), c(2, 2))
)
https://github.com/ropensci/waywiser
Suggested by @kellijohnson-NOAA
The text was updated successfully, but these errors were encountered: