Closed fhui28 closed 1 week ago
Thanks for pointing this out -- I don't think there's actually a problem here and will try to explain. It's probably easier to think about this when using the cutoff argument instead of n_knots
. First, the simulated data has a really high spatial range (0.5) so you would expect the spatial field to be pretty smooth (as the domain of X and Y are 0, 1). Second, when constructing these meshes, we generally want the cutoff distance to be smaller than the spatial range, so that we can capture finer scale spatial variability. In this example, the cutoff distance is greater than the spatial range, and the spatial random effects in the simulated object are all estimated at 0 (so the fields are not reported). I tried reducing range = 0.2
-- a cutoff distance of 0.2 will not generate omega_s
in the simulated dataframe, but all other cutoff values < 0.2 will.
Thanks for explaining Eric,
Your explanation makes sense and I don't see a problem methodologically. But I nevertheless think a warning should be put in place for the simulate function, especially given you can fit this coarse mesh model but then cannot properly simulate off the fitted object.
A better option might be to offer guidelines and warnings from make_mesh?
Get Outlook for Androidhttps://aka.ms/AAb9ysg
From: Eric Ward @.> Sent: Friday, September 13, 2024 10:47:58 PM To: pbs-assess/sdmTMB @.> Cc: Francis KC Hui @.>; Author @.> Subject: Re: [pbs-assess/sdmTMB] sdmTMB_simulate issue for very coarse mesh (Issue #370)
Thanks for pointing this out -- I don't think there's actually a problem here and will try to explain. It's probably easier to think about this when using the cutoff argument instead of n_knots. First, the simulated data has a really high spatial range (0.5) so you would expect the spatial field to be pretty smooth (as the domain of X and Y are 0, 1). Second, when constructing these meshes, we generally want the cutoff distance to be smaller than the spatial range, so that we can capture finer scale spatial variability. In this example, the cutoff distance is greater than the spatial range, and the spatial random effects in the simulated object are all estimated at 0 (so the fields are not reported). I tried reducing range = 0.2 -- a cutoff distance of 0.2 will not generate omega_s in the simulated dataframe, but all other cutoff values < 0.2 will.
— Reply to this email directly, view it on GitHubhttps://github.com/pbs-assess/sdmTMB/issues/370#issuecomment-2348879081, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABH6DBBZWSQI56MFQDGZERDZWLNHPAVCNFSM6AAAAABOELK76GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBYHA3TSMBYGE. You are receiving this because you authored the thread.Message ID: @.***>
I think some warnings like this should fix it: https://github.com/pbs-assess/sdmTMB/pull/371/commits/1418f42a2b905f1fcd9febf235103133161a5b2b
I think it's more insidious than that. I had messed up the logic such that those random field columns were getting dropped if any field values were exactly zero. I had meant for the logic to be that if all the field values were zero, drop them. I changed the logic such that they are included only if the input sigma_O, sigma_E, etc. are > 0. Thankfully this did not affect the 'observed', 'mu', or 'eta', columns—it just meant the field columns were mysteriously missing sometimes. Thanks for pointing this out @fhui28! It should be fixed now and I've added a unit test to cover this. https://github.com/pbs-assess/sdmTMB/commit/cb83a62207a2d62a4e1f7a8b2fa02d1535a2e703
library(sdmTMB)
set.seed(123)
predictor_dat <- data.frame(
X = runif(100), Y = runif(100),
a1 = rnorm(100), year = rep(1:2, each = 50))
mesh <- sdmTMB::make_mesh(predictor_dat, xy_cols = c("X", "Y"), n_knots = 30)
sim_dat <- sdmTMB::sdmTMB_simulate(
formula = ~ 1 + a1,
data = predictor_dat,
time = "year",
mesh = mesh,
family = gaussian(),
range = 0.5,
sigma_E = 0.1,
phi = 0.1,
sigma_O = 0.2,
seed = 42,
B = c(0.2, -0.4) # B0 = intercept, B1 = a1 slope
)
head(sim_dat, n = 3)
#> year X Y omega_s epsilon_st mu eta
#> 1 1 0.2875775 0.5999890 -0.08521738 0.015363059 0.41430830 0.41430830
#> 2 1 0.7883051 0.3328235 -0.28367725 -0.001568776 -0.18799951 -0.18799951
#> 3 1 0.4089769 0.4886130 -0.44112493 0.072181557 -0.07026663 -0.07026663
#> observed (Intercept) a1
#> 1 0.4135321 1 -0.7104066
#> 2 -0.2680277 1 0.2568837
#> 3 -0.1236159 1 -0.2466919
Created on 2024-09-13 with reprex v2.1.1
When the mesh is created with a sufficiently small number of knots/large cutoff so the mesh is coarse,
sdmTMB_simulate
will stop generating a spatial field as part of the data generation process; please see below taken from the example in the help file.I suspect the issue is that the mesh is not great (although the model still actually fits?), but regardless think it would be good in the least to produce a warning somewhere about this.
Thanks heaps sdmTMB team!
Make fake predictor(s) (a1) and sampling locations:
When n_knots goes below certain number (in this case below 24 knots), no spatial field will be generated as part of the simulation process. The issue also arises, as one would expect, if cutoffs is greater than a certain amount
Simulate responses
Note the model actually fits, though not sure if spatial parameter estimates make sense here or not
Created on 2024-09-13 with reprex v2.1.1