
Function for prediction at new locations for latent factor joint species distribution models
predict.lfJSDM.Rd
The function predict
collects posterior predictive samples for a set of new locations given an object of class `lfJSDM`.
Usage
# S3 method for lfJSDM
predict(object, X.0, coords.0,
ignore.RE = FALSE, ...)
Arguments
- object
an object of class lfJSDM
- X.0
the design matrix of covariates at the prediction locations. This should include a column of 1s for the intercept if an intercept is included in the model. If random effects are included in the model, the levels of the random effects at the new locations should be included as a column in the design matrix. The ordering of the levels should match the ordering used to fit the data in
lfJSDM
. Columns should correspond to the order of how covariates were specified in the formula argument oflfJSDM
. Column names of the random effects must match the name of the random effects, if specified in the formula argument oflfJSDM
.- coords.0
the spatial coordinates corresponding to
X.0
. Note thatspOccupancy
assumes coordinates are specified in a projected coordinate system.- ignore.RE
a logical value indicating whether to include unstructured random effects for prediction. If TRUE, random effects will be ignored and prediction will only use the fixed effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the random effect.
- ...
currently no additional arguments
Note
When ignore.RE = FALSE
, both sampled levels and non-sampled levels of random effects are supported for prediction. For sampled levels, the posterior distribution for the random intercept corresponding to that level of the random effect will be used in the prediction. For non-sampled levels, random values are drawn from a normal distribution using the posterior samples of the random effect variance, which results in fully propagated uncertainty in predictions with models that incorporate random effects.
Author
Jeffrey W. Doser doserjef@msu.edu,
Andrew O. Finley finleya@msu.edu
Value
A list object of class predict.lfJSDM
that consists of:
- psi.0.samples
a three-dimensional array of posterior predictive samples for the latent occurrence probability values.
- z.0.samples
a three-dimensional array of posterior predictive samples for the latent occurrence values.
- w.0.samples
a three-dimensional array of posterior predictive samples for the latent factors.
The return object will include additional objects used for standard extractor functions.
Examples
set.seed(400)
J.x <- 8
J.y <- 8
J <- J.x * J.y
n.rep<- sample(2:4, size = J, replace = TRUE)
N <- 6
# Community-level covariate effects
# Occurrence
beta.mean <- c(0.2, 0.5)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 0.3)
# Detection
alpha.mean <- c(0.5, 0.2, -0.1)
tau.sq.alpha <- c(0.2, 0.3, 1)
p.det <- length(alpha.mean)
# Draw species-level effects from community means.
beta <- matrix(NA, nrow = N, ncol = p.occ)
alpha <- matrix(NA, nrow = N, ncol = p.det)
for (i in 1:p.occ) {
beta[, i] <- rnorm(N, beta.mean[i], sqrt(tau.sq.beta[i]))
}
for (i in 1:p.det) {
alpha[, i] <- rnorm(N, alpha.mean[i], sqrt(tau.sq.alpha[i]))
}
n.factors <- 3
dat <- simMsOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, N = N, beta = beta, alpha = alpha,
sp = FALSE, factor.model = TRUE, n.factors = n.factors)
n.samples <- 5000
# Split into fitting and prediction data set
pred.indx <- sample(1:J, round(J * .25), replace = FALSE)
# Summarize the multiple replicates into a single value for use in a JSDM
y <- apply(dat$y[, -pred.indx, ], c(1, 2), max, na.rm = TRUE)
# Covariates
X <- dat$X[-pred.indx, ]
# Spatial coordinates
coords <- dat$coords[-pred.indx, ]
# Prediction values
X.0 <- dat$X[pred.indx, ]
psi.0 <- dat$psi[, pred.indx]
coords.0 <- dat$coords[pred.indx, ]
# Package all data into a list
covs <- X[, 2, drop = FALSE]
colnames(covs) <- c('occ.cov')
data.list <- list(y = y,
covs = covs,
coords = coords)
# Occupancy initial values
prior.list <- list(beta.comm.normal = list(mean = 0, var = 2.72),
tau.sq.beta.ig = list(a = 0.1, b = 0.1))
# Initial values
lambda.inits <- matrix(0, N, n.factors)
diag(lambda.inits) <- 1
lambda.inits[lower.tri(lambda.inits)] <- rnorm(sum(lower.tri(lambda.inits)))
inits.list <- list(alpha.comm = 0,
beta.comm = 0,
beta = 0,
tau.sq.beta = 1,
lambda = lambda.inits)
# Note that this is just a test case and more iterations/chains may need to
# be run to ensure convergence.
out <- lfJSDM(formula = ~ occ.cov,
data = data.list,
inits = inits.list,
n.samples = n.samples,
n.factors = 3,
priors = prior.list,
n.omp.threads = 1,
verbose = TRUE,
n.report = 1000,
n.burn = 4000)
#> ----------------------------------------
#> Preparing to run the model
#> ----------------------------------------
#> ----------------------------------------
#> Model description
#> ----------------------------------------
#> Latent Factor JSDM with Polya-Gamma latent
#> variable fit with 48 sites and 6 species.
#>
#> Samples per Chain: 5000
#> Burn-in: 4000
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 1000
#>
#> Using 3 latent factors.
#>
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#>
#> ----------------------------------------
#> Chain 1
#> ----------------------------------------
#> Sampling ...
#> Sampled: 1000 of 5000, 20.00%
#> -------------------------------------------------
#> Sampled: 2000 of 5000, 40.00%
#> -------------------------------------------------
#> Sampled: 3000 of 5000, 60.00%
#> -------------------------------------------------
#> Sampled: 4000 of 5000, 80.00%
#> -------------------------------------------------
#> Sampled: 5000 of 5000, 100.00%
summary(out)
#>
#> Call:
#> lfJSDM(formula = ~occ.cov, data = data.list, inits = inits.list,
#> priors = prior.list, n.factors = 3, n.samples = n.samples,
#> n.omp.threads = 1, verbose = TRUE, n.report = 1000, n.burn = 4000)
#>
#> Samples per Chain: 5000
#> Burn-in: 4000
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 1000
#> Run Time (min): 0.009
#>
#> ----------------------------------------
#> Community Level
#> ----------------------------------------
#> Means (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) -0.6866 0.6615 -2.0425 -0.6611 0.5878 NA 494
#> occ.cov 0.3710 0.3138 -0.2758 0.3859 0.9718 NA 299
#>
#> Variances (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept) 3.2808 3.2062 0.5214 2.4417 10.6860 NA 367
#> occ.cov 0.3972 0.5481 0.0405 0.2263 1.6701 NA 249
#>
#> ----------------------------------------
#> Species Level
#> ----------------------------------------
#> Estimates (logit scale):
#> Mean SD 2.5% 50% 97.5% Rhat ESS
#> (Intercept)-sp1 0.7700 0.3787 0.0939 0.7656 1.5485 NA 473
#> (Intercept)-sp2 -0.5180 0.3897 -1.3274 -0.5005 0.1727 NA 457
#> (Intercept)-sp3 -1.1655 0.5039 -2.1482 -1.1600 -0.2653 NA 215
#> (Intercept)-sp4 -0.2562 0.3931 -1.0937 -0.2482 0.4896 NA 466
#> (Intercept)-sp5 -0.0612 0.4520 -1.0065 -0.0443 0.7668 NA 284
#> (Intercept)-sp6 -3.4003 0.9080 -5.3860 -3.2944 -1.8693 NA 78
#> occ.cov-sp1 0.6920 0.3362 0.0946 0.6601 1.4096 NA 405
#> occ.cov-sp2 0.3138 0.3019 -0.2540 0.3113 0.9110 NA 414
#> occ.cov-sp3 0.2059 0.3717 -0.5998 0.2285 0.8716 NA 231
#> occ.cov-sp4 0.7180 0.3278 0.1194 0.6840 1.4000 NA 424
#> occ.cov-sp5 0.5076 0.3441 -0.1686 0.5090 1.2197 NA 379
#> occ.cov-sp6 -0.0982 0.5646 -1.5322 0.0017 0.7541 NA 136
# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0)