pbs-assess / sdmTMB

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

Visreg Error: Specified time column is missing from data #172

Closed seananderson closed 1 year ago

seananderson commented 1 year ago

Discussed in https://github.com/pbs-assess/sdmTMB/discussions/171

Originally posted by **NathanHebertMSVU** February 1, 2023 I fit my model with the following function call: _presence_model <- sdmTMB(presence ~ DEM_logScaled+I(DEM_logScaled^2)+BtmTempBNAMScaled+I(BtmTempBNAMScaled^2)+RangeTempScaled+ I(RangeTempScaled^2)+BtmSalinityBNAMScaled+I(BtmSalinityBNAMScaled^2)+BtmStressBNAMLogScaled+RangeStressLogScaled+ sqrt_DEM_SlopeScaled+DEM_NorthernessScaled+DEM_EasternessScaled+DEM_RDMVScaled+ as.numeric(snowcrab), data = survey_data_combined, mesh = mesh_model, family = binomial(), spatiotemporal = "ar1", spatial = "off", time = "year", control = sdmTMBcontrol(newton_loops = 1))_ If I then try and make a plot with visreg using this code: _visreg(presence_model, xvar = "RangeTempScaled", partial = F, rug = 1)_ I get: _Error: Specified `time` column is missing from `data`._ I'm not sure what is wrong. I would greatly appreciate any help!
seananderson commented 1 year ago

Hi @NathanHebertMSVU I've moved this to an issue because that shouldn't happen. However, I can't reproduce it. Can you try to reproduce it with with some version of the following? First, make sure you're on the latest sdmTMB (GitHub or CRAN should be fine). Second, maybe take a look at the str() of your data frame and check if there's anything funny with the time column 'year'?

library(sdmTMB)
fit <- sdmTMB(
  present ~ depth_scaled,
  data = pcod_2011,
  mesh = pcod_mesh_2011, 
  family = binomial(), 
  time = "year",
  spatial = "off", 
  spatiotemporal = "ar1"
)
visreg::visreg(fit, "depth_scaled", partial = FALSE, rug = 1)

Created on 2023-02-01 with reprex v2.0.2

Nathan-Hebert commented 1 year ago

I appreciate the very prompt reply! I updated to the current version.

It looks like "year" is of type int, which I assume should be okay? I unfortunately can't reproduce it with your code.

seananderson commented 1 year ago

If you can't get it to happen with the pcod dataset, can you email me the data set you have (maybe as an .rds) along with some reproducible code? It would be nice to get to the bottom of it.

seananderson commented 1 year ago

The issue is that visreg breaks (removes extra columns of data, i.e., removes the time column) if the response is of class logical. I have no idea why and haven't dug into their code. For now I added a warning:

library(sdmTMB)
pcod_2011$present_logical <- as.logical(pcod_2011$present)
fit <- sdmTMB(
  present_logical ~ depth_scaled,
  data = pcod_2011,
  mesh = pcod_mesh_2011, 
  family = binomial(), 
  spatial = "off", 
  spatiotemporal = "iid",
  time = "year"
)
#> Warning: We recommend against using `TRUE`/`FALSE` response values if you are going to
#> use the `visreg::visreg()` function after. Consider converting to integer with
#> `as.integer()`.
visreg::visreg(fit, "depth_scaled")
#> Error: Specified `time` column is missing from `data`.

Created on 2023-02-01 by the reprex package (v2.0.1)