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-4 (2026-05-28) 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.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))## 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%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.713007 0.07197 0.023991 0.046799
#> Great.tit forest -0.112864 0.03747 0.012489 0.025531
#> Great.tit elev -0.182750 0.02600 0.008666 0.008666
#> Blue.tit Int 0.459333 0.06138 0.020459 0.043076
#> Blue.tit forest -0.114337 0.04538 0.015127 0.015127
#> Blue.tit elev -0.167298 0.03661 0.012204 0.012204
#> Coal.tit Int 0.843922 0.11017 0.036723 0.069351
#> Coal.tit forest 0.047039 0.01344 0.004478 0.002778
#> Coal.tit elev -0.078690 0.03501 0.011669 0.024495
#> Crested.tit Int 0.555141 0.11395 0.037983 0.074773
#> Crested.tit forest 0.008411 0.01333 0.004444 0.004444
#> Crested.tit elev -0.112999 0.02480 0.008267 0.008267
#> Marsh.tit Int 0.304017 0.12228 0.040759 0.094867
#> Marsh.tit forest -0.139085 0.03373 0.011243 0.021937
#> Marsh.tit elev -0.148862 0.02989 0.009962 0.009962
#> Willow.tit Int 0.158287 0.08081 0.026937 0.050971
#> Willow.tit forest 0.095235 0.03085 0.010283 0.013375
#> Willow.tit elev 0.071009 0.03489 0.011631 0.020506
#>
#> 2. Quantiles for each variable:
#>
#> 2.5% 25% 50% 75% 97.5%
#> Great.tit Int 0.57786 0.690634 0.72883 0.75181 0.78604
#> Great.tit forest -0.18151 -0.129893 -0.08658 -0.08516 -0.08428
#> Great.tit elev -0.21705 -0.199097 -0.18684 -0.16828 -0.13938
#> Blue.tit Int 0.35220 0.422162 0.47817 0.50766 0.51569
#> Blue.tit forest -0.18321 -0.122868 -0.11678 -0.09827 -0.03970
#> Blue.tit elev -0.20490 -0.192683 -0.16438 -0.15909 -0.09786
#> Coal.tit Int 0.63107 0.837738 0.88874 0.89382 0.93607
#> Coal.tit forest 0.02923 0.040227 0.04492 0.05094 0.07133
#> Coal.tit elev -0.12202 -0.099300 -0.08961 -0.05570 -0.02682
#> Crested.tit Int 0.34779 0.499882 0.59500 0.63145 0.66915
#> Crested.tit forest -0.01220 -0.003127 0.01376 0.01607 0.02597
#> Crested.tit elev -0.15500 -0.120830 -0.11763 -0.08834 -0.08377
#> Marsh.tit Int 0.10603 0.232507 0.35158 0.38287 0.45381
#> Marsh.tit forest -0.18329 -0.163466 -0.13848 -0.12029 -0.08857
#> Marsh.tit elev -0.17918 -0.169275 -0.15662 -0.12857 -0.09647
#> Willow.tit Int 0.01273 0.131715 0.18980 0.20260 0.24984
#> Willow.tit forest 0.04755 0.076114 0.10243 0.12042 0.13065
#> Willow.tit elev 0.02590 0.047114 0.06032 0.08772 0.12232
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.7350 0.12641 0.04214 0.08303
#> Blue.tit rho 0.6582 0.17365 0.05788 0.13847
#> Coal.tit rho 0.7161 0.11269 0.03756 0.08534
#> Crested.tit rho 0.7719 0.13658 0.04553 0.08870
#> Marsh.tit rho 0.7789 0.12705 0.04235 0.08203
#> Willow.tit rho 0.9257 0.04507 0.01502 0.02977
#>
#> 2. Quantiles for each variable:
#>
#> 2.5% 25% 50% 75% 97.5%
#> Great.tit rho 0.5589 0.6756 0.6994 0.8402 0.9081
#> Blue.tit rho 0.3990 0.5314 0.6569 0.7641 0.8773
#> Coal.tit rho 0.5571 0.6297 0.7430 0.8266 0.8362
#> Crested.tit rho 0.5452 0.7497 0.7981 0.8472 0.9308
#> Marsh.tit rho 0.5622 0.7414 0.8205 0.8228 0.9400
#> Willow.tit rho 0.8573 0.8937 0.9172 0.9648 0.9841par(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)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