Introduction to the multiocc package

library(interp)
library(MCMCpack)
#> Loading required package: coda
#> Loading required package: MASS
#> 
#> Attaching package: 'MASS'
#> The following object is masked from 'package:interp':
#> 
#>     area
#> ##
#> ## Markov Chain Monte Carlo Package (MCMCpack)
#> ## Copyright (C) 2003-2026 Andrew D. Martin, Kevin M. Quinn, and Jong Hee Park
#> ##
#> ## Support provided by the U.S. National Science Foundation
#> ## (Grants SES-0350646 and SES-0350613)
#> ##
library(tmvtnorm)
#> Loading required package: mvtnorm
#> Loading required package: Matrix
#> Loading required package: stats4
#> Loading required package: gmm
#> Loading required package: sandwich
library(truncnorm)
library(multiocc)
library(MASS)
library(corrplot)
#> corrplot 0.95 loaded
library(fields)
#> Loading required package: spam
#> Spam version 2.11-3 (2026-01-05) is loaded.
#> Type 'help( Spam)' or 'demo( spam)' for a short introduction 
#> and overview of this package.
#> Help for individual functions is also obtained by adding the
#> suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
#> 
#> Attaching package: 'spam'
#> The following object is masked from 'package:stats4':
#> 
#>     mle
#> The following object is masked from 'package:Matrix':
#> 
#>     det
#> The following objects are masked from 'package:mvtnorm':
#> 
#>     rmvnorm, rmvt
#> The following objects are masked from 'package:base':
#> 
#>     backsolve, forwardsolve
#> Loading required package: viridisLite
#> Loading required package: RColorBrewer
#> 
#> Try help(fields) to get started.
data(detection)
data(occupancy)
data(coords)
DataNames <- list("species"=colnames(detection)[4:9],
             "detection"=c("duration"),"occupancy"=c("forest","elev"))
model.input <- multioccbuild(detection, occupancy, coords, DataNames, threshold = 15000)
#> Warning: Rows in detection with missing covariates have been removed for purposes of fitting the model, but the site/season combination is retained in occupancy and therefore predictions will be outputted.

Perform some exploratory data analysis

par(mfrow=c(1,3))
hist(occupancy$forest, main="", xlab="Forest")
hist(occupancy$elev, main="", xlab="Elevation")
hist(detection$duration, main="", xlab="Duration")


par(mfrow=c(3,2), mar=c(3,3,3,1))
quilt.plot(coords[,2:3], occupancy$forest[1:267], main="Forest Cover", zlim=c(-1.5,3))
fit <- Tps(coords[,2:3], occupancy$forest[1:267])
out <- predictSurface(fit, df=100)
image.plot(out, main="Forest Cover (interpolated)", zlim=c(-1.5,2))

quilt.plot(coords[,2:3], occupancy$elev[1:267], main="Elevation", zlim=c(-1.5,3.5))
fit <- Tps(coords[,2:3], occupancy$elev[1:267])
out <- predictSurface(fit, df=100)
image.plot(out, main="Elevation (interpolated)", zlim=c(-1.5,2))

quilt.plot(coords[,2:3], detection$duration[1:267], main="Duration", zlim=c(-2.5,3))
fit <- Tps(coords[,2:3], detection$duration[1:267])
out <- predictSurface(fit, df=100)
image.plot(out, main="Duration (Survey 1)", zlim=c(-2.5,2.5))

A short run for demonstration purposes

## Shorter run for demonstration purposes.
## library(tmvtnorm)
mcmc.out <- GibbsSampler(M.iter=10, M.burn=1, M.thin=1, model.input, q=10, sv=FALSE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%

A longer run (not executed) for scientific results

mcmc.out <- GibbsSampler(M.iter=50000, M.burn=20000, M.thin=1, model.input, q=10, sv=FALSE)

Can summarize output

summary(mcmc.out$samples$alpha)
#> 
#> Iterations = 1:9
#> Thinning interval = 1 
#> Number of chains = 1 
#> Sample size per chain = 9 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>                        Mean      SD Naive SE Time-series SE
#> Great.tit Int       0.67685 0.08350 0.027834       0.054377
#> Great.tit forest   -0.09610 0.02727 0.009090       0.009090
#> Great.tit elev     -0.14975 0.04430 0.014768       0.026787
#> Blue.tit Int        0.44738 0.07733 0.025777       0.025777
#> Blue.tit forest    -0.08451 0.02269 0.007565       0.007565
#> Blue.tit elev      -0.18559 0.04679 0.015595       0.030371
#> Coal.tit Int        0.78209 0.11151 0.037170       0.074384
#> Coal.tit forest    -0.01991 0.02047 0.006822       0.012722
#> Coal.tit elev      -0.09226 0.03082 0.010273       0.020919
#> Crested.tit Int     0.55876 0.11075 0.036918       0.076343
#> Crested.tit forest -0.04567 0.04742 0.015807       0.032240
#> Crested.tit elev   -0.07466 0.03652 0.012172       0.012172
#> Marsh.tit Int       0.31092 0.09203 0.030677       0.055269
#> Marsh.tit forest   -0.04797 0.02279 0.007595       0.007595
#> Marsh.tit elev     -0.22338 0.09767 0.032557       0.077301
#> Willow.tit Int      0.09057 0.06786 0.022620       0.039116
#> Willow.tit forest   0.08367 0.01744 0.005814       0.005814
#> Willow.tit elev     0.03986 0.03580 0.011933       0.015008
#> 
#> 2. Quantiles for each variable:
#> 
#>                        2.5%      25%      50%        75%    97.5%
#> Great.tit Int       0.52131  0.66320  0.70977  0.7223741  0.75723
#> Great.tit forest   -0.13706 -0.11002 -0.09497 -0.0904783 -0.04947
#> Great.tit elev     -0.20130 -0.18542 -0.15813 -0.1252374 -0.07509
#> Blue.tit Int        0.31764  0.38962  0.46177  0.4889005  0.55158
#> Blue.tit forest    -0.11226 -0.09740 -0.09009 -0.0827571 -0.04213
#> Blue.tit elev      -0.23439 -0.21441 -0.20894 -0.1549234 -0.10154
#> Coal.tit Int        0.59039  0.73418  0.79962  0.8481661  0.91982
#> Coal.tit forest    -0.04620 -0.03326 -0.02437  0.0005934  0.01026
#> Coal.tit elev      -0.13625 -0.10871 -0.10199 -0.0758144 -0.04674
#> Crested.tit Int     0.38245  0.50706  0.57020  0.6231967  0.70578
#> Crested.tit forest -0.09056 -0.07926 -0.06161 -0.0402808  0.03736
#> Crested.tit elev   -0.11693 -0.10264 -0.07998 -0.0646828 -0.00937
#> Marsh.tit Int       0.15356  0.26180  0.33033  0.3781374  0.41793
#> Marsh.tit forest   -0.07886 -0.06423 -0.04814 -0.0331432 -0.01516
#> Marsh.tit elev     -0.30610 -0.29629 -0.27002 -0.1567000 -0.05583
#> Willow.tit Int     -0.03889  0.05858  0.11954  0.1231798  0.15621
#> Willow.tit forest   0.05963  0.07481  0.08266  0.0911714  0.10975
#> Willow.tit elev    -0.01934  0.02520  0.05027  0.0631018  0.08047
summary(mcmc.out$samples$rho)
#> 
#> Iterations = 1:9
#> Thinning interval = 1 
#> Number of chains = 1 
#> Sample size per chain = 9 
#> 
#> 1. Empirical mean and standard deviation for each variable,
#>    plus standard error of the mean:
#> 
#>                   Mean      SD Naive SE Time-series SE
#> Great.tit rho   0.8322 0.17105  0.05702        0.09978
#> Blue.tit rho    0.8293 0.10245  0.03415        0.03415
#> Coal.tit rho    0.5962 0.13438  0.04479        0.05355
#> Crested.tit rho 0.9193 0.05637  0.01879        0.01879
#> Marsh.tit rho   0.9147 0.06801  0.02267        0.04032
#> Willow.tit rho  0.9380 0.06482  0.02161        0.05126
#> 
#> 2. Quantiles for each variable:
#> 
#>                   2.5%    25%    50%    75%  97.5%
#> Great.tit rho   0.5122 0.7274 0.8913 0.9218 0.9946
#> Blue.tit rho    0.6311 0.7990 0.8527 0.8943 0.9251
#> Coal.tit rho    0.3718 0.5063 0.6233 0.7094 0.7308
#> Crested.tit rho 0.8176 0.8951 0.9380 0.9445 0.9823
#> Marsh.tit rho   0.7882 0.8831 0.9365 0.9542 0.9819
#> Willow.tit rho  0.8284 0.8954 0.9770 0.9874 0.9894

Visualize correlation matrix

par(mfrow=c(1,1), mar=c(3,3,3,1))
sigout <- mcmc.out$samples$sig
Sig <- matrix(colMeans(sigout),6,6)
SpeciesCor <- cov2cor(Sig)
rownames(SpeciesCor) <- DataNames$species
colnames(SpeciesCor) <- DataNames$species
corrplot::corrplot(SpeciesCor)

Make predictions from fitted model

y.agg1 <-  aggregate(model.input$y[,1], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot1 <- 1*(y.agg1$x>0)

y.agg2 <- aggregate(model.input$y[,2], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot2 <- 1*(y.agg2$x>0)

y.agg3 <- aggregate(model.input$y[,3], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot3 <- 1*(y.agg3$x>0)

y.agg4 <- aggregate(model.input$y[,4], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot4 <- 1*(y.agg4$x>0)

y.agg5 <- aggregate(model.input$y[,5], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot5 <- 1*(y.agg5$x>0)

y.agg6 <- aggregate(model.input$y[,6], by=list(model.input$detection.info$siteID, 
                                              model.input$detection.info$season), FUN=sum, na.rm=TRUE)
y.plot6 <- 1*(y.agg6$x>0)

for (yr in c(1,4,7,10)){
  print(yr)

  range <- which(model.input$occupancy.info$season == yr)

  psiout <- mcmc.out$samples$psi
  #pout <- mcmc.out$p
  dim(psiout)

  psi1 <- apply(psiout[,0*2670+range],2,mean)
  psi2 <- apply(psiout[,1*2670+range],2,mean)
  psi3 <- apply(psiout[,2*2670+range],2,mean)
  psi4 <- apply(psiout[,3*2670+range],2,mean)
  psi5 <- apply(psiout[,4*2670+range],2,mean)
  psi6 <- apply(psiout[,5*2670+range],2,mean)

  par(mfrow=c(3,2), mar=c(1,3,3,1))
  fit <- Tps(coords[1:267,2:3], psi1)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Great Tit", zlim=c(-0.01,1.01))
  mtext(paste("Year",yr), side=3, line=-2, outer=TRUE)

  y.plot1.in <- y.plot1[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot1.in==1),2:3])

  fit <- Tps(coords[1:267,2:3], psi2)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Blue Tit", zlim=c(-0.01,1.01))

  y.plot2.in <- y.plot2[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot2.in==1),2:3])

  fit <- Tps(coords[1:267,2:3], psi3)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Coal Tit", zlim=c(-0.01,1.01))

  y.plot3.in <- y.plot3[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot3.in==1),2:3])

  fit <- Tps(coords[1:267,2:3], psi4)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Crested Tit", zlim=c(-0.01,1.01))

  y.plot4.in <- y.plot4[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot4.in==1),2:3])

  fit <- Tps(coords[1:267,2:3], psi5)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Marsh Tit", zlim=c(-0.01,1.01))

  y.plot5.in <- y.plot5[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot5.in==1),2:3])

  fit <- Tps(coords[1:267,2:3], psi6)
  out <- predictSurface(fit, df=100)
  image.plot(out, main="Willow Tit", zlim=c(-0.01,1.01))

  y.plot6.in <- y.plot6[which(model.input$occupancy.info$season ==yr)]
  points(coords[which(y.plot6.in==1),2:3])
}
#> [1] 1

#> [1] 4

#> [1] 7

#> [1] 10