sdmTMB icon indicating copy to clipboard operation
sdmTMB copied to clipboard

Add tmbstan examples, vignette, and predict function

Open seananderson opened this issue 4 years ago • 1 comments

Probably needs a prediction helper function to pass the posterior samples through the obj$report() function as is done in the predict function for the joint precision matrix simulation.

Should also be accompanied by map and start arguments in sdmTMB() and enhancing the ability to specify priors on parameters. From my testing, the PC prior helps a lot.

May want the ability to scale omega_s and epsilon_st deviations to help NUTS adaptation.

Would need a vignette with guidance.

seananderson avatar Jun 03 '21 17:06 seananderson

Some example code to work with:

# ...
priors <- sdmTMBpriors(
  b = normal(rep(0, 16),
    scale = c(
      rep(10, 7),
      rep(5, 1),
      rep(5, 8)
    )
  )
)

formula <- density ~ 0 +
    as.factor(year) +
    as.factor(survey) +
    poly(rocky, 3) +
    poly(muddy, 3) +
    poly(depth_scaled, 2)

fit <- sdmTMB(
  formula,
  data = dat,
  mesh = mesh,
  spatiotemporal = list("off", "on"),
  priors = priors,
  time = "year",
  silent = FALSE,
  anisotropy = TRUE,
  family = delta_gamma()
)

# fix kappa and anisotropy parameters at MLE values:
pars <- sdmTMB:::get_pars(fit)
kappa_map <- factor(rep(NA, length(pars$ln_kappa)))
H_map <- factor(rep(NA, length(pars$ln_H_input)))

fit_mle <- update(
  fit,
  control = sdmTMBcontrol(
    start = list(
      ln_kappa = pars$ln_kappa,
      ln_H_input = pars$ln_H_input
    ),
    map = list(
      ln_kappa = kappa_map,
      ln_H_input = H_map
    )
  ),
  do_fit = FALSE
)

 fit_stan <- tmbstan(
    obj = fit_mle,
    iter = 2000,
    chains = 4,
    seed = 192819,
    control = list(adapt_delta = 0.9, max_treedepth = 12)
  )

p <- predict(fit_mle, tmbstan_model = fit_stan)
p1 <- predict(fit_mle, tmbstan_model = fit_stan, model = 1)
p2 <- predict(fit_mle, tmbstan_model = fit_stan, model = 2)

# ...

seananderson avatar May 03 '22 20:05 seananderson