Conditionally simulate prediction data from a model object.

# S3 method for class 'ssn_lm'
conditional(
  object,
  newdata,
  output = "newdata",
  samples = 1000,
  simulate_covparams = FALSE,
  ...
)

# S3 method for class 'ssn_glm'
conditional(
  object,
  newdata,
  output = "newdata",
  type = c("link", "response", "new"),
  samples = 1000,
  newdata_size,
  ...
)

Arguments

object

A fitted model object.

newdata

A character string naming a single prediction data set (accessible via object$ssn.object$preds). May be omitted when "newdata" is not requested; otherwise, omission uses the .missing prediction set, if present. "all"/multiple prediction sets are not supported.

output

The output type, which can be any subset of c("newdata", "beta", "object") ("all" is shorthand for this full vector). If simulate_covparams = TRUE, the output type can also be any subset of c("cov", "ssn", "tailup", "taildown", "euclid", "nugget", "randcov"). For generalized linear models, "latent" returns simulated observed-site link values, including offsets. "random" returns realized random effects. The default is "newdata". See Details for more.

samples

The number of conditional simulations. The default is 1,000.

simulate_covparams

For ssn_lm() model objects, whether to also simulate new covariance parameters for each sample. simulate_covparams requires object$vcov$cov to be specified during model fitting by selecting ddf = "satterthwaite". simulate_covparams = TRUE should generally not be used for sample sizes greater than 500 given its computational inefficiencies. The default is FALSE.

...

Other arguments. Not used (needed for generic consistency).

type

For ssn_glm() model objects, the scale of the conditional simulations for newdata. When type = "link", the predicted means on the link scale are returned. When type = "response", the predicted means on the response scale are returned. When type = "new", a new observation is simulated from the appropriate response distribution with mean equal to the mean on the response scale and dispersion equal to the dispersion parameter from object. The default is "link".

newdata_size

The size value for each observation in newdata used when predicting for the binomial family, with a default value of 1.

Value

If output = "newdata", an a x b matrix of conditional simulations for each row in newdata, where a is the number of rows in newdata and b is the number of samples. If output = "beta", an p x b matrix of conditional simulations for each element in coef(object), where p is the number of fixed effects and b is the number of samples. If output = "object", an n x b matrix of observed responses (ssn_lm()) or fitted link values (including offsets) (ssn_glm()), where n is the number of observed rows and b is the number of samples. output = "latent" returns an n x b matrix of simulated observed link values (including offsets). output = "random" returns a q x b matrix of realized random-effect coefficients, separated by partition factor (when relevant). If output = "cov"/"ssn"/"tailup"/"taildown"/ "euclid"/"nugget"/"randcov" (simulate_covparams = TRUE only), a (covariance parameter) x b matrix of conditional simulations for each covariance parameter is returned, where b is the number of samples. "cov" returns every free covariance parameter, while "ssn" returns the tailup, taildown, Euclidean, and nugget subset. "tailup"/"taildown"/"euclid"/"nugget"/ "randcov" return just the corresponding covariance parameters.

If output has more than one element, a list is returned with the respective elements named according to the relevant output. For example, output = c("newdata", "beta") returns a list with elements "newdata" and "beta", each containing the relevant output for output = "newdata" and output = "beta", respectively.

Details

If "newdata" is in output, conditional simulations are returned for each row of the named prediction set. If "beta" is in output, conditional simulations are returned for each fixed effect (i.e., element of coef(object)). If "object" is in output, observed responses (ssn_lm()) or fitted link values (including offsets) (ssn_glm()) are returned in observed-row order, repeated across simulation columns. If "latent" is requested, the actual observed link-scale draws are returned, with offsets included. type affects only newdata. For example, c("newdata", "beta") returns the conditional simulations both for newdata and for the fixed effects. If "cov"/"ssn"/"tailup"/"taildown"/ "euclid"/"nugget"/"randcov" is in output (only available when simulate_covparams = TRUE), the simulated covariance parameter draws themselves are returned.

Examples

# Copy the mf04p .ssn data to a local directory and read it into R
# When modeling with your .ssn object, you will load it using the relevant
# path to the .ssn data on your machine
copy_lsn_to_temp()
temp_path <- paste0(tempdir(), "/MiddleFork04.ssn")
mf04p <- ssn_import(temp_path, predpts = "CapeHorn", overwrite = TRUE)
ssn_create_distmat(mf04p, predpts = "CapeHorn", overwrite = TRUE, among_predpts = TRUE)

ssn_mod <- ssn_lm(
  formula = Summer_mn ~ ELEV_DEM,
  ssn.object = mf04p,
  tailup_type = "exponential",
  additive = "afvArea"
)
set.seed(1)
draws <- conditional(ssn_mod, "CapeHorn")
#> Error in conditional(ssn_mod, "CapeHorn"): could not find function "conditional"
head(draws) # rows are prediction locations; columns are simulations
#> Error in eval(expr, envir, enclos): object 'draws' not found