Learn R Programming

spmodel (version 0.15.0)

conditional: Conditionally simulate from a model

Description

Conditionally simulate prediction data from a model object.

Usage

conditional(object, ...)

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

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

# S3 method for spautor conditional(object, newdata, output = "newdata", samples = 1000, ...)

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

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 (splm() and spautor()) or fitted link values (including offsets) (spglm() and spgautor()), 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 effects coefficients, separated by partition factor (when relevant). If output = "cov"/"spcov"/"randcov"

(simulate_covparams = TRUE only), a (covariance parameter) x b matrix of the of conditoinal simulations for each covariance parameter, where b is the number of samples. "cov" returns every covariance parameter, while "spcov"/"randcov" return just the spatial/random-effect covariance parameters, respectively.

If output has more than one element, a list is returned with the respetive 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.

Arguments

object

A fitted model object.

...

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

newdata

A data frame or sf object in which to look for variables with which to predict. If a data frame, newdata must contain all variables used by formula(object) and all variables representing coordinates. If an sf object, newdata must contain all variables used by formula(object) and coordinates are obtained from the geometry of newdata. If omitted, missing data from the fitted model object are used.

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", "spcov", "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 splm() 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.

type

For spglm() and spgautor() 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.

Details

If "newdata" is in output, conditional simulations are returned for each row of newdata. 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 (splm() or spautor()) or fitted link values (including offsets) (spglm() or spgautor()) 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"/"spcov"/"randcov" is in output (only available when simulate_covparams = TRUE), the simulated covariance parameter draws themselves are returned.

Examples

Run this code
set.seed(0)
spmod <- splm(sulfate ~ 1, data = sulfate, spcov_type = "exponential")
cond <- conditional(spmod, newdata = sulfate_preds)
predict(spmod, sulfate_preds[20, ], se.fit = TRUE)
c("fit_cond" = mean(cond[20, ]), "se.fit_cond" = sd(cond[20, ]))
hist(cond[20, ])

Run the code above in your browser using DataLab