pbs-assess / sdmTMB

:earth_americas: An R package for spatial and spatiotemporal GLMMs with TMB
https://pbs-assess.github.io/sdmTMB/
187 stars 27 forks source link

Error in applying visreg to fitted model #366

Closed mdmazur closed 1 month ago

mdmazur commented 2 months ago

I have been using visreg for a while now to plot conditional effects with sdmTMB, and at the end of last week I have started getting this error:

Error in `predict()`:
! `xy_cols` (the column names for the x and y coordinates) are not in
  `newdata`.

It seems to run the model again when running the visreg function. I'm not sure why it seems to think the model should be updated, but I think that may be where the error is coming from. Any ideas as to why this is happening?

Lewis-Barnett-NOAA commented 1 month ago

Please make sure that the column names for your spatial coordinates are the same in the fitted model and your prediction data frame

On Tue, Sep 3, 2024 at 3:53 PM mdmazur @.***> wrote:

I have been using visreg for a while now to plot conditional effects with sdmTMB, and at the end of last week I have started getting this error:

Error in predict(): ! xy_cols (the column names for the x and y coordinates) are not in newdata.

It seems to run the model again when running the visreg function. I'm not sure why it seems to think the model should be updated, but I think that may be where the error is coming from. Any ideas as to why this is happening?

— Reply to this email directly, view it on GitHub https://github.com/pbs-assess/sdmTMB/issues/366, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKMJP4A7Q3PTXCUPKYRTCLZUY4WVAVCNFSM6AAAAABNTCXIG2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGUYDGOJVGI3DSMI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Lewis Barnett, PhD (he/him) Research Fish Biologist

NOAA Fisheries, Alaska Fisheries Science Center 7600 Sand Point Way NE, Bldg 4 Seattle, Washington 98115 Google Voice: (206) 526-4111

seananderson commented 1 month ago

visreg does that part for you though. Can you provide a reproducible example, @mdmazur? You could start with this:

library(sdmTMB)
fit <- sdmTMB(
     density ~ s(depth_scaled),
     data = pcod_2011,
     mesh = pcod_mesh_2011,
     family = tweedie()
 )
visreg::visreg(fit, xvar = "depth_scaled")

Created on 2024-09-03 with reprex v2.1.1

It should be running the model through predict with newdata and also calculating residuals, but not refitting the model.

visreg has been a big pain to support. You can also make your own visualizations by forming a data frame with all but the variable of interest held at reference values, the variable of interest across a sequence of values, re_form = NA to omit random fields, re_form_iid = NA if needed, and se_fit = TRUE if you want standard errors (will be slower). There's an example in ?predict.sdmTMB if you read down to # Visualizing a marginal effect

mdmazur commented 1 month ago

Thank you, the data I am working with contains fishery data, so not sure I can share a reproducible example here, but here is the code for the sdmTMB model and visreg:

m <- sdmTMB(
  data = res,
  formula = presence ~ s(year,k=4) + poly(log(depth),2) + s(slope,k=5) + s(tpi,k=5) + as.factor(substrate) + as.factor(survey) + s(month,bs='cc',k=12),
  time = "year", mesh = mesh, family = binomial(link = "cloglog"),
  spatial='on',
  spatiotemporal='off',
  anisotropy=TRUE,
  silent=FALSE,
  control=sdmTMBcontrol(newton_loops=1))

visreg::visreg(m,xvar='depth')

Since posting this issue, I found that I don't always get this error. If I close out of my R session and start a new one and run the same code, visreg sometimes works. So maybe this is an issue with my R sessions and not necessarily sdmTMB and visreg.

Thanks for the alternative suggestion on visualizing a marginal effect. I might end up doing that if visreg does not consistently work with my code.

seananderson commented 1 month ago

Closing this since it doesn't seem to be reproducible, but feel free to re-open with a reproducible example.