Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look into waywiser compatibility #193

Open
seananderson opened this issue Mar 21, 2023 · 1 comment
Open

Look into waywiser compatibility #193

seananderson opened this issue Mar 21, 2023 · 1 comment

Comments

@seananderson
Copy link
Member

https://github.com/ropensci/waywiser

Suggested by @kellijohnson-NOAA

@ericward-noaa
Copy link
Collaborator

'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))
)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants