postHocLM {spOccupancy} | R Documentation |
Function for Fitting Linear Mixed Models with Previous Model Estimates
Description
Function for fitting a linear (mixed) model as a second-stage model where the response variable itself comes from a previous model fit and has uncertainty associated with it. The response variable is assumed to be a set of estimates from a previous model fit, where each value in the response variable has a posterior MCMC sample of estimates. This function is useful for doing "posthoc" analyses of model estimates (e.g., exploring how species traits relate to species-specific parameter estimates from a multi-species occupancy model). Such analyses are sometimes referred to as "two-stage" analyses.
Usage
postHocLM(formula, data, inits, priors, verbose = FALSE,
n.report = 100, n.samples, n.chains = 1, ...)
Arguments
formula |
a symbolic description of the model to be fit for the model using R's model syntax. Only right-hand side of formula is specified. See example below. Random intercepts are allowed using lme4 syntax (Bates et al. 2015). |
data |
a list containing data necessary for model fitting.
Valid tags are |
inits |
a list with each tag corresponding to a parameter name.
Valid tags are |
priors |
a list with each tag corresponding to a parameter name.
Valid tags are |
verbose |
if |
n.report |
the interval to report MCMC progress. |
n.samples |
the number of posterior samples to collect in each chain. Note that
by default, the same number of MCMC samples fit in the first stage model is
assumed to be fit for the second stage model. If |
n.chains |
the number of chains to run in sequence. |
... |
currently no additional arguments |
Value
An object of class postHocLM
that is a list comprised of:
beta.samples |
a |
tau.sq.samples |
a |
y.hat.samples |
a |
sigma.sq.samples |
a |
beta.star.samples |
a |
rhat |
a list of Gelman-Rubin diagnostic values for some of the model parameters. |
ESS |
a list of effective sample sizes for some of the model parameters. |
run.time |
execution time reported using |
bayes.R2 |
a |
The return object will include additional objects used for subsequent summarization.
Author(s)
Jeffrey W. Doser doserjef@msu.edu,
References
Bates, Douglas, Martin Maechler, Ben Bolker, Steve Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01.
Examples
# Simulate Data -----------------------------------------------------------
set.seed(100)
N <- 100
beta <- c(0, 0.5, 1.2)
tau.sq <- 1
p <- length(beta)
X <- matrix(1, nrow = N, ncol = p)
if (p > 1) {
for (i in 2:p) {
X[, i] <- rnorm(N)
} # i
}
mu <- X[, 1] * beta[1] + X[, 2] * beta[2] + X[, 3] * beta[3]
y <- rnorm(N, mu, sqrt(tau.sq))
# Replicate y n.samples times and add a small amount of noise that corresponds
# to uncertainty from a first stage model.
n.samples <- 1000
y <- matrix(y, n.samples, N, byrow = TRUE)
y <- y + rnorm(length(y), 0, 0.25)
# Package data for use with postHocLM -------------------------------------
colnames(X) <- c('int', 'cov.1', 'cov.2')
data.list <- list(y = y, covs = X)
data <- data.list
inits <- list(beta = 0, tau.sq = 1)
priors <- list(beta.normal = list(mean = 0, var = 10000),
tau.sq.ig = c(0.001, 0.001))
# Run the model -----------------------------------------------------------
out <- postHocLM(formula = ~ cov.1 + cov.2,
inits = inits,
data = data.list,
priors = priors,
verbose = FALSE,
n.chains = 1)
summary(out)