Fitting a Spatial Factor Multi-Species Occupancy Model

Data from: Plantas-Putumayo,

model
code
analysis
60->51 sites, 18 mammal species
Authors

German Forero

Robert Wallace

Galo Zapara-Rios

Emiliana Isasi-Catalá

Diego J. Lizcano

Published

August 16, 2025

Single-species occupancy models

Single-species occupancy models (MacKenzie et al., 2002) are widely used in ecology to estimate and predict the spatial distribution of a species from data collected in presence–absence repeated surveys. The popularity of these models stems from their ability to estimate the effects of environmental or management covariates on species occurrences, while accounting for false-negative errors in detection, which are common in surveys of natural populations.

Multi-species occupancy models

Multi-species occupancy models for estimating the effects of environmental covariates on several species occurrences while accounting for imperfect detection were build on the principle of the Single-species occupancy model, and developed more than 20 years ago.

Multi-species occupancy models are a powerful tool for combining information from multiple species to estimate both individual and community-level responses to management and environmental variables.

Objective

We want to asses the effect of protected areas on the occupancy of the species. We hypothesize that several mammal species have benefited of the conservation actions provided by protected areas, so we expect a decreases in occupancy from the interior of the protected area to the exterior as a function of the distance to the protected area border.

To account for several ecological factors we also tested and included elevation and forest integrity index as occupancy covariates.

We used the newly developed R package spOccupancy as approach to modelling the probability of occurrence of each species as a function of fixed effects of measured environmental covariates and random effects designed to account for unobserved sources of spatial dependence (spatial autocorrelation).

Code
library(grateful) # Facilitate Citation of R Packages
library(readxl) # Read Excel Files
library(DT) # A Wrapper of the JavaScript Library 'DataTables'
library(sf) # Simple Features for R
library(mapview) # Interactive Viewing of Spatial Data in R
library(maps) # Draw Geographical Maps
library(tmap) # Thematic Maps
library(terra) # Spatial Data Analysis
library(elevatr) # Access Elevation Data from Various APIs

# library(rjags) # Bayesian Graphical Models using MCMC 
library(bayesplot) # Plotting for Bayesian Models # Plotting for Bayesian Models
library(tictoc) # Functions for Timing R Scripts, as Well as Implementations of "Stack" and "StackList" Structures 
library(MCMCvis) # Tools to Visualize, Manipulate, and Summarize MCMC Output
library(coda) # Output Analysis and Diagnostics for MCMC
library(beepr) # Easily Play Notification Sounds on any Platform 
library(snowfall) # Easier Cluster Computing (Based on 'snow')

#library(ggmcmc)
library(camtrapR) # Camera Trap Data Management and Preparation 
library(spOccupancy) # Single-Species, Multi-Species, and Integrated Spatial Occupancy
library(tidyverse) # Easily Install and Load the 'Tidyverse'

Fitting a Multi-Species Spatial Occupancy Model

We use a more computationally efficient approach for fitting spatial multi-species occupancy models. This alternative approach is called a “spatial factor multi-species occupancy model”, and is described in depth in Doser, Finley, and Banerjee (2023). This newer approach also accounts for residual species correlations (i.e., it is a joint species distribution model with imperfect detection). The simulation results from Doser, Finley, and Banerjee (2023) show that this new alternative approach outperforms, or performs equally to spMsPGOcc(), while being substantially faster.

The Latent factor multi-species occupancy model is described in detail here

Data from Plantas Medicinales Putumayo

Here we use the table: - COL-18-Putumayo2023

Code
source("C:/CodigoR/WCS-CameraTrap/R/organiza_datos_v3.R")

AP_PlantasMed <- read_sf("C:/CodigoR/Occu_APs/shp/PlantasMedicinales/WDPA_WDOECM_May2025_Public_555511938_shp-polygons.shp")

FLII2016 <- rast("C:/CodigoR/WCS_2024/FLI/raster/FLII_final/FLII_2016.tif") 

# load data and make array_locID column
Col_18 <- loadproject("F:/WCS-CameraTrap/data/BDcorregidas/Colombia/COL-018-Putumayo2023_WCS_WI.xlsx") |> mutate(array_locID=paste("Col_18", locationID, sep="_"))
60 cameras in Cameras. 
 60 cameras in Deployment. 
 60 deployments in Deployment. 
 60 points in Deployment. 
 51 cameras in Images. 
 51 points in Images. 
[1] "dates ok"
year: 2024 
 Jaguar_Design: no year: 2023 
 Jaguar_Design: no 
Code
# get sites
Col_18_sites <- get.sites("F:/WCS-CameraTrap/data/BDcorregidas/Colombia/COL-018-Putumayo2023_WCS_WI.xlsx")



# get elevation map
elevation_18 <- rast(get_elev_raster(Col_18_sites, z = 10)) #z =1-14
# bb <-  st_as_sfc(st_bbox(elevation_17)) # make bounding box 




# extract covs using points and add to _sites
covs_Col_18_sites <- cbind(Col_18_sites, terra::extract(elevation_18, Col_18_sites))
# covs_Col_17_sites <- cbind(Col_17_sites, terra::extract(elevation_17, Col_17_sites))


# get which are in and out
covs_Col_18_sites$in_AP = st_intersects(covs_Col_18_sites, AP_PlantasMed, sparse = FALSE)
# covs_Col_17_sites$in_AP = st_intersects(covs_Col_17_sites, AP_Yasuni, sparse = FALSE)




# make a map
mapview (elevation_18, alpha=0.5) + 
  mapview (AP_PlantasMed, color = "green", col.regions = "green", alpha = 0.5) +
  mapview (covs_Col_18_sites, zcol = "in_AP", col.regions =c("red","blue"), burst = TRUE) 
Code
  # mapview (covs_Col_17_sites, zcol = "in_AP", col.regions =c("red","blue"), burst = TRUE) +

Data fom Bajo Madidi and Heath

Here we use the tables: - BOL-008a - BOL-008b - BOL14a - BOL14b

Tamshiyacu Tahuayo data

Here we use the tables: - PER-003_BD_ACRCTT_T0.xlsx - PER-002_BD_ACRTT-PILOTO.xlsx

Yasuni data

Here we use the tables Ecu-13, Ecu-17, Ecu-18 y Ecu-20

Llanganates data

Here we use the tables Ecu-14, Ecu-15 y Ecu-16.

Pacoche 2014 data

Here we use the tables Pacoche_DL_CT-RVP-2014

Camera trap operation data and detection history

Code
# 
# 
# 
# # Join 3 tables
# # fix count in ECU 13, 17, 22,
# Ecu_13$Count <- as.character(Ecu_13$Count)
# Ecu_17$Count <- as.character(Ecu_17$Count)
# Ecu_22$Count <- as.character(Ecu_22$Count)
# 
# Ecu_full <- Ecu_13 |> full_join(Ecu_17) |> 
#                       full_join(Ecu_18) |> 
#                       full_join(Ecu_20) |> 
# #                      full_join(Ecu_21) |> 
#                       full_join(Ecu_22)
# 
# ###### Bolivia
# 
# Bol_full <- Bol_Madidi_1  |> 
#    full_join(Bol_Madidi_2) |> 
#    full_join(Bol_Madidi_3) |> 
#    full_join(Bol_Madidi_4) 
# # change camera Id by point. two cameras in one
# Bol_full <- Bol_full |> mutate(Camera_Id=Point.x)
# 
# 
# # Ecu_18$Count <- as.numeric(Ecu_18$Count)
# Per_Tahuayo_piloto$Longitude <- as.numeric(Per_Tahuayo_piloto$Longitude)
# Per_Tahuayo_piloto$Latitude <- as.numeric(Per_Tahuayo_piloto$Latitude)
# 
# 
# Per_full <- Per_Tahuayo|> 
#   full_join(Per_Tahuayo_piloto) #|> 
#                       # full_join(Ecu_18) |> 
#                       # full_join(Ecu_20) |> 
#                       # full_join(Ecu_21) |> 
#                       # full_join(Ecu_22)
# 
# # rename camera id
# # Per_full$camid <- Per_full$`Camera_Id`
# 
# Ecu_full$Count <- as.numeric(Ecu_full$Count)
# Per_full$Count <- as.numeric(Per_full$Count)
# 
# 
# #################### Ecuador Llanganates
# 
# # Join 3 tables
# Ecu_Llanganates <- Ecu_14 |> full_join(Ecu_15) |> full_join(Ecu_16)
# 
# 
# # make columns equal to 46
# Ecu_full <- Ecu_full[,-47]
Col_18 <- Col_18[,-47]
# Ecu_Llanganates <- Ecu_Llanganates[,-c(50,49,48,47)]

##################################
data_full <- Col_18# rbind(Ecu_full, 
                   # Per_full,
                   # Bol_full,
                   # Col_18# ,
                   #Ecu_Llanganates)


# fix date format
# 
# Formatting a Date object
data_full$start_date <- as.Date(data_full$"start_date", "%Y/%m/%d")
data_full$start_date <- format(data_full$start_date, "%Y-%m-%d")

data_full$end_date <- as.Date(data_full$"end_date", "%Y/%m/%d")
data_full$end_date <- format(data_full$end_date, "%Y-%m-%d")

data_full$eventDate <- as.Date(data_full$"Date_Time_Captured", "%Y/%m/%d")
data_full$eventDate <- format(data_full$eventDate, "%Y-%m-%d")

# Per_full$eventDateTime <- ymd_hms(paste(Per_full$"photo_date", Per_full$"photo_time", sep=" "))
data_full$eventDateTime <- ymd_hms(data_full$"Date_Time_Captured")

###############################
# remove duplicated cameras
################################
# ind1 <- which(data_full$Camera_Id=="ECU-020-C0027")
# ind2 <- which(data_full$Camera_Id=="ECU-020-C0006")
# data_full <- data_full[-ind1,]
# data_full <- data_full[-ind2,]


# filter 2021 and make uniques
CToperation  <- data_full |> dplyr::group_by(Camera_Id) |> #(array_locID) |> 
                           mutate(minStart=start_date, maxEnd=end_date) |> distinct(Longitude, Latitude, minStart, maxEnd) |> dplyr::ungroup()
# remove one duplicated
# View(CToperation)
# CToperation <- CToperation[-15,]

# M3-19: M3-21: M4-10: M4-12: M4-18:  From Madidi
# CToperation <- CToperation[-c(379, 373, 365, 362, 357),]

# CToperation[231,3] <- "-4.482831" #Latitude
# CToperation[93,3] <- "-0.5548211" #Latitude
# CToperation[93,2] <- "-76.48333"

# duplicated in llanganates
# CToperation <- CToperation[-464,] 

### Check duplicated
# View(as.data.frame(sort(table(CToperation$Latitude))))
# View(as.data.frame(sort(table(CToperation$Longitude))))
# View(as.data.frame(table(CToperation$Camera_Id)))

# CToperation <- CToperation[-c(90,94),]
# Latitude -4.48283
# Latitude -0.554820969700813
# Longitude -76.4833290316164

# Generamos la matríz de operación de las cámaras

camop <- cameraOperation(CTtable= CToperation, # Tabla de operación
                         stationCol= "Camera_Id", # Columna que define la estación
                         setupCol= "minStart", #Columna fecha de colocación
                         retrievalCol= "maxEnd", #Columna fecha de retiro
                         #hasProblems= T, # Hubo fallos de cámaras
                         dateFormat= "%Y-%m-%d")#, #, # Formato de las fechas
                         #cameraCol="Camera_Id")
                         # sessionCol= "Year")

# Generar las historias de detección ---------------------------------------
## remove plroblem species

# Per_full$scientificName <- paste(Per_full$genus, Per_full$species, sep=" ")

#### remove NAs and setups
ind <- which(is.na(data_full$scientificName))
data_full <- data_full[-ind,]
# ind <- which(Per_full$scientificName=="NA NA")
# Per_full <- Per_full[-ind,]

# ind <- which(Per_full$scientificName=="Set up")
# Per_full <- Per_full[-ind,]
# 
# ind <- which(Per_full$scientificName=="Blank")
# Per_full <- Per_full[-ind,]
# 
# ind <- which(Per_full$scientificName=="Unidentifiable")
# Per_full <- Per_full[-ind,]


DetHist_list <- lapply(unique(data_full$scientificName), FUN = function(x) {
  detectionHistory(
    recordTable         = data_full, # abla de registros
    camOp                = camop, # Matriz de operación de cámaras
    stationCol           = "Camera_Id",
    speciesCol           = "scientificName",
    recordDateTimeCol    = "eventDateTime",
    recordDateTimeFormat  = "%Y-%m-%d %H:%M:%S",
    species              = x,     # la función reemplaza x por cada una de las especies
    occasionLength       = 7, # Colapso de las historias a 10 ías
    day1                 = "station", # "survey" a specific date, "station", #inicie en la fecha de cada survey
    datesAsOccasionNames = FALSE,
    includeEffort        = TRUE,
    scaleEffort          = FALSE,
    #unmarkedMultFrameInput=TRUE
    timeZone             = "America/Bogota" 
    )
  }
)

# names
names(DetHist_list) <- unique(data_full$scientificName)

# Finalmente creamos una lista nueva donde estén solo las historias de detección
ylist <- lapply(DetHist_list, FUN = function(x) x$detection_history)
# otra lista con effort scaled
efort <- lapply(DetHist_list, FUN = function(x) x$effort)

# number of observetions per sp, collapsed to 7 days
# lapply(ylist, sum, na.rm = TRUE)

Arrange spatial covariates

The standard EPSG code for a Lambert Azimuthal Equal-Area projection for South America is EPSG:10603 (WGS 84 / GLANCE South America). This projection is specifically designed for the continent and has its center located around the central meridian for the region. The units are meters.

FLII scores range from 0 (lowest integrity) to 10 (highest). Grantham discretized this range to define three broad illustrative categories: low (≤6.0); medium (>6.0 and <9.6); and high integrity (≥9.6).

Code
#transform coord data to Lambert Azimuthal Equal-Area
# AP_Tahuayo_UTM <- st_transform(AP_Tahuayo, "EPSG:10603")
# Convert to LINESTRING
# AP_Tahuayo_UTM_line <- st_cast(AP_Tahuayo_UTM,"MULTILINESTRING")# "LINESTRING")
#transform Yasuni to Lambert Azimuthal Equal-Area
# AP_Yasuni_UTM <- st_transform(AP_Yasuni, "EPSG:10603")
# Convert to LINESTRING
# AP_Yasuni_UTM_line <- st_cast(AP_Yasuni_UTM, "MULTILINESTRING")

#transform Yasuni to Lambert Azimuthal Equal-Area
# AP_Madidi_UTM <- st_transform(Madidi_NP, "EPSG:10603")
# Convert to LINESTRING
# AP_Madidi_UTM_line <- st_cast(AP_Madidi_UTM, "MULTILINESTRING")

# Plantas
AP_PlantasMed_UTM <- st_transform(AP_PlantasMed, "EPSG:10603")
# Convert to LINESTRING
AP_PlantasMed_UTM_line <- st_cast(AP_PlantasMed_UTM, "MULTILINESTRING")

#Llanganates
# AP_Llanganates_UTM <- st_transform(AP_Llanganates, "EPSG:10603")


############## sf AP Union 
AP_merged_sf_UTM <- AP_PlantasMed_UTM 
                     # st_union(
                     #         AP_Yasuni_UTM,
                     #         AP_Tahuayo_UTM,
                     #         AP_Madidi_UTM,
                     #         AP_PlantasMed_UTM,
                     #         AP_Llanganates_UTM
                     #         )

AP_merged_line <- st_cast(AP_merged_sf_UTM, to="MULTILINESTRING")


# make sf() from data table
data_full_sf <- CToperation |> 
    st_as_sf(coords = c("Longitude", "Latitude"), 
              crs = 4326)

# get the elevation point from AWS
elev_data <- elevatr::get_elev_point(locations = data_full_sf, src = "aws")
Mosaicing & Projecting
Note: Elevation units are in meters
Code
# extract elev and paste to table
data_full_sf$elev <- elev_data$elevation
str(data_full_sf$elev)
 num [1:51] 707 857 857 680 854 737 854 857 778 737 ...
Code
# extract in AP
data_full_sf$in_AP = as.factor(st_intersects(data_full_sf, AP_PlantasMed, sparse = FALSE))

in_AP <- as.numeric((st_drop_geometry(data_full_sf$in_AP)))

# extract FLII
data_full_sf$FLII <- terra::extract(FLII2016, data_full_sf)[,2]
str(data_full_sf$FLII)
 num [1:51] 8.58 9.52 9.52 8.44 8.73 ...
Code
# Replace all NAs with min flii in a numeric column
data_full_sf$FLII[is.na(data_full_sf$FLII)] <- min(data_full_sf$FLII, na.rm = TRUE)

# mapview(full_sites_14_15_16_sf, zcol = "in_AP", burst = TRUE)

# Transform coord to Lambert Azimuthal Equal-Area
data_full_sf_UTM <- st_transform(data_full_sf, "EPSG:10603")


coords <- st_coordinates(data_full_sf_UTM)
#str(coords)

#### fix duplicated coord
# -1840583.53296873  en x
# -1842515.36969736  en x
# 1547741.44311964  en y
# 1541202.24796904 en y

# which(coords[,1]=="-1840583.53296873")
# which(coords[,1]=="-1842515.36969736")
# which(coords[,2]=="1547741.44311964")
# which(coords[,2]=="1541202.24796904")

# make Ecu_14_15_16 an sf object
#    cam_sf <- st_as_sf(Ecu_14_15_16, coords = c("lon","lat"))   #crs="EPSG:4326")
    #--- set CRS ---#
#    st_crs(cam_sf) <- 4326




# Calculate the distance
#multiplic <- full_sites_14_15_16_sf_UTM |> mutate(multiplic= as.numeric(in_AP)) 
multiplic=ifelse(data_full_sf_UTM$in_AP=="TRUE",-1,1)
data_full_sf_UTM$border_dist <- as.numeric(st_distance(data_full_sf_UTM, AP_PlantasMed_UTM_line) * multiplic )
# print(border_dist)

# convert true false to inside 1, outside 0
data_full_sf_UTM <- data_full_sf_UTM |>
  mutate(in_AP = case_when(
    str_detect(in_AP, "TRUE") ~ 1, # "inside_AP",
    str_detect(in_AP, "FALSE") ~ 0 #"outside_AP"
  )) |> mutate(in_AP=as.factor(in_AP))


hist(data_full_sf_UTM$border_dist)

Code
hist(data_full_sf_UTM$elev)

Prepare the model

TipData in a 3D array

The data must be placed in a three-dimensional array with dimensions corresponding to species, sites, and replicates in that order.

The function sfMsPGOcc

Fits multi-species spatial occupancy models with species correlations (i.e., a spatially-explicit joint species distribution model with imperfect detection). We use Polya-Gamma latent variables and a spatial factor modeling approach. Currently, models are implemented using a Nearest Neighbor Gaussian Process.

Code
# Detection-nondetection data ---------
# Species of interest, can select individually
# curr.sp <- sort(unique(Ecu_14_15_16$.id))# c('BAWW', 'BLJA', 'GCFL')
# sort(names(DetHist_list))
selected.sp <-  c(
# "Atelocynus microtis" ,
"Cabassous unicinctus",
# "Coendou prehensilis" ,
"Coendou" ,
"Cuniculus paca",
"Dasyprocta fuliginosa",      
"Dasypus novemcinctus" ,    
"Didelphis marsupialis", 
"Eira barbara",  
"Galictis vittata",
#"Herpailurus yagouaroundi",
"Leopardus",    
# "Leopardus tigrinus" ,
# "Leopardus wiedii",         
"Mazama americana",
# "Mazama nemorivaga",
#"Mitu tuberosum" ,
"Myoprocta pratti",
# "Myrmecophaga tridactyla",
"Nasua nasua" ,
# "Mazama americana",         
# "Myotis myotis",           
# "Nasua narica",             
# "Odocoileus virginianus",   
"Panthera onca" ,
# "Procyon cancrivorus" ,
"Pecari tajacu",    
# "Penelope jacquacu" ,
# "Priodontes maximus" ,
"Procyon cancrivorous",      
# "Psophia leucoptera",
"Puma concolor" ,
"Speothos venaticus",
# "Puma yagouaroundi",        
# "Rattus rattus" ,
# "Roedor sp.",
# "Sciurus sp.",       
# "Sus scrofa",               
# "Sylvilagus brasiliensis",  
"Tamandua tetradactyla",   
#"Tapirus pinchaque" ,
"Tapirus terrestris"
# "Tayassu pecari"
#"Tinamus major"            
              )

# y.msom <- y[which(sp.codes %in% selected.sp), , ]
# str(y.msom)

# Use selection
y.selected <- ylist[selected.sp]   

#####################################
#### three-dimensional array with dimensions corresponding to:
#### species, sites, and replicates
#####################################

# 1. Load the abind library to make arrays easily 
library(abind)
my_array_abind <- abind(y.selected, # start from list
                        along = 3, # 3D array
                        use.first.dimnames=TRUE) # keep names

# Transpose the array to have:
# species, sites, and sampling occasions in that order
# The new order is (3rd dim, 1st dim, 2nd dim)
transposed_array <- aperm(my_array_abind, c(3, 1, 2))

#### site covs
sitecovs <- as.data.frame(st_drop_geometry(
                    data_full_sf_UTM[,5:8]))

 sitecovs[, 1] <- as.vector((sitecovs[,1]))   # scale numeric covariates
 sitecovs[, 1] <- as.numeric((sitecovs[,2]))   # scale numeric covariates
 sitecovs[, 3] <- as.vector((sitecovs[,3]))   # scale numeric covariates
 sitecovs[, 4] <- as.vector((sitecovs[,4]))   # scale numeric covariates
 # sitecovs$fact <- factor(c("A", "A", "B"))    # categorical covariate

names(sitecovs) <- c("elev", "in_AP", "FLII", "border_dist")

# check consistancy equal number of spatial covariates and rows in data
# identical(nrow(ylist[[1]]), nrow(covars)) 

# Base de datos para los análisis -----------------------------------------

# match the names to "y"  "occ.covs" "det.covs" "coords" 
data_list <- list(y = transposed_array, # Historias de detección
                  occ.covs = sitecovs, # covs de sitio
                  det.covs  = list(effort = DetHist_list[[1]]$effort),
                  coords = st_coordinates(data_full_sf_UTM)
                  )  # agregamos el esfuerzo de muestreo como covariable de observación

Running the model

We let spOccupancy set the initial values by default based on the prior distributions.

Code
# Running the model

# 3. 1 Modelo multi-especie  -----------------------------------------

# 2. Model fitting --------------------------------------------------------
# Fit a non-spatial, multi-species occupancy model. 
out.null <- msPGOcc(occ.formula = ~ scale(elev) +
                               scale(border_dist) + 
                               scale(FLII) , 
               det.formula = ~ scale(effort) , # Ordinal.day + I(Ordinal.day^2) + Year
                 data = data_list, 
                 n.samples = 6000, 
                 n.thin = 10, 
                 n.burn = 1000, 
                 n.chains = 3,
                 n.report = 1000);beep(sound = 4)
----------------------------------------
    Preparing to run the model
----------------------------------------
No prior specified for beta.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for alpha.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for tau.sq.beta.ig.
Setting prior shape to 0.1 and prior scale to 0.1
No prior specified for tau.sq.alpha.ig.
Setting prior shape to 0.1 and prior scale to 0.1
z is not specified in initial values.
Setting initial values based on observed data
beta.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
alpha.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
tau.sq.beta is not specified in initial values.
Setting initial values to random values between 0.5 and 10
tau.sq.alpha is not specified in initial values.
Setting to initial values to random values between 0.5 and 10
beta is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
alpha is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
----------------------------------------
    Model description
----------------------------------------
Multi-species Occupancy Model with Polya-Gamma latent
variable fit with 51 sites and 18 species.

Samples per Chain: 6000 
Burn-in: 1000 
Thinning Rate: 10 
Number of Chains: 3 
Total Posterior Samples: 1500 

Source compiled with OpenMP support and model fit using 1 thread(s).

----------------------------------------
    Chain 1
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
----------------------------------------
    Chain 2
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
----------------------------------------
    Chain 3
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
Code
summary(out.null, level = 'community')

Call:
msPGOcc(occ.formula = ~scale(elev) + scale(border_dist) + scale(FLII), 
    det.formula = ~scale(effort), data = data_list, n.samples = 6000, 
    n.report = 1000, n.burn = 1000, n.thin = 10, n.chains = 3)

Samples per Chain: 6000
Burn-in: 1000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 1500
Run Time (min): 0.837

----------------------------------------
    Community Level
----------------------------------------
Occurrence Means (logit scale): 
                      Mean     SD    2.5%     50%  97.5%   Rhat ESS
(Intercept)        -1.0769 0.5961 -2.1822 -1.0963 0.1722 1.0221 387
scale(elev)         0.0462 0.2525 -0.4465  0.0555 0.5528 1.0169 803
scale(border_dist) -0.0010 0.2531 -0.4923 -0.0044 0.4772 1.0038 534
scale(FLII)         0.1187 0.3189 -0.4549  0.0877 0.8066 1.0038 613

Occurrence Variances (logit scale): 
                     Mean     SD   2.5%    50%   97.5%   Rhat ESS
(Intercept)        4.2682 2.8290 1.2668 3.5861 11.3341 1.0261 251
scale(elev)        0.2649 0.3048 0.0349 0.1686  1.0144 1.0156 535
scale(border_dist) 0.2272 0.2568 0.0320 0.1535  0.8735 1.0119 628
scale(FLII)        0.8390 0.9101 0.0890 0.6087  2.9360 1.0679 235

Detection Means (logit scale): 
                 Mean     SD    2.5%     50%   97.5%   Rhat  ESS
(Intercept)   -2.2063 0.4458 -3.1458 -2.2052 -1.3615 1.0156  493
scale(effort)  0.4540 0.1581  0.1655  0.4487  0.7943 1.0094 1310

Detection Variances (logit scale): 
                Mean     SD   2.5%    50%  97.5%   Rhat  ESS
(Intercept)   2.7575 1.3375 1.0464 2.4594 6.1069 1.0052  487
scale(effort) 0.1483 0.1291 0.0293 0.1077 0.5036 1.0032 1048
Code
# Fit a non-spatial, Latent Factor Multi-Species Occupancy Model. 
  out.lfMs <- msPGOcc(occ.formula = ~ scale(elev) +
                               scale(border_dist) + 
                               scale(FLII) , 
               det.formula = ~ scale(effort), # Ordinal.day + I(Ordinal.day^2) + Year
                 data = data_list, 
                 n.omp.threads = 6,
                 n.samples = 6000, 
                 n.factors = 5, # balance of rare sp. and run time
                 n.thin = 10, 
                 n.burn = 1000, 
                 n.chains = 3,
                 n.report = 1000);beep(sound = 4)
----------------------------------------
    Preparing to run the model
----------------------------------------
Warning in msPGOcc(occ.formula = ~scale(elev) + scale(border_dist) +
scale(FLII), : 'n.factors' is not an argument
No prior specified for beta.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for alpha.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for tau.sq.beta.ig.
Setting prior shape to 0.1 and prior scale to 0.1
No prior specified for tau.sq.alpha.ig.
Setting prior shape to 0.1 and prior scale to 0.1
z is not specified in initial values.
Setting initial values based on observed data
beta.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
alpha.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
tau.sq.beta is not specified in initial values.
Setting initial values to random values between 0.5 and 10
tau.sq.alpha is not specified in initial values.
Setting to initial values to random values between 0.5 and 10
beta is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
alpha is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
----------------------------------------
    Model description
----------------------------------------
Multi-species Occupancy Model with Polya-Gamma latent
variable fit with 51 sites and 18 species.

Samples per Chain: 6000 
Burn-in: 1000 
Thinning Rate: 10 
Number of Chains: 3 
Total Posterior Samples: 1500 

Source compiled with OpenMP support and model fit using 6 thread(s).

----------------------------------------
    Chain 1
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
----------------------------------------
    Chain 2
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
----------------------------------------
    Chain 3
----------------------------------------
Sampling ... 
Sampled: 1000 of 6000, 16.67%
-------------------------------------------------
Sampled: 2000 of 6000, 33.33%
-------------------------------------------------
Sampled: 3000 of 6000, 50.00%
-------------------------------------------------
Sampled: 4000 of 6000, 66.67%
-------------------------------------------------
Sampled: 5000 of 6000, 83.33%
-------------------------------------------------
Sampled: 6000 of 6000, 100.00%
Code
summary(out.lfMs, level = 'community')

Call:
msPGOcc(occ.formula = ~scale(elev) + scale(border_dist) + scale(FLII), 
    det.formula = ~scale(effort), data = data_list, n.samples = 6000, 
    n.omp.threads = 6, n.report = 1000, n.burn = 1000, n.thin = 10, 
    n.chains = 3, n.factors = 5)

Samples per Chain: 6000
Burn-in: 1000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 1500
Run Time (min): 0.8412

----------------------------------------
    Community Level
----------------------------------------
Occurrence Means (logit scale): 
                      Mean     SD    2.5%     50%  97.5%   Rhat ESS
(Intercept)        -1.0426 0.5903 -2.1271 -1.0664 0.1932 1.0628 258
scale(elev)         0.0225 0.2540 -0.4787  0.0264 0.5074 1.0088 769
scale(border_dist) -0.0074 0.2501 -0.5221 -0.0012 0.4705 1.0111 727
scale(FLII)         0.0982 0.3267 -0.4551  0.0783 0.8234 1.0166 752

Occurrence Variances (logit scale): 
                     Mean     SD   2.5%    50%   97.5%   Rhat ESS
(Intercept)        4.2469 2.6484 1.3794 3.5146 10.9348 1.0909 210
scale(elev)        0.2774 0.3162 0.0382 0.1802  1.1586 1.0080 350
scale(border_dist) 0.2290 0.3115 0.0325 0.1423  0.9730 1.1946 353
scale(FLII)        0.8409 0.8987 0.0866 0.5816  3.0295 1.1434 234

Detection Means (logit scale): 
                 Mean     SD    2.5%     50%   97.5%   Rhat ESS
(Intercept)   -2.2128 0.4530 -3.1837 -2.2079 -1.3761 1.0197 633
scale(effort)  0.4604 0.1688  0.1477  0.4563  0.8075 1.0110 592

Detection Variances (logit scale): 
                Mean     SD   2.5%    50%  97.5%   Rhat ESS
(Intercept)   2.8373 1.4476 0.9928 2.5078 6.4614 1.0615 268
scale(effort) 0.1578 0.1438 0.0300 0.1173 0.5267 1.0291 955
Code
# Fit a Spatial Factor Multi-Species Occupancy Model
# latent spatial factors.
tictoc::tic()
  out.sp <- sfMsPGOcc(occ.formula = ~ scale(elev) +
                               scale(border_dist) + 
                               scale(FLII) , 
               det.formula = ~ scale(effort), # Ordinal.day + I(Ordinal.day^2) + Year,            
                      data = data_list, 
                      n.omp.threads = 6,
                      n.batch = 600, 
                      batch.length = 25, # iter=600*25
                      n.thin = 10, 
                      n.burn = 5000, 
                      n.chains = 3,
                      NNGP = TRUE,
                      n.factors = 5, # balance of rare sp. and run time
                      n.neighbors = 15,
                      cov.model = 'exponential',
                      n.report = 1000);beep(sound = 4)
----------------------------------------
    Preparing to run the model
----------------------------------------
No prior specified for beta.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for alpha.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for tau.sq.beta.
Using an inverse-Gamma prior with prior shape 0.1 and prior scale 0.1
No prior specified for tau.sq.alpha.
Using an inverse-Gamma prior with prior shape 0.1 and prior scale 0.1
No prior specified for phi.unif.
Setting uniform bounds based on the range of observed spatial coordinates.
z is not specified in initial values.
Setting initial values based on observed data
beta.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
alpha.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
tau.sq.beta is not specified in initial values.
Setting initial values to random values between 0.5 and 10
tau.sq.alpha is not specified in initial values.
Setting initial values to random values between 0.5 and 10
beta is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
alpha is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
phi is not specified in initial values.
Setting initial value to random values from the prior distribution
lambda is not specified in initial values.
Setting initial values of the lower triangle to 0
----------------------------------------
    Building the neighbor list
----------------------------------------
----------------------------------------
Building the neighbors of neighbors list
----------------------------------------
----------------------------------------
    Model description
----------------------------------------
Spatial Factor NNGP Multi-species Occupancy Model with Polya-Gamma latent
variable fit with 51 sites and 18 species.

Samples per chain: 15000 (600 batches of length 25)
Burn-in: 5000 
Thinning Rate: 10 
Number of Chains: 3 
Total Posterior Samples: 3000 

Using the exponential spatial correlation model.

Using 5 latent spatial factors.
Using 15 nearest neighbors.

Source compiled with OpenMP support and model fit using 6 thread(s).

Adaptive Metropolis with target acceptance rate: 43.0
----------------------------------------
    Chain 1
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
----------------------------------------
    Chain 2
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
----------------------------------------
    Chain 3
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
Code
tictoc::toc()
276.31 sec elapsed
Code
#########################
tictoc::tic()
  out.sp.cat <- sfMsPGOcc(occ.formula = ~ scale(elev) +
                               factor(in_AP) + 
                               scale(FLII) , 
               det.formula = ~ scale(effort), # Ordinal.day + I(Ordinal.day^2) + Year,            
                      data = data_list, 
                      n.omp.threads = 6,
                      n.batch = 600, 
                      batch.length = 25, # iter=600*25
                      n.thin = 10, 
                      n.burn = 5000, 
                      n.chains = 3,
                      NNGP = TRUE,
                      n.factors = 5, # balance of rare sp. and run time
                      n.neighbors = 15,
                      cov.model = 'exponential',
                      n.report = 1000);beep(sound = 4)
----------------------------------------
    Preparing to run the model
----------------------------------------
No prior specified for beta.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for alpha.comm.normal.
Setting prior mean to 0 and prior variance to 2.72
No prior specified for tau.sq.beta.
Using an inverse-Gamma prior with prior shape 0.1 and prior scale 0.1
No prior specified for tau.sq.alpha.
Using an inverse-Gamma prior with prior shape 0.1 and prior scale 0.1
No prior specified for phi.unif.
Setting uniform bounds based on the range of observed spatial coordinates.
z is not specified in initial values.
Setting initial values based on observed data
beta.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
alpha.comm is not specified in initial values.
Setting initial values to random values from the prior distribution
tau.sq.beta is not specified in initial values.
Setting initial values to random values between 0.5 and 10
tau.sq.alpha is not specified in initial values.
Setting initial values to random values between 0.5 and 10
beta is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
alpha is not specified in initial values.
Setting initial values to random values from the community-level normal distribution
phi is not specified in initial values.
Setting initial value to random values from the prior distribution
lambda is not specified in initial values.
Setting initial values of the lower triangle to 0
----------------------------------------
    Building the neighbor list
----------------------------------------
----------------------------------------
Building the neighbors of neighbors list
----------------------------------------
----------------------------------------
    Model description
----------------------------------------
Spatial Factor NNGP Multi-species Occupancy Model with Polya-Gamma latent
variable fit with 51 sites and 18 species.

Samples per chain: 15000 (600 batches of length 25)
Burn-in: 5000 
Thinning Rate: 10 
Number of Chains: 3 
Total Posterior Samples: 3000 

Using the exponential spatial correlation model.

Using 5 latent spatial factors.
Using 15 nearest neighbors.

Source compiled with OpenMP support and model fit using 6 thread(s).

Adaptive Metropolis with target acceptance rate: 43.0
----------------------------------------
    Chain 1
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
----------------------------------------
    Chain 2
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
----------------------------------------
    Chain 3
----------------------------------------
Sampling ... 
Batch: 600 of 600, 100.00%
Code
tictoc::toc()
274.33 sec elapsed
Code
# tictoc::tic()
#   out.sp.gaus <- sfMsPGOcc(occ.formula = ~ scale(border_dist) , 
#                            det.formula = ~ scale(effort), # Ordinal.day + I(Ordinal.day^2) + Year,              
#                            data = data_list, 
#                            n.batch = 400, 
#                            batch.length = 25,
#                            n.thin = 5, 
#                            n.burn = 5000, 
#                            n.chains = 1,
#                            NNGP = TRUE,
#                            n.factors = 5,
#                            n.neighbors = 15,
#                            cov.model = 'gaussian',
#                            n.report = 100);beep(sound = 4)
# tictoc::toc()

# save the results to not run again
# save(out, file="C:/CodigoR/Occu_APs_all/blog/2025-10-15-analysis/result/result_2.R") # guardamos los resultados para no correr de nuevo
# save the results to not run again
# save(out.sp, file="C:/CodigoR/Occu_APs_all/blog/2025-10-15-analysis/result/sp_result_2.R") # guardamos los resultados para no correr de nuevo

# save(out.lfMs, file="C:/CodigoR/Occu_APs_all/blog/2025-10-15-analysis/result/lfms_result_2.R") # guardamos los resultados para no correr de nuevo


# load("C:/CodigoR/Occu_APs_all/blog/2025-10-15-analysis/result/sp_result_2.R")
# summary(fit.commu)

Model comparison

Code
# 4. Model comparison -----------------------------------------------------
# Compute Widely Applicable Information Criterion (WAIC)
# Lower values indicate better model fit. 
waicOcc(out.null)
      elpd         pD       WAIC 
-932.43187   61.04599 1986.95571 
Code
waicOcc(out.lfMs)
      elpd         pD       WAIC 
-932.34833   61.70634 1988.10934 
Code
waicOcc(out.sp)
     elpd        pD      WAIC 
-827.7398  113.2061 1881.8920 
Code
waicOcc(out.sp.cat)
     elpd        pD      WAIC 
-828.1037  112.6926 1881.5927 

Model comparison as table

Code
# Here we summarize the spatial factor loadings
# summary(out.sp$lambda.samples)

# Resultados --------------------------------------------------------------
# Extraemos lo tabla de valores estimados
modresult <- cbind(as.data.frame(waicOcc(out.null)),
                  as.data.frame(waicOcc(out.sp)),
                  as.data.frame(waicOcc(out.lfMs)),
                  as.data.frame(waicOcc(out.sp.cat))
                  #as.data.frame(waicOcc(out.sp.gaus))
                  )
# View(modresult)
modresult_sorted <- as.data.frame(t(modresult)) |> 
  arrange(WAIC) # sort by
  
DT::datatable(modresult_sorted)
Important

Lower values in WAIC indicate better model fit.

WAIC is the Widely Applicable Information Criterion (Watanabe 2010).

Model validation

We next perform a posterior predictive check using the Freeman-Tukey statistic grouping the data by sites. We summarize the posterior predictive check with the summary() function, which reports a Bayesian p-value. A Bayesian p-value that hovers around 0.5 indicates adequate model fit, while values less than 0.1 or greater than 0.9 suggest our model does not fit the data well (Hobbs and Hooten 2015). As always with a simulation-based analysis using MCMC, you will get numerically slightly different values.

Code
# 3. Model validation -----------------------------------------------------
# Perform a posterior predictive check to assess model fit. 
ppc.out <- ppcOcc(out.null, fit.stat = 'freeman-tukey', 
                  group = 1)
ppc.out.lfMs <- ppcOcc(out.lfMs, fit.stat = 'freeman-tukey', 
                  group = 1)
ppc.out.sp <- ppcOcc(out.sp, fit.stat = 'freeman-tukey',
                     group = 1)
ppc.out.sp.cat <- ppcOcc(out.sp.cat, fit.stat = 'freeman-tukey',
                     group = 1)

# Calculate a Bayesian p-value as a simple measure of Goodness of Fit.
# Bayesian p-values between 0.1 and 0.9 indicate adequate model fit. 
# summary(ppc.out)
# summary(ppc.out.lfMs)
# summary(ppc.out.sp)
summary(ppc.out.sp.cat)

Call:
ppcOcc(object = out.sp.cat, fit.stat = "freeman-tukey", group = 1)

Samples per Chain: 15000
Burn-in: 5000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 3000

----------------------------------------
    Community Level
----------------------------------------
Bayesian p-value:  0.374 

----------------------------------------
    Species Level
----------------------------------------
Cabassous unicinctus Bayesian p-value: 0.4363
Coendou Bayesian p-value: 0.3833
Cuniculus paca Bayesian p-value: 0.0163
Dasyprocta fuliginosa Bayesian p-value: 0.3483
Dasypus novemcinctus Bayesian p-value: 0.0583
Didelphis marsupialis Bayesian p-value: 0.464
Eira barbara Bayesian p-value: 0.9563
Galictis vittata Bayesian p-value: 0.3777
Leopardus Bayesian p-value: 0.532
Mazama americana Bayesian p-value: 0.4003
Myoprocta pratti Bayesian p-value: 0.0863
Nasua nasua Bayesian p-value: 0.6027
Panthera onca Bayesian p-value: 0.183
Pecari tajacu Bayesian p-value: 0.3913
Puma concolor Bayesian p-value: 0.3803
Speothos venaticus Bayesian p-value: 0.4097
Tamandua tetradactyla Bayesian p-value: 0.6413
Tapirus terrestris Bayesian p-value: 0.0647
Fit statistic:  freeman-tukey 
Important

A Bayesian p-value that around 0.5 indicates adequate model fit, while values less than 0.1 or greater than 0.9 suggest our model does not fit the data well.

Fit plot

Code
#### fit plot
ppc.df <- data.frame(fit = ppc.out.sp.cat$fit.y, 
                     fit.rep = ppc.out.sp.cat$fit.y.rep, 
                     color = 'lightskyblue1')

ppc.df$color[ppc.df$fit.rep.1 > ppc.df$fit.1] <- 'lightsalmon'
plot(ppc.df$fit.1, ppc.df$fit.rep.1, bg = ppc.df$color, pch = 21, 
     ylab = 'Fit', xlab = 'True')
lines(ppc.df$fit.1, ppc.df$fit.1, col = 'black')

The most symmetrical, better fit!

The most symmetrical, better fit!

Posterior Summary

Code
# 5. Posterior summaries --------------------------------------------------
# Concise summary of main parameter estimates
summary(out.sp.cat, level = 'community')

Call:
sfMsPGOcc(occ.formula = ~scale(elev) + factor(in_AP) + scale(FLII), 
    det.formula = ~scale(effort), data = data_list, cov.model = "exponential", 
    NNGP = TRUE, n.neighbors = 15, n.factors = 5, n.batch = 600, 
    batch.length = 25, n.omp.threads = 6, n.report = 1000, n.burn = 5000, 
    n.thin = 10, n.chains = 3)

Samples per Chain: 15000
Burn-in: 5000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 3000
Run Time (min): 4.571

----------------------------------------
    Community Level
----------------------------------------
Occurrence Means (logit scale): 
                  Mean     SD    2.5%     50%   97.5%   Rhat  ESS
(Intercept)    -1.7140 0.7301 -3.0593 -1.7564 -0.2302 1.0098  505
scale(elev)     0.1165 0.6099 -1.0235  0.1313  1.3341 1.0096  231
factor(in_AP)1 -0.4151 1.5058 -3.3190 -0.4193  2.4249 1.0070  194
scale(FLII)     0.1042 0.3996 -0.5886  0.0754  1.0076 1.0007 1119

Occurrence Variances (logit scale): 
                 Mean     SD   2.5%    50%   97.5%   Rhat  ESS
(Intercept)    5.7135 4.2356 1.3131 4.7624 16.0379 1.0316  287
scale(elev)    0.3349 0.3894 0.0374 0.2086  1.3884 1.0227 1088
factor(in_AP)1 1.5747 2.9810 0.0476 0.5704  9.0443 1.0232 1007
scale(FLII)    1.2269 1.3918 0.0863 0.7902  4.9032 1.0242  496

Detection Means (logit scale): 
                 Mean     SD    2.5%     50%   97.5%   Rhat  ESS
(Intercept)   -2.1668 0.4407 -3.0691 -2.1551 -1.3347 1.0125  878
scale(effort)  0.4651 0.1607  0.1659  0.4593  0.7922 1.0009 2020

Detection Variances (logit scale): 
                Mean     SD   2.5%    50%  97.5%   Rhat  ESS
(Intercept)   2.7807 1.4367 0.9724 2.4685 6.5935 1.0075  880
scale(effort) 0.1521 0.1278 0.0294 0.1164 0.5143 1.0047 2109

----------------------------------------
    Spatial Covariance
----------------------------------------
        Mean     SD  2.5%    50%  97.5%   Rhat  ESS
phi-1 0.0076 0.0036 7e-04 0.0078 0.0133 0.9999  914
phi-2 0.0070 0.0039 4e-04 0.0071 0.0133 1.0032  662
phi-3 0.0071 0.0038 6e-04 0.0071 0.0132 1.0004 1052
phi-4 0.0065 0.0040 3e-04 0.0063 0.0132 1.0016  705
phi-5 0.0067 0.0040 4e-04 0.0067 0.0132 0.9998 1084
Code
summary(out.sp.cat, level = 'species')

Call:
sfMsPGOcc(occ.formula = ~scale(elev) + factor(in_AP) + scale(FLII), 
    det.formula = ~scale(effort), data = data_list, cov.model = "exponential", 
    NNGP = TRUE, n.neighbors = 15, n.factors = 5, n.batch = 600, 
    batch.length = 25, n.omp.threads = 6, n.report = 1000, n.burn = 5000, 
    n.thin = 10, n.chains = 3)

Samples per Chain: 15000
Burn-in: 5000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 3000
Run Time (min): 4.571

----------------------------------------
    Species Level
----------------------------------------
Occurrence (logit scale): 
                                        Mean     SD    2.5%     50%   97.5%
(Intercept)-Cabassous unicinctus     -2.7758 1.2546 -5.0922 -2.8560 -0.1333
(Intercept)-Coendou                  -3.1256 1.8358 -6.5074 -3.2049  0.5983
(Intercept)-Cuniculus paca            0.5201 0.6470 -0.7042  0.5139  1.8204
(Intercept)-Dasyprocta fuliginosa    -0.0082 0.7535 -1.5797  0.0030  1.4513
(Intercept)-Dasypus novemcinctus     -0.3732 0.7193 -1.8345 -0.3561  1.0104
(Intercept)-Didelphis marsupialis    -2.2422 2.1392 -5.6147 -2.4288  2.4900
(Intercept)-Eira barbara              1.0723 1.6256 -1.4826  0.8289  4.9249
(Intercept)-Galictis vittata         -2.6962 2.2591 -6.5000 -2.8923  1.7274
(Intercept)-Leopardus                -2.0144 2.0958 -5.6284 -2.1667  2.6677
(Intercept)-Mazama americana         -0.8346 1.4728 -3.3096 -0.9663  2.4236
(Intercept)-Myoprocta pratti         -4.3281 1.1562 -6.8593 -4.2229 -2.3501
(Intercept)-Nasua nasua              -0.1339 1.1841 -2.1737 -0.2156  2.3010
(Intercept)-Panthera onca            -3.9956 1.1069 -6.4235 -3.9481 -2.0321
(Intercept)-Pecari tajacu            -2.6057 2.0913 -6.4009 -2.7070  1.8162
(Intercept)-Puma concolor            -3.2762 1.3207 -5.9005 -3.2926 -0.5684
(Intercept)-Speothos venaticus       -3.1509 2.0718 -7.0844 -3.2127  1.1610
(Intercept)-Tamandua tetradactyla    -0.7010 1.9179 -3.6035 -0.9875  4.2013
(Intercept)-Tapirus terrestris       -3.7718 1.0710 -6.0364 -3.6937 -1.8598
scale(elev)-Cabassous unicinctus     -0.0103 0.7951 -1.6226  0.0046  1.5342
scale(elev)-Coendou                   0.4521 0.8154 -1.0475  0.4214  2.1827
scale(elev)-Cuniculus paca           -0.0356 0.7098 -1.4452 -0.0250  1.3337
scale(elev)-Dasyprocta fuliginosa     0.4158 0.7421 -1.0180  0.4074  1.9160
scale(elev)-Dasypus novemcinctus      0.2868 0.7153 -1.1006  0.2857  1.7301
scale(elev)-Didelphis marsupialis     0.0220 0.8211 -1.6078  0.0385  1.6332
scale(elev)-Eira barbara              0.2015 0.7736 -1.2403  0.1845  1.7841
scale(elev)-Galictis vittata          0.0718 0.8175 -1.5567  0.0779  1.6193
scale(elev)-Leopardus                 0.0124 0.8246 -1.7004  0.0367  1.6183
scale(elev)-Mazama americana          0.0943 0.7836 -1.4347  0.1038  1.6575
scale(elev)-Myoprocta pratti          0.2028 0.7189 -1.1817  0.1991  1.6365
scale(elev)-Nasua nasua               0.0527 0.7630 -1.4399  0.0632  1.5997
scale(elev)-Panthera onca            -0.0585 0.7892 -1.6926 -0.0477  1.4454
scale(elev)-Pecari tajacu             0.0577 0.8014 -1.4870  0.0695  1.6154
scale(elev)-Puma concolor            -0.0350 0.7969 -1.6315 -0.0093  1.4907
scale(elev)-Speothos venaticus        0.4087 0.7957 -1.0495  0.3891  2.0876
scale(elev)-Tamandua tetradactyla     0.1734 0.7958 -1.3354  0.1644  1.8019
scale(elev)-Tapirus terrestris       -0.1162 0.8014 -1.8196 -0.0736  1.3446
factor(in_AP)1-Cabassous unicinctus  -0.7269 1.9006 -4.7769 -0.6542  2.7499
factor(in_AP)1-Coendou                0.0557 1.9287 -3.4537 -0.0015  4.0973
factor(in_AP)1-Cuniculus paca        -0.5701 1.7590 -4.1106 -0.5554  2.8054
factor(in_AP)1-Dasyprocta fuliginosa  0.1250 1.8592 -3.2592  0.0772  3.9660
factor(in_AP)1-Dasypus novemcinctus  -0.1405 1.7838 -3.6023 -0.1156  3.3838
factor(in_AP)1-Didelphis marsupialis -0.5953 1.9121 -4.6882 -0.5549  2.9858
factor(in_AP)1-Eira barbara          -0.1080 1.9464 -3.7221 -0.1555  3.9745
factor(in_AP)1-Galictis vittata      -0.5671 1.8545 -4.2783 -0.5501  2.8810
factor(in_AP)1-Leopardus             -0.6177 1.9173 -4.5919 -0.5213  2.9021
factor(in_AP)1-Mazama americana      -0.4355 1.8491 -4.1216 -0.4489  3.2084
factor(in_AP)1-Myoprocta pratti      -0.4033 1.7726 -3.9496 -0.4079  3.0311
factor(in_AP)1-Nasua nasua           -0.4554 1.8282 -4.1406 -0.4114  3.0325
factor(in_AP)1-Panthera onca         -0.8269 1.9289 -5.0110 -0.7403  2.5568
factor(in_AP)1-Pecari tajacu         -0.5518 1.9810 -4.5634 -0.5506  3.1582
factor(in_AP)1-Puma concolor         -0.7231 1.8940 -4.6968 -0.6211  2.7517
factor(in_AP)1-Speothos venaticus    -0.0425 1.9422 -3.6583 -0.1278  3.9264
factor(in_AP)1-Tamandua tetradactyla -0.2259 1.9097 -3.7931 -0.2644  3.7070
factor(in_AP)1-Tapirus terrestris    -0.8839 1.9006 -5.0176 -0.8356  2.5788
scale(FLII)-Cabassous unicinctus     -1.0168 0.6930 -2.5501 -0.9626  0.1412
scale(FLII)-Coendou                   0.4073 1.0112 -1.3835  0.3176  2.7329
scale(FLII)-Cuniculus paca           -0.3199 0.5053 -1.4167 -0.2843  0.5865
scale(FLII)-Dasyprocta fuliginosa    -0.7356 0.6143 -2.0894 -0.6863  0.2871
scale(FLII)-Dasypus novemcinctus     -0.4116 0.5372 -1.5246 -0.3840  0.5704
scale(FLII)-Didelphis marsupialis     0.3626 0.9887 -1.4839  0.2839  2.6071
scale(FLII)-Eira barbara              0.0951 0.7107 -1.4648  0.1034  1.4272
scale(FLII)-Galictis vittata         -0.4991 1.0653 -3.1583 -0.3575  1.2653
scale(FLII)-Leopardus                 0.3803 1.0770 -1.4682  0.2658  2.9072
scale(FLII)-Mazama americana          0.9323 1.0164 -0.6458  0.8016  3.2334
scale(FLII)-Myoprocta pratti          0.9450 0.8994 -0.4354  0.8044  3.0356
scale(FLII)-Nasua nasua              -0.4827 0.7135 -2.0764 -0.4092  0.7852
scale(FLII)-Panthera onca             0.5434 0.8182 -0.7759  0.4536  2.4598
scale(FLII)-Pecari tajacu             0.3740 1.1045 -1.4775  0.2461  3.0545
scale(FLII)-Puma concolor             0.3724 0.8446 -1.1195  0.2882  2.2880
scale(FLII)-Speothos venaticus        0.4722 1.0640 -1.2948  0.3391  3.0043
scale(FLII)-Tamandua tetradactyla    -0.4591 0.8587 -2.4009 -0.3876  1.0347
scale(FLII)-Tapirus terrestris        0.9849 0.9166 -0.3795  0.8441  3.2136
                                       Rhat  ESS
(Intercept)-Cabassous unicinctus     1.0461  687
(Intercept)-Coendou                  1.0037  470
(Intercept)-Cuniculus paca           1.0098  669
(Intercept)-Dasyprocta fuliginosa    1.0292  691
(Intercept)-Dasypus novemcinctus     1.0107  894
(Intercept)-Didelphis marsupialis    1.0069  301
(Intercept)-Eira barbara             1.0026  431
(Intercept)-Galictis vittata         1.0263  320
(Intercept)-Leopardus                1.0381  338
(Intercept)-Mazama americana         1.0046  568
(Intercept)-Myoprocta pratti         1.0118  970
(Intercept)-Nasua nasua              1.0221  734
(Intercept)-Panthera onca            1.0036  844
(Intercept)-Pecari tajacu            1.0432  345
(Intercept)-Puma concolor            1.0029  842
(Intercept)-Speothos venaticus       1.0537  371
(Intercept)-Tamandua tetradactyla    1.0226  295
(Intercept)-Tapirus terrestris       1.0047  773
scale(elev)-Cabassous unicinctus     1.0009  325
scale(elev)-Coendou                  1.0132  339
scale(elev)-Cuniculus paca           1.0082  293
scale(elev)-Dasyprocta fuliginosa    1.0151  303
scale(elev)-Dasypus novemcinctus     1.0155  303
scale(elev)-Didelphis marsupialis    1.0072  305
scale(elev)-Eira barbara             1.0097  356
scale(elev)-Galictis vittata         1.0046  314
scale(elev)-Leopardus                1.0002  357
scale(elev)-Mazama americana         1.0013  321
scale(elev)-Myoprocta pratti         1.0045  296
scale(elev)-Nasua nasua              1.0037  288
scale(elev)-Panthera onca            1.0113  317
scale(elev)-Pecari tajacu            1.0005  377
scale(elev)-Puma concolor            1.0059  379
scale(elev)-Speothos venaticus       1.0130  396
scale(elev)-Tamandua tetradactyla    1.0082  402
scale(elev)-Tapirus terrestris       1.0043  383
factor(in_AP)1-Cabassous unicinctus  1.0243  300
factor(in_AP)1-Coendou               1.0012  310
factor(in_AP)1-Cuniculus paca        1.0068  243
factor(in_AP)1-Dasyprocta fuliginosa 1.0018  287
factor(in_AP)1-Dasypus novemcinctus  1.0042  283
factor(in_AP)1-Didelphis marsupialis 1.0104  311
factor(in_AP)1-Eira barbara          1.0009  307
factor(in_AP)1-Galictis vittata      1.0034  279
factor(in_AP)1-Leopardus             1.0073  322
factor(in_AP)1-Mazama americana      1.0061  282
factor(in_AP)1-Myoprocta pratti      1.0071  269
factor(in_AP)1-Nasua nasua           1.0078  264
factor(in_AP)1-Panthera onca         1.0087  324
factor(in_AP)1-Pecari tajacu         1.0193  298
factor(in_AP)1-Puma concolor         1.0111  310
factor(in_AP)1-Speothos venaticus    1.0016  334
factor(in_AP)1-Tamandua tetradactyla 1.0031  312
factor(in_AP)1-Tapirus terrestris    1.0096  290
scale(FLII)-Cabassous unicinctus     1.0118 1096
scale(FLII)-Coendou                  1.0043 1242
scale(FLII)-Cuniculus paca           1.0070 1952
scale(FLII)-Dasyprocta fuliginosa    1.0006 1403
scale(FLII)-Dasypus novemcinctus     1.0046 1912
scale(FLII)-Didelphis marsupialis    1.0045 1439
scale(FLII)-Eira barbara             1.0029 1767
scale(FLII)-Galictis vittata         1.0054  883
scale(FLII)-Leopardus                1.0128  740
scale(FLII)-Mazama americana         1.0071  760
scale(FLII)-Myoprocta pratti         1.0111 1118
scale(FLII)-Nasua nasua              1.0088 1470
scale(FLII)-Panthera onca            1.0022 1474
scale(FLII)-Pecari tajacu            1.0077  796
scale(FLII)-Puma concolor            1.0061 1505
scale(FLII)-Speothos venaticus       1.0046  909
scale(FLII)-Tamandua tetradactyla    1.0225 1063
scale(FLII)-Tapirus terrestris       1.0106  816

Detection (logit scale): 
                                       Mean     SD    2.5%     50%   97.5%
(Intercept)-Cabassous unicinctus    -2.4851 0.7730 -4.1419 -2.4247 -1.1347
(Intercept)-Coendou                 -3.4320 1.2406 -5.8909 -3.4184 -1.0581
(Intercept)-Cuniculus paca          -0.1105 0.1324 -0.3714 -0.1085  0.1374
(Intercept)-Dasyprocta fuliginosa   -0.6898 0.1583 -1.0079 -0.6871 -0.3829
(Intercept)-Dasypus novemcinctus    -0.1587 0.1489 -0.4495 -0.1569  0.1333
(Intercept)-Didelphis marsupialis   -3.6001 1.0085 -5.5356 -3.6189 -1.6318
(Intercept)-Eira barbara            -2.7524 0.3461 -3.4472 -2.7549 -2.0858
(Intercept)-Galictis vittata        -3.8043 1.2003 -6.0692 -3.8168 -1.4391
(Intercept)-Leopardus               -3.6796 1.0176 -5.6716 -3.6724 -1.7777
(Intercept)-Mazama americana        -2.7969 0.5205 -3.8185 -2.7862 -1.8089
(Intercept)-Myoprocta pratti        -0.8651 0.5105 -1.9547 -0.8507  0.0949
(Intercept)-Nasua nasua             -2.2986 0.3258 -2.9371 -2.2840 -1.7034
(Intercept)-Panthera onca           -1.0784 0.5287 -2.2165 -1.0354 -0.1278
(Intercept)-Pecari tajacu           -3.8346 1.2116 -6.1795 -3.8567 -1.5436
(Intercept)-Puma concolor           -2.1948 0.7460 -3.7684 -2.1435 -0.8914
(Intercept)-Speothos venaticus      -3.7074 1.2149 -6.0807 -3.7154 -1.3665
(Intercept)-Tamandua tetradactyla   -3.2761 0.5980 -4.4721 -3.2799 -2.0895
(Intercept)-Tapirus terrestris      -0.4445 0.3873 -1.2379 -0.4357  0.2817
scale(effort)-Cabassous unicinctus   0.2025 0.3435 -0.5058  0.2136  0.8672
scale(effort)-Coendou                0.4346 0.3754 -0.2774  0.4290  1.2421
scale(effort)-Cuniculus paca         0.4540 0.1604  0.1543  0.4525  0.7848
scale(effort)-Dasyprocta fuliginosa  0.7235 0.2522  0.2862  0.6999  1.2837
scale(effort)-Dasypus novemcinctus   0.4952 0.1772  0.1570  0.4833  0.8582
scale(effort)-Didelphis marsupialis  0.5238 0.3855 -0.2092  0.5033  1.3484
scale(effort)-Eira barbara           0.4360 0.2839 -0.0702  0.4220  1.0690
scale(effort)-Galictis vittata       0.4279 0.3685 -0.2726  0.4270  1.1894
scale(effort)-Leopardus              0.4990 0.4023 -0.2495  0.4713  1.3957
scale(effort)-Mazama americana       0.6527 0.3758  0.0361  0.6173  1.5209
scale(effort)-Myoprocta pratti       0.6277 0.3931 -0.0388  0.5902  1.4947
scale(effort)-Nasua nasua            0.6603 0.3289  0.0992  0.6339  1.3833
scale(effort)-Panthera onca          0.4926 0.3608 -0.2059  0.4807  1.2570
scale(effort)-Pecari tajacu          0.4776 0.3866 -0.2461  0.4680  1.2966
scale(effort)-Puma concolor          0.2873 0.3618 -0.4280  0.2940  0.9962
scale(effort)-Speothos venaticus     0.4853 0.3948 -0.2631  0.4669  1.3459
scale(effort)-Tamandua tetradactyla  0.2350 0.3189 -0.3699  0.2337  0.8722
scale(effort)-Tapirus terrestris     0.2700 0.3303 -0.4267  0.2786  0.9121
                                      Rhat  ESS
(Intercept)-Cabassous unicinctus    1.0251 1138
(Intercept)-Coendou                 1.0058  672
(Intercept)-Cuniculus paca          1.0000 3000
(Intercept)-Dasyprocta fuliginosa   1.0106 3176
(Intercept)-Dasypus novemcinctus    1.0009 3000
(Intercept)-Didelphis marsupialis   1.0130  512
(Intercept)-Eira barbara            1.0029  946
(Intercept)-Galictis vittata        1.0032  623
(Intercept)-Leopardus               1.0615  442
(Intercept)-Mazama americana        1.0010  880
(Intercept)-Myoprocta pratti        1.0020 3000
(Intercept)-Nasua nasua             1.0026 1529
(Intercept)-Panthera onca           1.0003 3000
(Intercept)-Pecari tajacu           1.0424  317
(Intercept)-Puma concolor           1.0053  993
(Intercept)-Speothos venaticus      1.0530  452
(Intercept)-Tamandua tetradactyla   1.0096  609
(Intercept)-Tapirus terrestris      1.0006 3000
scale(effort)-Cabassous unicinctus  1.0004 2815
scale(effort)-Coendou               1.0010 2834
scale(effort)-Cuniculus paca        1.0026 2684
scale(effort)-Dasyprocta fuliginosa 1.0060 2772
scale(effort)-Dasypus novemcinctus  1.0028 2833
scale(effort)-Didelphis marsupialis 1.0046 2576
scale(effort)-Eira barbara          1.0000 2716
scale(effort)-Galictis vittata      1.0000 2387
scale(effort)-Leopardus             1.0020 2012
scale(effort)-Mazama americana      1.0062 2116
scale(effort)-Myoprocta pratti      1.0013 2692
scale(effort)-Nasua nasua           1.0001 2703
scale(effort)-Panthera onca         1.0014 3000
scale(effort)-Pecari tajacu         1.0002 3167
scale(effort)-Puma concolor         1.0057 2797
scale(effort)-Speothos venaticus    1.0029 2551
scale(effort)-Tamandua tetradactyla 1.0009 2593
scale(effort)-Tapirus terrestris    1.0013 2778

----------------------------------------
    Spatial Covariance
----------------------------------------
        Mean     SD  2.5%    50%  97.5%   Rhat  ESS
phi-1 0.0076 0.0036 7e-04 0.0078 0.0133 0.9999  914
phi-2 0.0070 0.0039 4e-04 0.0071 0.0133 1.0032  662
phi-3 0.0071 0.0038 6e-04 0.0071 0.0132 1.0004 1052
phi-4 0.0065 0.0040 3e-04 0.0063 0.0132 1.0016  705
phi-5 0.0067 0.0040 4e-04 0.0067 0.0132 0.9998 1084
Code
summary(out.sp.cat, level = 'both')

Call:
sfMsPGOcc(occ.formula = ~scale(elev) + factor(in_AP) + scale(FLII), 
    det.formula = ~scale(effort), data = data_list, cov.model = "exponential", 
    NNGP = TRUE, n.neighbors = 15, n.factors = 5, n.batch = 600, 
    batch.length = 25, n.omp.threads = 6, n.report = 1000, n.burn = 5000, 
    n.thin = 10, n.chains = 3)

Samples per Chain: 15000
Burn-in: 5000
Thinning Rate: 10
Number of Chains: 3
Total Posterior Samples: 3000
Run Time (min): 4.571

----------------------------------------
    Community Level
----------------------------------------
Occurrence Means (logit scale): 
                  Mean     SD    2.5%     50%   97.5%   Rhat  ESS
(Intercept)    -1.7140 0.7301 -3.0593 -1.7564 -0.2302 1.0098  505
scale(elev)     0.1165 0.6099 -1.0235  0.1313  1.3341 1.0096  231
factor(in_AP)1 -0.4151 1.5058 -3.3190 -0.4193  2.4249 1.0070  194
scale(FLII)     0.1042 0.3996 -0.5886  0.0754  1.0076 1.0007 1119

Occurrence Variances (logit scale): 
                 Mean     SD   2.5%    50%   97.5%   Rhat  ESS
(Intercept)    5.7135 4.2356 1.3131 4.7624 16.0379 1.0316  287
scale(elev)    0.3349 0.3894 0.0374 0.2086  1.3884 1.0227 1088
factor(in_AP)1 1.5747 2.9810 0.0476 0.5704  9.0443 1.0232 1007
scale(FLII)    1.2269 1.3918 0.0863 0.7902  4.9032 1.0242  496

Detection Means (logit scale): 
                 Mean     SD    2.5%     50%   97.5%   Rhat  ESS
(Intercept)   -2.1668 0.4407 -3.0691 -2.1551 -1.3347 1.0125  878
scale(effort)  0.4651 0.1607  0.1659  0.4593  0.7922 1.0009 2020

Detection Variances (logit scale): 
                Mean     SD   2.5%    50%  97.5%   Rhat  ESS
(Intercept)   2.7807 1.4367 0.9724 2.4685 6.5935 1.0075  880
scale(effort) 0.1521 0.1278 0.0294 0.1164 0.5143 1.0047 2109

----------------------------------------
    Species Level
----------------------------------------
Occurrence (logit scale): 
                                        Mean     SD    2.5%     50%   97.5%
(Intercept)-Cabassous unicinctus     -2.7758 1.2546 -5.0922 -2.8560 -0.1333
(Intercept)-Coendou                  -3.1256 1.8358 -6.5074 -3.2049  0.5983
(Intercept)-Cuniculus paca            0.5201 0.6470 -0.7042  0.5139  1.8204
(Intercept)-Dasyprocta fuliginosa    -0.0082 0.7535 -1.5797  0.0030  1.4513
(Intercept)-Dasypus novemcinctus     -0.3732 0.7193 -1.8345 -0.3561  1.0104
(Intercept)-Didelphis marsupialis    -2.2422 2.1392 -5.6147 -2.4288  2.4900
(Intercept)-Eira barbara              1.0723 1.6256 -1.4826  0.8289  4.9249
(Intercept)-Galictis vittata         -2.6962 2.2591 -6.5000 -2.8923  1.7274
(Intercept)-Leopardus                -2.0144 2.0958 -5.6284 -2.1667  2.6677
(Intercept)-Mazama americana         -0.8346 1.4728 -3.3096 -0.9663  2.4236
(Intercept)-Myoprocta pratti         -4.3281 1.1562 -6.8593 -4.2229 -2.3501
(Intercept)-Nasua nasua              -0.1339 1.1841 -2.1737 -0.2156  2.3010
(Intercept)-Panthera onca            -3.9956 1.1069 -6.4235 -3.9481 -2.0321
(Intercept)-Pecari tajacu            -2.6057 2.0913 -6.4009 -2.7070  1.8162
(Intercept)-Puma concolor            -3.2762 1.3207 -5.9005 -3.2926 -0.5684
(Intercept)-Speothos venaticus       -3.1509 2.0718 -7.0844 -3.2127  1.1610
(Intercept)-Tamandua tetradactyla    -0.7010 1.9179 -3.6035 -0.9875  4.2013
(Intercept)-Tapirus terrestris       -3.7718 1.0710 -6.0364 -3.6937 -1.8598
scale(elev)-Cabassous unicinctus     -0.0103 0.7951 -1.6226  0.0046  1.5342
scale(elev)-Coendou                   0.4521 0.8154 -1.0475  0.4214  2.1827
scale(elev)-Cuniculus paca           -0.0356 0.7098 -1.4452 -0.0250  1.3337
scale(elev)-Dasyprocta fuliginosa     0.4158 0.7421 -1.0180  0.4074  1.9160
scale(elev)-Dasypus novemcinctus      0.2868 0.7153 -1.1006  0.2857  1.7301
scale(elev)-Didelphis marsupialis     0.0220 0.8211 -1.6078  0.0385  1.6332
scale(elev)-Eira barbara              0.2015 0.7736 -1.2403  0.1845  1.7841
scale(elev)-Galictis vittata          0.0718 0.8175 -1.5567  0.0779  1.6193
scale(elev)-Leopardus                 0.0124 0.8246 -1.7004  0.0367  1.6183
scale(elev)-Mazama americana          0.0943 0.7836 -1.4347  0.1038  1.6575
scale(elev)-Myoprocta pratti          0.2028 0.7189 -1.1817  0.1991  1.6365
scale(elev)-Nasua nasua               0.0527 0.7630 -1.4399  0.0632  1.5997
scale(elev)-Panthera onca            -0.0585 0.7892 -1.6926 -0.0477  1.4454
scale(elev)-Pecari tajacu             0.0577 0.8014 -1.4870  0.0695  1.6154
scale(elev)-Puma concolor            -0.0350 0.7969 -1.6315 -0.0093  1.4907
scale(elev)-Speothos venaticus        0.4087 0.7957 -1.0495  0.3891  2.0876
scale(elev)-Tamandua tetradactyla     0.1734 0.7958 -1.3354  0.1644  1.8019
scale(elev)-Tapirus terrestris       -0.1162 0.8014 -1.8196 -0.0736  1.3446
factor(in_AP)1-Cabassous unicinctus  -0.7269 1.9006 -4.7769 -0.6542  2.7499
factor(in_AP)1-Coendou                0.0557 1.9287 -3.4537 -0.0015  4.0973
factor(in_AP)1-Cuniculus paca        -0.5701 1.7590 -4.1106 -0.5554  2.8054
factor(in_AP)1-Dasyprocta fuliginosa  0.1250 1.8592 -3.2592  0.0772  3.9660
factor(in_AP)1-Dasypus novemcinctus  -0.1405 1.7838 -3.6023 -0.1156  3.3838
factor(in_AP)1-Didelphis marsupialis -0.5953 1.9121 -4.6882 -0.5549  2.9858
factor(in_AP)1-Eira barbara          -0.1080 1.9464 -3.7221 -0.1555  3.9745
factor(in_AP)1-Galictis vittata      -0.5671 1.8545 -4.2783 -0.5501  2.8810
factor(in_AP)1-Leopardus             -0.6177 1.9173 -4.5919 -0.5213  2.9021
factor(in_AP)1-Mazama americana      -0.4355 1.8491 -4.1216 -0.4489  3.2084
factor(in_AP)1-Myoprocta pratti      -0.4033 1.7726 -3.9496 -0.4079  3.0311
factor(in_AP)1-Nasua nasua           -0.4554 1.8282 -4.1406 -0.4114  3.0325
factor(in_AP)1-Panthera onca         -0.8269 1.9289 -5.0110 -0.7403  2.5568
factor(in_AP)1-Pecari tajacu         -0.5518 1.9810 -4.5634 -0.5506  3.1582
factor(in_AP)1-Puma concolor         -0.7231 1.8940 -4.6968 -0.6211  2.7517
factor(in_AP)1-Speothos venaticus    -0.0425 1.9422 -3.6583 -0.1278  3.9264
factor(in_AP)1-Tamandua tetradactyla -0.2259 1.9097 -3.7931 -0.2644  3.7070
factor(in_AP)1-Tapirus terrestris    -0.8839 1.9006 -5.0176 -0.8356  2.5788
scale(FLII)-Cabassous unicinctus     -1.0168 0.6930 -2.5501 -0.9626  0.1412
scale(FLII)-Coendou                   0.4073 1.0112 -1.3835  0.3176  2.7329
scale(FLII)-Cuniculus paca           -0.3199 0.5053 -1.4167 -0.2843  0.5865
scale(FLII)-Dasyprocta fuliginosa    -0.7356 0.6143 -2.0894 -0.6863  0.2871
scale(FLII)-Dasypus novemcinctus     -0.4116 0.5372 -1.5246 -0.3840  0.5704
scale(FLII)-Didelphis marsupialis     0.3626 0.9887 -1.4839  0.2839  2.6071
scale(FLII)-Eira barbara              0.0951 0.7107 -1.4648  0.1034  1.4272
scale(FLII)-Galictis vittata         -0.4991 1.0653 -3.1583 -0.3575  1.2653
scale(FLII)-Leopardus                 0.3803 1.0770 -1.4682  0.2658  2.9072
scale(FLII)-Mazama americana          0.9323 1.0164 -0.6458  0.8016  3.2334
scale(FLII)-Myoprocta pratti          0.9450 0.8994 -0.4354  0.8044  3.0356
scale(FLII)-Nasua nasua              -0.4827 0.7135 -2.0764 -0.4092  0.7852
scale(FLII)-Panthera onca             0.5434 0.8182 -0.7759  0.4536  2.4598
scale(FLII)-Pecari tajacu             0.3740 1.1045 -1.4775  0.2461  3.0545
scale(FLII)-Puma concolor             0.3724 0.8446 -1.1195  0.2882  2.2880
scale(FLII)-Speothos venaticus        0.4722 1.0640 -1.2948  0.3391  3.0043
scale(FLII)-Tamandua tetradactyla    -0.4591 0.8587 -2.4009 -0.3876  1.0347
scale(FLII)-Tapirus terrestris        0.9849 0.9166 -0.3795  0.8441  3.2136
                                       Rhat  ESS
(Intercept)-Cabassous unicinctus     1.0461  687
(Intercept)-Coendou                  1.0037  470
(Intercept)-Cuniculus paca           1.0098  669
(Intercept)-Dasyprocta fuliginosa    1.0292  691
(Intercept)-Dasypus novemcinctus     1.0107  894
(Intercept)-Didelphis marsupialis    1.0069  301
(Intercept)-Eira barbara             1.0026  431
(Intercept)-Galictis vittata         1.0263  320
(Intercept)-Leopardus                1.0381  338
(Intercept)-Mazama americana         1.0046  568
(Intercept)-Myoprocta pratti         1.0118  970
(Intercept)-Nasua nasua              1.0221  734
(Intercept)-Panthera onca            1.0036  844
(Intercept)-Pecari tajacu            1.0432  345
(Intercept)-Puma concolor            1.0029  842
(Intercept)-Speothos venaticus       1.0537  371
(Intercept)-Tamandua tetradactyla    1.0226  295
(Intercept)-Tapirus terrestris       1.0047  773
scale(elev)-Cabassous unicinctus     1.0009  325
scale(elev)-Coendou                  1.0132  339
scale(elev)-Cuniculus paca           1.0082  293
scale(elev)-Dasyprocta fuliginosa    1.0151  303
scale(elev)-Dasypus novemcinctus     1.0155  303
scale(elev)-Didelphis marsupialis    1.0072  305
scale(elev)-Eira barbara             1.0097  356
scale(elev)-Galictis vittata         1.0046  314
scale(elev)-Leopardus                1.0002  357
scale(elev)-Mazama americana         1.0013  321
scale(elev)-Myoprocta pratti         1.0045  296
scale(elev)-Nasua nasua              1.0037  288
scale(elev)-Panthera onca            1.0113  317
scale(elev)-Pecari tajacu            1.0005  377
scale(elev)-Puma concolor            1.0059  379
scale(elev)-Speothos venaticus       1.0130  396
scale(elev)-Tamandua tetradactyla    1.0082  402
scale(elev)-Tapirus terrestris       1.0043  383
factor(in_AP)1-Cabassous unicinctus  1.0243  300
factor(in_AP)1-Coendou               1.0012  310
factor(in_AP)1-Cuniculus paca        1.0068  243
factor(in_AP)1-Dasyprocta fuliginosa 1.0018  287
factor(in_AP)1-Dasypus novemcinctus  1.0042  283
factor(in_AP)1-Didelphis marsupialis 1.0104  311
factor(in_AP)1-Eira barbara          1.0009  307
factor(in_AP)1-Galictis vittata      1.0034  279
factor(in_AP)1-Leopardus             1.0073  322
factor(in_AP)1-Mazama americana      1.0061  282
factor(in_AP)1-Myoprocta pratti      1.0071  269
factor(in_AP)1-Nasua nasua           1.0078  264
factor(in_AP)1-Panthera onca         1.0087  324
factor(in_AP)1-Pecari tajacu         1.0193  298
factor(in_AP)1-Puma concolor         1.0111  310
factor(in_AP)1-Speothos venaticus    1.0016  334
factor(in_AP)1-Tamandua tetradactyla 1.0031  312
factor(in_AP)1-Tapirus terrestris    1.0096  290
scale(FLII)-Cabassous unicinctus     1.0118 1096
scale(FLII)-Coendou                  1.0043 1242
scale(FLII)-Cuniculus paca           1.0070 1952
scale(FLII)-Dasyprocta fuliginosa    1.0006 1403
scale(FLII)-Dasypus novemcinctus     1.0046 1912
scale(FLII)-Didelphis marsupialis    1.0045 1439
scale(FLII)-Eira barbara             1.0029 1767
scale(FLII)-Galictis vittata         1.0054  883
scale(FLII)-Leopardus                1.0128  740
scale(FLII)-Mazama americana         1.0071  760
scale(FLII)-Myoprocta pratti         1.0111 1118
scale(FLII)-Nasua nasua              1.0088 1470
scale(FLII)-Panthera onca            1.0022 1474
scale(FLII)-Pecari tajacu            1.0077  796
scale(FLII)-Puma concolor            1.0061 1505
scale(FLII)-Speothos venaticus       1.0046  909
scale(FLII)-Tamandua tetradactyla    1.0225 1063
scale(FLII)-Tapirus terrestris       1.0106  816

Detection (logit scale): 
                                       Mean     SD    2.5%     50%   97.5%
(Intercept)-Cabassous unicinctus    -2.4851 0.7730 -4.1419 -2.4247 -1.1347
(Intercept)-Coendou                 -3.4320 1.2406 -5.8909 -3.4184 -1.0581
(Intercept)-Cuniculus paca          -0.1105 0.1324 -0.3714 -0.1085  0.1374
(Intercept)-Dasyprocta fuliginosa   -0.6898 0.1583 -1.0079 -0.6871 -0.3829
(Intercept)-Dasypus novemcinctus    -0.1587 0.1489 -0.4495 -0.1569  0.1333
(Intercept)-Didelphis marsupialis   -3.6001 1.0085 -5.5356 -3.6189 -1.6318
(Intercept)-Eira barbara            -2.7524 0.3461 -3.4472 -2.7549 -2.0858
(Intercept)-Galictis vittata        -3.8043 1.2003 -6.0692 -3.8168 -1.4391
(Intercept)-Leopardus               -3.6796 1.0176 -5.6716 -3.6724 -1.7777
(Intercept)-Mazama americana        -2.7969 0.5205 -3.8185 -2.7862 -1.8089
(Intercept)-Myoprocta pratti        -0.8651 0.5105 -1.9547 -0.8507  0.0949
(Intercept)-Nasua nasua             -2.2986 0.3258 -2.9371 -2.2840 -1.7034
(Intercept)-Panthera onca           -1.0784 0.5287 -2.2165 -1.0354 -0.1278
(Intercept)-Pecari tajacu           -3.8346 1.2116 -6.1795 -3.8567 -1.5436
(Intercept)-Puma concolor           -2.1948 0.7460 -3.7684 -2.1435 -0.8914
(Intercept)-Speothos venaticus      -3.7074 1.2149 -6.0807 -3.7154 -1.3665
(Intercept)-Tamandua tetradactyla   -3.2761 0.5980 -4.4721 -3.2799 -2.0895
(Intercept)-Tapirus terrestris      -0.4445 0.3873 -1.2379 -0.4357  0.2817
scale(effort)-Cabassous unicinctus   0.2025 0.3435 -0.5058  0.2136  0.8672
scale(effort)-Coendou                0.4346 0.3754 -0.2774  0.4290  1.2421
scale(effort)-Cuniculus paca         0.4540 0.1604  0.1543  0.4525  0.7848
scale(effort)-Dasyprocta fuliginosa  0.7235 0.2522  0.2862  0.6999  1.2837
scale(effort)-Dasypus novemcinctus   0.4952 0.1772  0.1570  0.4833  0.8582
scale(effort)-Didelphis marsupialis  0.5238 0.3855 -0.2092  0.5033  1.3484
scale(effort)-Eira barbara           0.4360 0.2839 -0.0702  0.4220  1.0690
scale(effort)-Galictis vittata       0.4279 0.3685 -0.2726  0.4270  1.1894
scale(effort)-Leopardus              0.4990 0.4023 -0.2495  0.4713  1.3957
scale(effort)-Mazama americana       0.6527 0.3758  0.0361  0.6173  1.5209
scale(effort)-Myoprocta pratti       0.6277 0.3931 -0.0388  0.5902  1.4947
scale(effort)-Nasua nasua            0.6603 0.3289  0.0992  0.6339  1.3833
scale(effort)-Panthera onca          0.4926 0.3608 -0.2059  0.4807  1.2570
scale(effort)-Pecari tajacu          0.4776 0.3866 -0.2461  0.4680  1.2966
scale(effort)-Puma concolor          0.2873 0.3618 -0.4280  0.2940  0.9962
scale(effort)-Speothos venaticus     0.4853 0.3948 -0.2631  0.4669  1.3459
scale(effort)-Tamandua tetradactyla  0.2350 0.3189 -0.3699  0.2337  0.8722
scale(effort)-Tapirus terrestris     0.2700 0.3303 -0.4267  0.2786  0.9121
                                      Rhat  ESS
(Intercept)-Cabassous unicinctus    1.0251 1138
(Intercept)-Coendou                 1.0058  672
(Intercept)-Cuniculus paca          1.0000 3000
(Intercept)-Dasyprocta fuliginosa   1.0106 3176
(Intercept)-Dasypus novemcinctus    1.0009 3000
(Intercept)-Didelphis marsupialis   1.0130  512
(Intercept)-Eira barbara            1.0029  946
(Intercept)-Galictis vittata        1.0032  623
(Intercept)-Leopardus               1.0615  442
(Intercept)-Mazama americana        1.0010  880
(Intercept)-Myoprocta pratti        1.0020 3000
(Intercept)-Nasua nasua             1.0026 1529
(Intercept)-Panthera onca           1.0003 3000
(Intercept)-Pecari tajacu           1.0424  317
(Intercept)-Puma concolor           1.0053  993
(Intercept)-Speothos venaticus      1.0530  452
(Intercept)-Tamandua tetradactyla   1.0096  609
(Intercept)-Tapirus terrestris      1.0006 3000
scale(effort)-Cabassous unicinctus  1.0004 2815
scale(effort)-Coendou               1.0010 2834
scale(effort)-Cuniculus paca        1.0026 2684
scale(effort)-Dasyprocta fuliginosa 1.0060 2772
scale(effort)-Dasypus novemcinctus  1.0028 2833
scale(effort)-Didelphis marsupialis 1.0046 2576
scale(effort)-Eira barbara          1.0000 2716
scale(effort)-Galictis vittata      1.0000 2387
scale(effort)-Leopardus             1.0020 2012
scale(effort)-Mazama americana      1.0062 2116
scale(effort)-Myoprocta pratti      1.0013 2692
scale(effort)-Nasua nasua           1.0001 2703
scale(effort)-Panthera onca         1.0014 3000
scale(effort)-Pecari tajacu         1.0002 3167
scale(effort)-Puma concolor         1.0057 2797
scale(effort)-Speothos venaticus    1.0029 2551
scale(effort)-Tamandua tetradactyla 1.0009 2593
scale(effort)-Tapirus terrestris    1.0013 2778

----------------------------------------
    Spatial Covariance
----------------------------------------
        Mean     SD  2.5%    50%  97.5%   Rhat  ESS
phi-1 0.0076 0.0036 7e-04 0.0078 0.0133 0.9999  914
phi-2 0.0070 0.0039 4e-04 0.0071 0.0133 1.0032  662
phi-3 0.0071 0.0038 6e-04 0.0071 0.0132 1.0004 1052
phi-4 0.0065 0.0040 3e-04 0.0063 0.0132 1.0016  705
phi-5 0.0067 0.0040 4e-04 0.0067 0.0132 0.9998 1084

Bayesian p-values can be inspected to check for lack of fit (overall or by species). Lack of fit at significance level = 0.05 is indicated by Bayesian p-values below 0.025 or greater than 0.975. The overall Bayesian p-value (Bpvalue) indicates no problems with lack of fit. Likewise, species-level Bayesian p-values (Bpvalue_species) indicate no lack of fit for any species.

Gelman and Rubin’s convergence diagnostic: Approximate convergence is diagnosed when the upper limit is close to 1.

Convergence is diagnosed when the chains have ‘forgotten’ their initial values, and the output from all chains is indistinguishable. The gelman.diag diagnostic is applied to a single variable from the chain. It is based a comparison of within-chain and between-chain variances, and is similar to a classical analysis of variance.

Values substantially above 1 indicate lack of convergence.

Model Diagnostics

Code
# Extract posterior draws for later use
posterior1 <- as.array(out.sp.cat)

# Trace plots to check chain mixing. Extract posterior samples and bind in a single matrix.
POSTERIOR.MATRIX <- cbind(out.sp.cat$alpha.comm.samples, 
                          out.sp.cat$beta.comm.samples,  
                          out.sp.cat$alpha.samples, 
                          out.sp.cat$beta.samples)

# Matrix output is all chains combined, split into 3 chains.
CHAIN.1 <- as.mcmc(POSTERIOR.MATRIX[1:1000,])
CHAIN.2 <- as.mcmc(POSTERIOR.MATRIX[1001:2000,])
CHAIN.3 <- as.mcmc(POSTERIOR.MATRIX[2001:3000,])
# CHAIN.4 <- as.mcmc(POSTERIOR.MATRIX[8001:10000,])

# Bind four chains as coda mcmc.list object.
POSTERIOR.CHAINS <- mcmc.list(CHAIN.1, CHAIN.2, CHAIN.3)#, CHAIN.4)

# Create an empty folder.
# dir.create ("Beetle_plots")

# Plot chain mixing of each parameter to a multi-panel plot and save to the new folder. ART 5 mins

######################################
#### SAVE Diagnostics at PDF
# MCMCtrace(POSTERIOR.CHAINS, params = "all", Rhat = TRUE, n.eff = TRUE)#, pdf = TRUE, filename = "Beetle_240909_traceplots.pdf", wd = "Beetle_plots")



#mcmc_trace(fit.commu, parms = c("beta.ranef.cont.border_dist.mean"))

#posterior2 <- extract(fit.commu, inc_warmup = TRUE, permuted = FALSE)

#color_scheme_set("mix-blue-pink")
#p <- mcmc_trace(posterior1,  pars = c("mu", "tau"), n_warmup = 300,
#                facet_args = list(nrow = 2, labeller = label_parsed))
#p + facet_text(size = 15)



#outMCMC <- fit.commu #Convert output to MCMC object
#diagnostics chains 

# all as pdf
# MCMCtrace(outMCMC)

# MCMCtrace(outMCMC, params = c("alpha0"), type = 'trace', Rhat = TRUE, n.eff = TRUE)

# MCMCtrace(outMCMC, params = c("beta0"), type = 'trace', Rhat = TRUE, n.eff = TRUE)

# MCMCtrace(outMCMC, params = c("beta.ranef.cont.border_dist"), type = 'trace', Rhat = TRUE, n.eff = TRUE)

# MCMCtrace(out.sp, params = c("beta.ranef.cont.border_dist.mean"), type = 'trace', pdf = F, Rhat = TRUE, n.eff = TRUE)

# MCMCtrace(outMCMC, params = c("beta.ranef.cont.elev"), type = 'trace', Rhat = TRUE, n.eff = TRUE)

# MCMCtrace(outMCMC, params = c("beta.ranef.cont.elev.mean"), type = 'trace', pdf = F, Rhat = TRUE, n.eff = TRUE)


### density
# MCMCtrace(outMCMC, params = c("Nspecies"), ISB = FALSE, pdf = F, exact = TRUE, post_zm = TRUE, type = 'density', Rhat = TRUE, n.eff = TRUE, ind = TRUE)

### density
# MCMCtrace(outMCMC, params = c("beta.ranef.cont.elev.mean"), ISB = FALSE, pdf = F, exact = TRUE, post_zm = TRUE, type = 'density', Rhat = TRUE, n.eff = TRUE, ind = TRUE)

### density
#MCMCtrace(outMCMC, params = c("beta.ranef.cont.border_dist.mean"), ISB = FALSE, pdf = F, exact = TRUE, post_zm = TRUE, type = 'density', Rhat = TRUE, n.eff = TRUE, ind = TRUE)

#coda::gelman.diag(outMCMC,  multivariate = FALSE, transform=FALSE)
                    
# coda::gelman.plot(outMCMC,  multivariate = FALSE)



# 
# mcmc_intervals(outMCMC, pars = c("Nspecies_in_AP[1]",
#                                  "Nspecies_in_AP[2]"),
#                point_est = "mean",
#                prob = 0.75, prob_outer = 0.95) +
#   ggtitle("Number of species") + 
#   scale_y_discrete(labels = c("Nspecies_in_AP[1]"=levels(sitecovs$in_AP)[1],
#              "Nspecies_in_AP[2]"=levels(sitecovs$in_AP)[2]))
# 
# #Continuous
# p <- mcmc_intervals(outMCMC, 
#                pars = c("beta.ranef.cont.border_dist.mean",
#                          #"beta.ranef.cont.elev.mean",
#                         "beta.ranef.categ.in_AP.mean[2]"))
# 
# # relabel parameters
# p + scale_y_discrete(
#   labels = c("beta.ranef.cont.border_dist.mean"="Dist_border",
#                          #"beta.ranef.cont.elev.mean"="Elevation",
#                         "beta.ranef.categ.in_AP.mean[2]"="in_AP")
# ) +
#   ggtitle("Treatment effect on all species")
# 

Posterior Summary (effect)

Community effects

Code
# Create simple plot summaries using MCMCvis package.
# Detection covariate effects --------- 
MCMCplot(out.sp.cat$alpha.comm.samples, ref_ovl = TRUE, ci = c(50, 95))
# Occupancy community-level effects 
MCMCplot(out.sp.cat$beta.comm.samples, ref_ovl = TRUE, ci = c(50, 95))
(a) Detection
(b) Occupancy
Figure 1: Community effects

Species effects

Code
# Occupancy species-level effects 
MCMCplot(out.sp.cat$beta.samples[,19:36], ref_ovl = TRUE, ci = c(50, 95))

# Occupancy species-level effects 
MCMCplot(out.sp$beta.samples[,37:54], ref_ovl = TRUE, ci = c(50, 95))

# Occupancy species-level effects 
MCMCplot(out.sp.cat$beta.samples[,55:72], ref_ovl = TRUE, ci = c(50, 95))
(a) elevation
(b) distance border
(c) FLII
Figure 2: Species effects

Species effects using bayesplot

Code
library(bayesplot)
#mcmc_areas(outMCMC, regex_pars = "Nspecies_in_AP")
# mcmc_areas(outMCMC, regex_pars = "Nspecies_in_AP")

mcmc_intervals(out.sp$beta.samples[,27:52] , point_est = "mean",
               prob = 0.75, prob_outer = 0.95) + 
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", size = 0.5)

mcmc_intervals(out.sp$beta.samples[,53:78] , point_est = "mean",
               prob = 0.75, prob_outer = 0.95) + 
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", size = 0.5)

mcmc_intervals(out.sp$beta.samples[,79:104] , point_est = "mean",
               prob = 0.75, prob_outer = 0.95) + 
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", size = 0.5)

Prediction as graph

This prediction uses the non spatial model

Code
# 6. Prediction -----------------------------------------------------------
# Predict occupancy along a gradient of elev   
# Create a set of values across the range of observed elev values
elev.pred.vals <- seq(min(data_list$occ.covs$elev), 
                              max(data_list$occ.covs$elev), 
                              length.out = 100)
# Scale predicted values by mean and standard deviation used to fit the model
elev.pred.vals.scale <- (elev.pred.vals - mean(data_list$occ.covs$elev)) / 
                             sd(data_list$occ.covs$elev)
# Create a set of values across the range of observed elev values
border_dist.pred.vals <- seq(min(data_list$occ.covs$border_dist), 
                              max(data_list$occ.covs$border_dist), 
                              length.out = 100)
# Scale predicted values by mean and standard deviation used to fit the model
border_dist.pred.vals.scale <- (border_dist.pred.vals -
                                  mean(data_list$occ.covs$border_dist)) /
                                  sd(data_list$occ.covs$border_dist)

# Create a set of values across the range of observed FLII values
FLII.pred.vals <- seq(min(data_list$occ.covs$FLII), 
                              max(data_list$occ.covs$FLII), 
                              length.out = 100)
# Scale predicted values by mean and standard deviation used to fit the model
FLII.pred.vals.scale <- (FLII.pred.vals -
                                  mean(data_list$occ.covs$FLII)) /
                                  sd(data_list$occ.covs$FLII)

# Predict occupancy across elev  values at mean values of all other variables
pred.df1 <- as.matrix(data.frame(intercept = 1, 
                                 elev = elev.pred.vals.scale, 
                                 border_dist = 0,
                                 FLII=0))#, catchment = 0, density = 0, 
                         # slope = 0))
# Predict occupancy across elev  values at mean values of all other variables
pred.df2 <- as.matrix(data.frame(intercept = 1, elev = 0, 
                         border_dist = border_dist.pred.vals.scale,
                         FLII= 0 ))#, catchment = 0, density = 0, 
                         # slope = 0))
# Predict occupancy across elev  values at mean values of all other variables
pred.df3 <- as.matrix(data.frame(intercept = 1, elev = 0, 
                         border_dist = 0,
                         FLII= FLII.pred.vals.scale ))#, catchment = 0, density = 0, 
                         # slope = 0))

out.pred1 <- predict(out.null, pred.df1) # using non spatial
str(out.pred1)
psi.0.quants <- apply(out.pred1$psi.0.samples, c(2, 3), quantile, 
                          prob = c(0.025, 0.5, 0.975))
sp.codes <- attr(data_list$y, "dimnames")[[1]]
psi.plot.dat <- data.frame(psi.med = c(t(psi.0.quants[2, , ])), 
                                 psi.low = c(t(psi.0.quants[1, , ])), 
                                 psi.high = c(t(psi.0.quants[3, , ])), 
                           elev = elev.pred.vals, 
                                 sp.codes = rep(sp.codes, 
                                                each = length(elev.pred.vals)))

ggplot(psi.plot.dat, aes(x = elev, y = psi.med)) + 
  geom_ribbon(aes(ymin = psi.low, ymax = psi.high), fill = 'grey70') +
  geom_line() + 
  facet_wrap(vars(sp.codes)) + 
  theme_bw() + 
  labs(x = 'elevation (m)', y = 'Occupancy Probability') 


out.pred2 <- predict(out.null, pred.df2) # using non spatial
str(out.pred2)
psi.0.quants <- apply(out.pred2$psi.0.samples, c(2, 3), quantile, 
                          prob = c(0.025, 0.5, 0.975))
sp.codes <- attr(data_list$y, "dimnames")[[1]]
psi.plot.dat <- data.frame(psi.med = c(t(psi.0.quants[2, , ])), 
                                 psi.low = c(t(psi.0.quants[1, , ])), 
                                 psi.high = c(t(psi.0.quants[3, , ])), 
                           border_dist = border_dist.pred.vals, 
                                 sp.codes = rep(sp.codes, 
                                                each = length(border_dist.pred.vals)))

ggplot(psi.plot.dat, aes(x = border_dist/1000, y = psi.med)) + 
  geom_ribbon(aes(ymin = psi.low, ymax = psi.high), fill = 'grey70') +
  geom_line() + 
  facet_wrap(vars(sp.codes)) + 
  theme_bw() + 
  labs(x = 'border_dist (Km)', y = 'Occupancy Probability') 


out.pred3 <- predict(out.null, pred.df3) # using non spatial
str(out.pred3)
psi.0.quants <- apply(out.pred3$psi.0.samples, c(2, 3), quantile, 
                          prob = c(0.025, 0.5, 0.975))
sp.codes <- attr(data_list$y, "dimnames")[[1]]
psi.plot.dat <- data.frame(psi.med = c(t(psi.0.quants[2, , ])), 
                                 psi.low = c(t(psi.0.quants[1, , ])), 
                                 psi.high = c(t(psi.0.quants[3, , ])), 
                           FLII = FLII.pred.vals, 
                                 sp.codes = rep(sp.codes, 
                                                each = length(FLII.pred.vals)))

ggplot(psi.plot.dat, aes(x = FLII/1000, y = psi.med)) + 
  geom_ribbon(aes(ymin = psi.low, ymax = psi.high), fill = 'grey70') +
  geom_line() + 
  facet_wrap(vars(sp.codes)) + 
  theme_bw() + 
  labs(x = 'FLII', y = 'Occupancy Probability') 

Spatial Prediction in elevation_EC

Code
#aggregate  resolution to (factor = 3)
#transform coord data to UTM
elevation_UTM <- project(elevation_EC, "EPSG:10603")
elevation_17.aggregate <- aggregate(elevation_UTM, fact=10)
res(elevation_17.aggregate)

# Convert the SpatRaster to a data frame with coordinates
df_coords <- as.data.frame(elevation_17.aggregate, xy = TRUE)
names(df_coords) <-c("X","Y","elev")

elev.pred <- (df_coords$elev - mean(data_list$occ.covs$elev)) / sd(data_list$occ.covs$elev)


############### Predict new data ############### 
# we predict at one covariate varing and others at mean value = 0
# intercept = 1, elev = 0, border_dist = 0, FLII= 0
# in that order
################ ################ ################ 
predict_data <- cbind(1, elev.pred, 0, 0)
colnames(predict_data) <- c("intercept",
                            "elev",
                            "border_dist",
                            "FLII" )

# X.0 <- cbind(1, 1, elev.pred)#, elev.pred^2)

# coords.0 <- as.matrix(hbefElev[, c('Easting', 'Northing')])
out.sp.ms.pred <- predict(out.sp, 
                          X.0=predict_data, 
                          df_coords[,1:2],
                          threads=4) #Warning :'threads' is not an argument

# extract the array of interest= occupancy
predicted_array <- out.sp.ms.pred$psi.0.samples



dim(predicted_array)
# df_plot <- NA # empty column
predicted_raster_list <- list() # rast(nrows = 52, ncols = 103,
                         #ext(elevation_17.aggregate), 
                         # crs = "EPSG:32717")

###################################################
# simpler way to make mean in the array by species
# array order: itera=3000, sp=22, pixels=5202
# we wan to keep index 2 and 3
library(plyr)
aresult = (plyr::aaply(predicted_array, # mean fo all iterations
                       c(2,3), 
                       mean))

##############################################
#### Loop to make rasters and put in a list
# array order: itera=3000, sp=22, pixels=5202
for (i in 1:dim(predicted_array)[2]){
 # Convert array slice to data frame
 # df_plot[i] <- as.data.frame(predicted_array[1, i, ]) # sp_i
 # df_plot[i] <- as.vector(aresult[i,]) # Extract sp
 # Producing an SDM for all (posterior mean)
 plot.dat <- data.frame(x = df_coords$X, 
                        y = df_coords$Y, 
                        psi.sp = as.vector(aresult[i,]))
 pred_rast <- rast(plot.dat, 
                   type = "xyz", 
                   crs = "EPSG:10603") # Replace EPSG:4326 with your CRS
 predicted_raster_list[[i]] <- pred_rast
}

# Convert the list to a SpatRaster stack
predictad_raster_stack <- rast(predicted_raster_list)
# put species names
names(predictad_raster_stack) <- selected.sp

plot(predictad_raster_stack)

# get the mean
predicted_mean <- mean(predictad_raster_stack)

plot(predicted_mean, main="mean occupancy")

mapview(predicted_mean) + mapview(AP_Yasuni_UTM_line)
# 

Package Citation

Code
pkgs <- cite_packages(output = "paragraph", pkgs="Session", out.dir = ".")
# knitr::kable(pkgs)
pkgs

We used R v. 4.4.2 (R Core Team 2024) and the following R packages: abind v. 1.4.8 (Plate and Heiberger 2024), bayesplot v. 1.14.0 (Gabry et al. 2019; Gabry and Mahr 2025), beepr v. 2.0 (Bååth 2024), camtrapR v. 3.0.0 (Niedballa et al. 2016), coda v. 0.19.4.1 (Plummer et al. 2006), DT v. 0.34.0 (Xie et al. 2025), elevatr v. 0.99.0 (Hollister et al. 2023), maps v. 3.4.3 (Becker et al. 2025), mapview v. 2.11.4 (Appelhans et al. 2025), MCMCvis v. 0.16.3 (Youngflesh 2018), sf v. 1.0.21 (Pebesma 2018; Pebesma and Bivand 2023), snow v. 0.4.4 (Tierney et al. 2021), snowfall v. 1.84.6.3 (Knaus 2023), spOccupancy v. 0.8.0 (Doser et al. 2022, 2024; Doser, Finley, and Banerjee 2023), terra v. 1.8.70 (Hijmans 2025), tictoc v. 1.2.1 (Izrailev 2024), tidyverse v. 2.0.0 (Wickham et al. 2019), tmap v. 4.2 (Tennekes 2018).

Sesion info

Code
print(sessionInfo(), locale = FALSE)
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: internal


attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] abind_1.4-8       lubridate_1.9.4   forcats_1.0.0     stringr_1.5.2    
 [5] dplyr_1.1.4       purrr_1.1.0       readr_2.1.5       tidyr_1.3.1      
 [9] tibble_3.2.1      ggplot2_4.0.0     tidyverse_2.0.0   spOccupancy_0.8.0
[13] camtrapR_3.0.0    snowfall_1.84-6.3 snow_0.4-4        beepr_2.0        
[17] coda_0.19-4.1     MCMCvis_0.16.3    tictoc_1.2.1      bayesplot_1.14.0 
[21] elevatr_0.99.0    terra_1.8-70      tmap_4.2          maps_3.4.3       
[25] mapview_2.11.4    sf_1.0-21         DT_0.34.0         readxl_1.4.3     
[29] grateful_0.3.0   

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3      rstudioapi_0.17.1       audio_0.1-11           
  [4] jsonlite_2.0.0          wk_0.9.4                magrittr_2.0.3         
  [7] farver_2.1.2            nloptr_2.1.1            rmarkdown_2.30         
 [10] vctrs_0.6.5             minqa_1.2.8             base64enc_0.1-3        
 [13] RcppNumerical_0.6-0     progress_1.2.3          htmltools_0.5.8.1      
 [16] leafsync_0.1.0          curl_7.0.0              raster_3.6-32          
 [19] cellranger_1.1.0        s2_1.1.9                sass_0.4.10            
 [22] bslib_0.9.0             slippymath_0.3.1        KernSmooth_2.23-24     
 [25] htmlwidgets_1.6.4       cachem_1.1.0            stars_0.6-8            
 [28] uuid_1.2-1              mime_0.13               lifecycle_1.0.4        
 [31] iterators_1.0.14        pkgconfig_2.0.3         cols4all_0.8-1         
 [34] Matrix_1.7-1            R6_2.6.1                fastmap_1.2.0          
 [37] shiny_1.9.1             digest_0.6.37           colorspace_2.1-1       
 [40] leafem_0.2.4            crosstalk_1.2.1         lwgeom_0.2-14          
 [43] progressr_0.15.0        spacesXYZ_1.6-0         timechange_0.3.0       
 [46] httr_1.4.7              mgcv_1.9-1              compiler_4.4.2         
 [49] microbenchmark_1.5.0    proxy_0.4-27            withr_3.0.2            
 [52] doParallel_1.0.17       brew_1.0-10             S7_0.2.0               
 [55] DBI_1.2.3               logger_0.4.0            MASS_7.3-61            
 [58] maptiles_0.10.0         tmaptools_3.3           leaflet_2.2.3          
 [61] classInt_0.4-11         tools_4.4.2             units_0.8-7            
 [64] leaflegend_1.2.1        httpuv_1.6.16           glue_1.8.0             
 [67] satellite_1.0.5         nlme_3.1-166            promises_1.3.3         
 [70] grid_4.4.2              generics_0.1.3          leaflet.providers_2.0.0
 [73] gtable_0.3.6            tzdb_0.4.0              shinyBS_0.61.1         
 [76] class_7.3-22            data.table_1.17.8       hms_1.1.3              
 [79] sp_2.2-0                RANN_2.6.2              foreach_1.5.2          
 [82] pillar_1.11.1           later_1.4.2             splines_4.4.2          
 [85] lattice_0.22-6          tidyselect_1.2.1        knitr_1.50             
 [88] svglite_2.1.3           stats4_4.4.2            xfun_0.52              
 [91] shinydashboard_0.7.3    leafpop_0.1.0           stringi_1.8.4          
 [94] rematch_2.0.0           yaml_2.3.10             boot_1.3-31            
 [97] evaluate_1.0.4          codetools_0.2-20        cli_3.6.5              
[100] RcppParallel_5.1.9      systemfonts_1.1.0       xtable_1.8-4           
[103] jquerylib_0.1.4         secr_5.1.0              dichromat_2.0-0.1      
[106] Rcpp_1.1.0              spAbundance_0.2.1       png_0.1-8              
[109] XML_3.99-0.18           parallel_4.4.2          prettyunits_1.2.0      
[112] lme4_1.1-35.5           mvtnorm_1.3-2           scales_1.4.0           
[115] e1071_1.7-16            crayon_1.5.3            rlang_1.1.6            

References

Appelhans, Tim, Florian Detsch, Christoph Reudenbach, and Stefan Woellauer. 2025. mapview: Interactive Viewing of Spatial Data in r. https://CRAN.R-project.org/package=mapview.
Bååth, Rasmus. 2024. beepr: Easily Play Notification Sounds on Any Platform. https://CRAN.R-project.org/package=beepr.
Becker, Richard A., Allan R. Wilks, Ray Brownrigg, Thomas P. Minka, and Alex Deckmyn. 2025. maps: Draw Geographical Maps. https://CRAN.R-project.org/package=maps.
Doser, Jeffrey W., Andrew O. Finley, and Sudipto Banerjee. 2023. “Joint Species Distribution Models with Imperfect Detection for High-Dimensional Spatial Data.” Ecology, e4137. https://doi.org/10.1002/ecy.4137.
Doser, Jeffrey W., Andrew O. Finley, Marc Kéry, and Elise F. Zipkin. 2022. spOccupancy: An r Package for Single-Species, Multi-Species, and Integrated Spatial Occupancy Models.” Methods in Ecology and Evolution 13: 1670–78. https://doi.org/10.1111/2041-210X.13897.
Doser, Jeffrey W., Andrew O. Finley, Sarah P. Saunders, Marc Kéry, Aaron S. Weed, and Elise F. Zipkin. 2024. “Modeling Complex Species-Environment Relationships Through Spatially-Varying Coefficient Occupancy Models.” Journal of Agricultural, Biological, and Environmental Statistics. https://doi.org/10.1007/s13253-023-00595-6.
Gabry, Jonah, and Tristan Mahr. 2025. bayesplot: Plotting for Bayesian Models.” https://mc-stan.org/bayesplot/.
Gabry, Jonah, Daniel Simpson, Aki Vehtari, Michael Betancourt, and Andrew Gelman. 2019. “Visualization in Bayesian Workflow.” J. R. Stat. Soc. A 182: 389–402. https://doi.org/10.1111/rssa.12378.
Hijmans, Robert J. 2025. terra: Spatial Data Analysis. https://CRAN.R-project.org/package=terra.
Hollister, Jeffrey, Tarak Shah, Jakub Nowosad, Alec L. Robitaille, Marcus W. Beck, and Mike Johnson. 2023. elevatr: Access Elevation Data from Various APIs. https://doi.org/10.5281/zenodo.8335450.
Izrailev, Sergei. 2024. tictoc: Functions for Timing r Scripts, as Well as Implementations of Stack and StackList Structures. https://CRAN.R-project.org/package=tictoc.
Knaus, Jochen. 2023. snowfall: Easier Cluster Computing (Based on snow). https://CRAN.R-project.org/package=snowfall.
Niedballa, Jürgen, Rahel Sollmann, Alexandre Courtiol, and Andreas Wilting. 2016. camtrapR: An r Package for Efficient Camera Trap Data Management.” Methods in Ecology and Evolution 7 (12): 1457–62. https://doi.org/10.1111/2041-210X.12600.
Pebesma, Edzer. 2018. Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal 10 (1): 439–46. https://doi.org/10.32614/RJ-2018-009.
Pebesma, Edzer, and Roger Bivand. 2023. Spatial Data Science: With applications in R. Chapman and Hall/CRC. https://doi.org/10.1201/9780429459016.
Plate, Tony, and Richard Heiberger. 2024. abind: Combine Multidimensional Arrays. https://CRAN.R-project.org/package=abind.
Plummer, Martyn, Nicky Best, Kate Cowles, and Karen Vines. 2006. CODA: Convergence Diagnosis and Output Analysis for MCMC.” R News 6 (1): 7–11. https://journal.r-project.org/archive/.
R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Tennekes, Martijn. 2018. tmap: Thematic Maps in R.” Journal of Statistical Software 84 (6): 1–39. https://doi.org/10.18637/jss.v084.i06.
Tierney, Luke, A. J. Rossini, Na Li, and H. Sevcikova. 2021. snow: Simple Network of Workstations. https://CRAN.R-project.org/package=snow.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Xie, Yihui, Joe Cheng, Xianying Tan, and Garrick Aden-Buie. 2025. DT: A Wrapper of the JavaScript Library DataTables. https://CRAN.R-project.org/package=DT.
Youngflesh, Casey. 2018. MCMCvis: Tools to Visualize, Manipulate, and Summarize MCMC Output.” Journal of Open Source Software 3 (24): 640. https://doi.org/10.21105/joss.00640.

Reuse

Citation

BibTeX citation:
@online{forero2025,
  author = {Forero, German and Wallace, Robert and Zapara-Rios, Galo and
    Isasi-Catalá, Emiliana and J. Lizcano, Diego},
  title = {Plantas-Putumayo {Spatial} {Factor} {Multi-Species}
    {Occupancy} {Model}},
  date = {2025-08-16},
  url = {https://dlizcano.github.io/Occu_APs_all/blog/2025-11-01-Putumayo/},
  langid = {en}
}
For attribution, please cite this work as:
Forero, German, Robert Wallace, Galo Zapara-Rios, Emiliana Isasi-Catalá, and Diego J. Lizcano. 2025. “Plantas-Putumayo Spatial Factor Multi-Species Occupancy Model.” August 16, 2025. https://dlizcano.github.io/Occu_APs_all/blog/2025-11-01-Putumayo/.