sim_MVN_X {sim2Dpredictr} | R Documentation |
Simulate Spatially Correlated MVN Data
Description
Takes N draws from a Multivariate Normal (MVN) distribution using either
base R or the R package spam
. This function requires the Cholesky
decomposition of the desired covariance matrix.
Usage
sim_MVN_X(
N,
mu = 0,
L = NULL,
R = NULL,
S = NULL,
Q = NULL,
use.spam = FALSE,
use.MASS = FALSE,
X.categorical = FALSE,
X.num.categories = 2,
X.category.type = "percentile",
X.percentiles = NULL,
X.manual.thresh = NULL,
X.cat.names = NULL
)
Arguments
N |
The number of draws to take from MVN; i.e., the number of subjects. |
mu |
One of the following:
|
L , R |
|
S , Q |
A covariance or precision matrix respectively. These are for
use with |
use.spam |
Logical. If |
use.MASS |
Logical. When |
X.categorical |
Default is |
X.num.categories |
A scalar value denoting the number of categories in which to divide the data. |
X.category.type |
Tells R how to categorize the data. Options are
|
X.percentiles |
A vector of percentiles to be used in thresholding
when |
X.manual.thresh |
A vector containing the thresholds for categorizing
the values; e.g. if |
X.cat.names |
A vector of category names. If |
Value
Matrix of dimension N
x (nrow(L))
(or equivalently
N
x (nrow(R))
) where each row is draw from MVN, and each
column represents a different "variable"; e.g. location in an image.
Note
This function requires the Cholesky decomposition of the desired
covariance matrix for the MVN; this allows for using this function in
simulating multiple datasets of N
MVN draws while only taking the
Cholesky decomposition of the covariance matrix once.
References
Furrer R, Sain SR (2010). “spam: A Sparse Matrix R Package with Emphasis on MCMC Methods for Gaussian Markov Random Fields.” Journal of Statistical Software, 36(10), 1-25. https://www.jstatsoft.org/v36/i10/.
Ripley BD (1987). Stochastic Simulation. John Wiley & Sons. doi:10.1002/9780470316726.
Rue H (2001). “Fast Sampling of Gaussian Markov Random Fields.” Journal of the Royal Statistical Society B, 63, 325-338. doi:10.1111/1467-9868.00288.
Examples
## verify MVN with base R
set.seed(732)
Lex <- chol_s2Dp(corr.structure = "ar1",
im.res = c(3, 3),
rho = 0.25,
sigma = 1,
use.spam = FALSE,
corr.min = 0.02,
triangle = "lower",
return.cov = TRUE)
XbR = sim_MVN_X(N = 1000, mu = 0, L = Lex$L)
apply(XbR, 2, mean)
cov(XbR)
Lex$S
## verify MVN with \code{spam}
set.seed(472)
Rex <- chol_s2Dp(im.res = c(3, 3), matrix.type = "prec",
use.spam = TRUE, neighborhood = "ar1",
triangle = "upper", return.prec = TRUE)
Xspam = sim_MVN_X(N = 1000, mu = 0, R = Rex$R, Q = Rex$Q)
apply(Xspam, 2, mean)
solve(cov(Xspam))
as.matrix(Rex$Q)
## Categories
set.seed(832)
Xtest <- sim_MVN_X(N = 30, mu = 0, L = Lex$L,
X.categorical = TRUE,
X.num.categories = 3,
X.category.type = "percentile",
X.cat.names = c("A", "B", "C"))
Xtest