Skip to contents

The function predict collects posterior predictive samples for a set of new locations given an object of class `sfJSDM`.

Usage

# S3 method for sfJSDM
predict(object, X.0, coords.0, n.omp.threads = 1, verbose = TRUE, 
        n.report = 100, ignore.RE = FALSE, ...)

Arguments

object

an object of class sfJSDM

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 sfJSDM. Columns should correspond to the order of how covariates were specified in the formula argument of sfJSDM. Column names of the random effects must match the name of the random effects, if specified in the formula argument of sfJSDM.

coords.0

the spatial coordinates corresponding to X.0. Note that spOccupancy assumes coordinates are specified in a projected coordinate system.

n.omp.threads

a positive integer indicating the number of threads to use for SMP parallel processing. The package must be compiled for OpenMP support. For most Intel-based machines, we recommend setting n.omp.threads up to the number of hyperthreaded cores. Note, n.omp.threads > 1 might not work on some systems.

verbose

if TRUE, model specification and progress of the sampler is printed to the screen. Otherwise, nothing is printed to the screen.

n.report

the interval to report sampling progress.

ignore.RE

a logical value indicating whether to include unstructured random effects for prediction. If TRUE, unstructured random effects will be ignored and prediction will only use the fixed effects and the spatial random effects. If FALSE, random effects will be included in the prediction for both observed and unobserved levels of the unstructured random effects.

...

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

An list object of class predict.sfJSDM 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 spatial factors.

run.time

execution time reported using proc.time().

The return object will include additional objects used for standard extractor functions.

Examples

set.seed(400)

# Simulate Data -----------------------------------------------------------
J.x <- 7
J.y <- 7
J <- J.x * J.y
n.rep <- sample(2:4, size = J, replace = TRUE)
N <- 5
# Community-level covariate effects
# Occurrence
beta.mean <- c(0.2, -0.15)
p.occ <- length(beta.mean)
tau.sq.beta <- c(0.6, 0.3)
# Detection
alpha.mean <- c(0.5, 0.2, -.2)
tau.sq.alpha <- c(0.2, 0.3, 0.8)
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
phi <- runif(n.factors, 3/1, 3/.4)
sp <- TRUE

dat <- simMsOcc(J.x = J.x, J.y = J.y, n.rep = n.rep, N = N, beta = beta, alpha = alpha,
                phi = phi, sigma.sq = sigma.sq, sp = TRUE, cov.model = 'exponential', 
                factor.model = TRUE, n.factors = n.factors)
#> sigma.sq is specified but will be set to 1 for spatial latent factor model

# Number of batches
n.batch <- 10
# Batch length
batch.length <- 25
n.samples <- n.batch * batch.length

# 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)
# Occupancy covariates
X <- dat$X[-pred.indx, ]
# Coordinates
coords <- as.matrix(dat$coords[-pred.indx, ])
# Prediction values
X.0 <- dat$X[pred.indx, ]
coords.0 <- as.matrix(dat$coords[pred.indx, ])
psi.0 <- dat$psi[, 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)

# Priors 
prior.list <- list(beta.comm.normal = list(mean = 0, var = 2.72), 
                   tau.sq.beta.ig = list(a = 0.1, b = 0.1), 
                   phi.unif = list(a = 3/1, b = 3/.1)) 
# Starting 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(beta.comm = 0, 
                   beta = 0, 
                   tau.sq.beta = 1, 
                   phi = 3 / .5, 
                   sigma.sq = 2,
                   lambda = lambda.inits)
# Tuning
tuning.list <- list(phi = 1) 

out <- sfJSDM(formula = ~ occ.cov, 
              data = data.list,
              inits = inits.list, 
              n.batch = n.batch, 
              batch.length = batch.length, 
              accept.rate = 0.43, 
              n.factors = 3,
              priors = prior.list, 
              cov.model = "exponential", 
              tuning = tuning.list, 
              n.omp.threads = 1, 
              verbose = TRUE, 
              NNGP = TRUE, 
              n.neighbors = 5, 
              search.type = 'cb', 
              n.report = 10, 
              n.burn = 100, 
              n.thin = 1)
#> ----------------------------------------
#> 	Preparing to run the model
#> ----------------------------------------
#> w is not specified in initial values.
#> Setting initial value to 0
#> ----------------------------------------
#> 	Building the neighbor list
#> ----------------------------------------
#> ----------------------------------------
#> Building the neighbors of neighbors list
#> ----------------------------------------
#> ----------------------------------------
#> 	Model description
#> ----------------------------------------
#> Spatial Factor NNGP JSDM with Polya-Gamma latent
#> variable fit with 37 sites and 5 species.
#> 
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 100 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 150 
#> 
#> Using the exponential spatial correlation model.
#> 
#> Using 3 latent spatial factors.
#> Using 5 nearest neighbors.
#> 
#> Source compiled with OpenMP support and model fit using 1 thread(s).
#> 
#> Adaptive Metropolis with target acceptance rate: 43.0
#> ----------------------------------------
#> 	Chain 1
#> ----------------------------------------
#> Sampling ... 
#> Batch: 10 of 10, 100.00%

summary(out, level = 'both')
#> 
#> Call:
#> sfJSDM(formula = ~occ.cov, data = data.list, inits = inits.list, 
#>     priors = prior.list, tuning = tuning.list, cov.model = "exponential", 
#>     NNGP = TRUE, n.neighbors = 5, search.type = "cb", n.factors = 3, 
#>     n.batch = n.batch, batch.length = batch.length, accept.rate = 0.43, 
#>     n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = 100, 
#>     n.thin = 1)
#> 
#> Samples per Chain: 250
#> Burn-in: 100
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 150
#> Run Time (min): 0.0034
#> 
#> ----------------------------------------
#> 	Community Level
#> ----------------------------------------
#> Means (logit scale): 
#>               Mean     SD    2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.1007 0.3342 -0.4977 0.1030 0.7540   NA  42
#> occ.cov     0.2026 0.3276 -0.4323 0.2034 0.7658   NA  64
#> 
#> Variances (logit scale): 
#>               Mean     SD   2.5%    50%  97.5% Rhat ESS
#> (Intercept) 0.5499 0.8968 0.0521 0.2857 2.7510   NA  53
#> occ.cov     0.3043 0.6094 0.0377 0.1507 1.0189   NA  45
#> 
#> ----------------------------------------
#> 	Species Level
#> ----------------------------------------
#> Estimates (logit scale): 
#>                    Mean     SD    2.5%     50%  97.5% Rhat ESS
#> (Intercept)-sp1  0.5571 0.4246 -0.1451  0.5193 1.6090   NA  39
#> (Intercept)-sp2  0.3407 0.3506 -0.3803  0.3308 1.0841   NA  75
#> (Intercept)-sp3  0.0657 0.4983 -0.8131  0.0648 1.2181   NA  47
#> (Intercept)-sp4 -0.2450 0.4318 -1.0940 -0.2119 0.5054   NA  45
#> (Intercept)-sp5 -0.3054 0.4211 -1.1914 -0.2870 0.4447   NA  65
#> occ.cov-sp1      0.0387 0.3622 -0.7727  0.0739 0.6604   NA  69
#> occ.cov-sp2      0.2206 0.3287 -0.4796  0.2140 0.8650   NA  77
#> occ.cov-sp3      0.2236 0.3007 -0.3038  0.2575 0.7830   NA  82
#> occ.cov-sp4      0.4315 0.3682 -0.3728  0.4270 1.1493   NA  48
#> occ.cov-sp5      0.1062 0.3345 -0.5790  0.1472 0.7096   NA  81
#> 
#> ----------------------------------------
#> 	Spatial Covariance
#> ----------------------------------------
#>          Mean     SD   2.5%     50%   97.5% Rhat ESS
#> phi-1 14.1144 7.9703 3.7721 13.0015 28.4244   NA   8
#> phi-2 14.6929 7.4450 3.8134 13.0415 27.8781   NA  15
#> phi-3 15.3113 6.8360 5.2080 14.4169 27.5695   NA  19

# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0, verbose = FALSE)