Skip to contents

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

Usage

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

Arguments

object

an object of class svcPGBinom

X.0

the design matrix of covariates at the prediction locations. Note that for spatially-varying coefficients models the order of covariates in X.0 must be the same as the order of covariates specified in the model formula. This should include a column of 1s for the intercept if an intercept is included in the model. If unstructured 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 svcPGBinom. Columns should correspond to the order of how covariates were specified in the corresponding formula argument of svcPGBinom. Column names of the random effects must match the name of the random effects, if specified in the corresponding formula argument of svcPGBinom.

coords.0

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

weights.0

a numeric vector containing the binomial weights (i.e., the total number of Bernoulli trials) at each site. If weights.0 is not specified, we assume 1 trial at each site (i.e., presence/absence).

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.

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.

n.report

the interval to report sampling progress.

...

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.svcPGBinom consisting of:

psi.0.samples

a coda object of posterior predictive samples for the binomial probability values.

y.0.samples

a coda object of posterior predictive samples for the binomial data.

w.0.samples

a three-dimensional array of posterior predictive samples for the spatial random effects, with dimensions corresponding to MCMC iteration, coefficient, and site.

run.time

execution time reported using proc.time().

Examples

set.seed(1000)
# Sites
J.x <- 10
J.y <- 10
J <- J.x * J.y
# Binomial weights
weights <- sample(10, J, replace = TRUE)
beta <- c(0, 0.5, -0.2, 0.75)
p <- length(beta)
# No unstructured random effects
psi.RE <- list()
# Spatial parameters
sp <- TRUE
# Two spatially-varying covariates. 
svc.cols <- c(1, 2)
p.svc <- length(svc.cols)
cov.model <- "exponential"
sigma.sq <- runif(p.svc, 0.4, 1.5)
phi <- runif(p.svc, 3/1, 3/0.2)

# Simulate the data  
dat <- simBinom(J.x = J.x, J.y = J.y, weights = weights, beta = beta, 
                psi.RE = psi.RE, sp = sp, svc.cols = svc.cols, 
                cov.model = cov.model, sigma.sq = sigma.sq, phi = phi)

# Binomial data
y <- dat$y
# Covariates
X <- dat$X
# Spatial coordinates
coords <- dat$coords

# Subset data for prediction if desired
pred.indx <- sample(1:J, round(J * .25), replace = FALSE)
y.0 <- y[pred.indx, drop = FALSE]
X.0 <- X[pred.indx, , drop = FALSE]
coords.0 <- coords[pred.indx, ]
y <- y[-pred.indx, drop = FALSE]
X <- X[-pred.indx, , drop = FALSE]
coords <- coords[-pred.indx, ]
weights.0 <- weights[pred.indx]
weights <- weights[-pred.indx]

# Package all data into a list
# Covariates
covs <- cbind(X)
colnames(covs) <- c('int', 'cov.1', 'cov.2', 'cov.3')

# Data list bundle
data.list <- list(y = y, 
                  covs = covs,
                  coords = coords, 
                  weights = weights)
# Priors
prior.list <- list(beta.normal = list(mean = 0, var = 2.72), 
                   sigma.sq.ig = list(a = 2, b = 1), 
                   phi.unif = list(a = 3 / 1, b = 3 / 0.1)) 

# Starting values
inits.list <- list(beta = 0, alpha = 0,
                   sigma.sq = 1, phi = phi)
# Tuning
tuning.list <- list(phi = 1) 

n.batch <- 10
batch.length <- 25
n.burn <- 100
n.thin <- 1

out <- svcPGBinom(formula = ~ cov.1 + cov.2 + cov.3, 
                  svc.cols = c(1, 2),
                  data = data.list, 
                  n.batch = n.batch, 
                  batch.length = batch.length, 
                  inits = inits.list, 
                  priors = prior.list,
                  accept.rate = 0.43, 
                  cov.model = "exponential", 
                  tuning = tuning.list, 
                  n.omp.threads = 1, 
                  verbose = TRUE, 
                  NNGP = TRUE, 
                  n.neighbors = 5,
                  n.report = 2, 
                  n.burn = n.burn, 
                  n.thin = n.thin, 
                  n.chains = 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 NNGP Binomial model with Polya-Gamma latent
#> variable fit with 75 sites.
#> 
#> Samples per chain: 250 (10 batches of length 25)
#> Burn-in: 100 
#> Thinning Rate: 1 
#> Number of Chains: 1 
#> Total Posterior Samples: 150 
#> 
#> Number of spatially-varying coefficients: 2 
#> Using the exponential spatial correlation model.
#> 
#> 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: 2 of 10, 20.00%
#> 	Coefficient	Parameter	Acceptance	Tuning
#> 	1		phi		72.0		1.01005
#> 	2		phi		64.0		1.03045
#> -------------------------------------------------
#> Batch: 4 of 10, 40.00%
#> 	Coefficient	Parameter	Acceptance	Tuning
#> 	1		phi		64.0		1.03045
#> 	2		phi		60.0		1.05127
#> -------------------------------------------------
#> Batch: 6 of 10, 60.00%
#> 	Coefficient	Parameter	Acceptance	Tuning
#> 	1		phi		48.0		1.05127
#> 	2		phi		64.0		1.07251
#> -------------------------------------------------
#> Batch: 8 of 10, 80.00%
#> 	Coefficient	Parameter	Acceptance	Tuning
#> 	1		phi		48.0		1.07251
#> 	2		phi		92.0		1.09417
#> -------------------------------------------------
#> Batch: 10 of 10, 100.00%

summary(out)
#> 
#> Call:
#> svcPGBinom(formula = ~cov.1 + cov.2 + cov.3, data = data.list, 
#>     inits = inits.list, priors = prior.list, tuning = tuning.list, 
#>     svc.cols = c(1, 2), cov.model = "exponential", NNGP = TRUE, 
#>     n.neighbors = 5, n.batch = n.batch, batch.length = batch.length, 
#>     accept.rate = 0.43, n.omp.threads = 1, verbose = TRUE, n.report = 2, 
#>     n.burn = n.burn, n.thin = n.thin, n.chains = 1)
#> 
#> Samples per Chain: 250
#> Burn-in: 100
#> Thinning Rate: 1
#> Number of Chains: 1
#> Total Posterior Samples: 150
#> Run Time (min): 0.0066
#> 
#> Occurrence (logit scale): 
#>                Mean     SD    2.5%     50%   97.5% Rhat ESS
#> (Intercept) -0.1358 0.2418 -0.6392 -0.1234  0.2544   NA   8
#> cov.1        0.3990 0.1820  0.0374  0.4094  0.7315   NA  46
#> cov.2       -0.2776 0.1297 -0.5530 -0.2818 -0.0607   NA  35
#> cov.3        0.7719 0.1514  0.5309  0.7617  1.0844   NA  59
#> 
#> Spatial Covariance: 
#>                         Mean     SD   2.5%     50%   97.5% Rhat ESS
#> sigma.sq-(Intercept)  0.6638 0.3332 0.1821  0.5766  1.4302   NA  10
#> sigma.sq-cov.1        0.4605 0.2463 0.1541  0.3960  1.0536   NA  13
#> phi-(Intercept)       6.6234 3.2495 3.0790  6.2252 13.3789   NA  11
#> phi-cov.1            19.6955 6.6160 4.8037 20.4743 28.8445   NA   9

# Predict at new locations ------------------------------------------------
out.pred <- predict(out, X.0, coords.0, weights.0, verbose = FALSE)
str(out.pred)
#> List of 6
#>  $ w.0.samples  : num [1:150, 1:2, 1:25] 1.5303 1.4206 -0.6834 -0.0571 0.8027 ...
#>  $ psi.0.samples: 'mcmc' num [1:150, 1:25] 0.4261 0.3072 0.0603 0.4586 0.3098 ...
#>   ..- attr(*, "mcpar")= num [1:3] 1 150 1
#>  $ y.0.samples  : 'mcmc' num [1:150, 1:25] 5 3 0 4 6 1 2 2 5 7 ...
#>   ..- attr(*, "mcpar")= num [1:3] 1 150 1
#>  $ run.time     : 'proc_time' Named num [1:5] 0.09 0.051 0.035 0 0
#>   ..- attr(*, "names")= chr [1:5] "user.self" "sys.self" "elapsed" "user.child" ...
#>  $ call         : language predict.svcPGOcc(object = object, X.0 = X.0, coords.0 = coords.0, weights.0 = weights.0,      n.omp.threads = n.o| __truncated__ ...
#>  $ object.class : chr "svcPGBinom"
#>  - attr(*, "class")= chr "predict.svcPGOcc"