Simulate from a spatial/spatiotemporal model

sdmTMB_sim(
  mesh,
  x,
  y,
  range,
  time_steps = 1L,
  X = NULL,
  betas = NULL,
  family = gaussian(link = "identity"),
  rho = 0,
  sigma_O = 0.1,
  sigma_E = 0,
  sigma_V = rep(0, length(betas)),
  phi = 0.01,
  thetaf = 1.5,
  df = 3,
  seed = sample.int(1e+06, 1),
  list = FALSE,
  size = NULL
)

Arguments

mesh

Output from make_mesh() or a mesh directly from INLA.

x

A vector of x coordinates. Should match mesh.

y

A vector of y coordinates. Should match mesh.

range

Parameter that controls the decay of spatial correlation.

time_steps

The number of time steps.

X

An optional covariate design matrix. If omitted and betas is not NULL, will be set to standard normal draws.

betas

A vector of beta values (design-matrix fixed-effect coefficient values). If a random walk (sigma_V > 0), these are the starting values.

family

Family as in sdmTMB().

rho

Spatiotemporal correlation between years; should be between -1 and 1.

sigma_O

SD of spatial process (Omega).

sigma_E

SD of spatiotemporal process (Epsilon). Can be scalar or vector for time-varying model.

sigma_V

A vector of standard deviations of time-varying random walk on parameters. Set to 0 for parameters that should not vary through time.

phi

Observation error scale parameter.

thetaf

Tweedie p (power) parameter; between 1 and 2.

df

Student-t degrees of freedom.

seed

A value with which to set the random seed.

list

Logical for whether output is in list format. If TRUE, data is in list element 1 and input values in element 2.

size

Specific for the binomial family, vector representing binomial N. If not included, defaults to 1 (bernoulli)

Value

A data frame where:

  • omega_s represents the simulated spatial random effects.

  • epsilon_st represents the simulated spatiotemporal random effects.

  • eta is the true value in link space

  • mu is the true value in inverse link space.

  • observed represents the simulated process with observation error.

  • b_... contain the beta values for each covariate used to simulate each time slice.

  • cov_... covariate values for each observation.

Examples

# \donttest{
if (inla_installed()) {
set.seed(42)
x <- runif(50, -1, 1)
y <- runif(50, -1, 1)
N <- length(x)
time_steps <- 6
X <- model.matrix(~ x1, data.frame(x1 = rnorm(N * time_steps)))
loc <- data.frame(x = x, y = y)
mesh <- make_mesh(loc, xy_cols = c("x", "y"), cutoff = 0.1)
s <- sdmTMB_sim(
  x = x, y = y, mesh = mesh, X = X,
  betas = c(0.5, 0.7), time_steps = time_steps, rho = 0.5,
  phi = 0.2, range = 0.8, sigma_O = 0, sigma_E = 0.3,
  seed = 123, family = gaussian()
)

mesh <- make_mesh(s, xy_cols = c("x", "y"), cutoff = 0.1)
m <- sdmTMB(
  data = s, formula = observed ~ x1,
  time = "time", spde = mesh,
  fields = "AR1", include_spatial = FALSE
)
tidy(m, conf.int = TRUE)
tidy(m, "ran_pars", conf.int = TRUE)

# example with time-varying sigma_E (spatiotemporal variation)
s <- sdmTMB_sim(
  x = x, y = y, mesh = mesh, X = X,
  betas = c(0.5, 0.7), time_steps = time_steps, rho = 0,
  phi = 0.2, range = 0.8, sigma_O = 0,
  sigma_E = seq(0.2, 1, length.out = time_steps),
  seed = 123, family = gaussian())
}
# }