Description Usage Arguments Details Value Examples
Genenerate random draws from the mNIX distribution.
1 | mnix_sim(n, lambda, Omega, nu, tau, y, X, id)
|
n |
Number of random draws (integer). |
lambda |
Mean parameter. A vector of length |
Omega |
Precision matrix parameter. A matrix of size |
nu |
Degrees-of-freedom parameter. A scalar or a vector of length |
tau |
Scale parameter. A scalar or a vector of length |
y |
Optional response vector of length |
X |
Optional covariate matrix of size |
id |
Optional subject identifier vector of length |
If y
, X
and id
are unspecified, then (beta, sigma)
are drawn from mNIX(lambda, Omega, nu, tau)
. Otherwise, they are drawn from the posterior mNIX distribution p(beta, sigma | y, X, id, lambda, Omega, nu, tau)
. In this case, the number of subjects nsub
must be n
or 1
.
A list with elements beta
and sigma
of size n x p
and length n
, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # helper functions
# generate random dataset with nSub subjects and p covariates
sim_data <- function(nSub, p) {
N <- sample(2:10, nSub, replace = TRUE)
M <- sum(N)
X <- matrix(rnorm(M*p), M, p)
y <- rnorm(M)
id <- sample(rep(1:length(N), times = N))
list(id = id, X = X, y = y)
}
# generate nPhi random hyperparameter sets on p covariates
sim_phi <- function(nPhi, p) {
Phi <- replicate(n = nPhi, {
list(lambda = rnorm(p), Omega = diag(rexp(p)),
nu = rexp(1), tau = rexp(1))
}, simplify = FALSE)
# format form mnix_sim
Phi <- unlist_bind(Phi,
name = c("lambda", "Omega", "nu", "tau"),
bind = c(rbind, cbind, c, c))
Phi$Omega <- drop(array(Phi$Omega, dim = c(p, p, nPhi)))
Phi
}
# sample from unrestricted mNIX distribution
n <- 5 # number of samples
p <- 2 # number of covariates
phi <- sim_phi(nPhi = 1, p = p) # generate hyperparameters
mnix_sim(n = n,
lambda = phi$lambda, Omega = phi$Omega, nu = phi$nu, tau = phi$tau)
# vectorized calculations
Phi <- sim_phi(nPhi = n, p = p)
mnix_sim(n = n,
lambda = Phi$lambda, Omega = Phi$Omega, nu = Phi$nu, tau = Phi$tau)
# sample from posterior mNIX distribution
# generate data
yX <- sim_data(nSub = n, p = p)
# single phi, single subject
mnix_sim(n = n,
y = yX$y[yX$id == 1], X = yX$X[yX$id == 1,],
lambda = phi$lambda, Omega = phi$Omega, nu = phi$nu, tau = phi$tau)
# single phi, multiple subjects
mnix_sim(n = n,
y = yX$y, X = yX$X, id = yX$id,
lambda = phi$lambda, Omega = phi$Omega, nu = phi$nu, tau = phi$tau)
# multiple phi, multiple subjects
mnix_sim(n = n,
y = yX$y, X = yX$X, id = yX$id,
lambda = Phi$lambda, Omega = Phi$Omega, nu = Phi$nu, tau = Phi$tau)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.