Occupancy five species. Parita Bay, Panama

spatial prediction

Authors

Diego J. Lizcano

Jorge Velásquez-Tibata

Published

March 20, 2024

Read spatial layers

grouping by 3 days

Code
# load function
source("C:/CodigoR/AudubonPanama/R/matrix_creator.R")
############### load Packages
library(readr) # readr 
library(readxl) # read excel
library(tidyverse) # 
library(unmarked) # occupancy by maximun likelihood
library(sf) # simple feature
library(mapview) # view map
library(ubms) # occupancy by Bayesian made easy
library(stocc) # spatial data for example
library(terra) 
library(raster)
library(RColorBrewer)
library(rasterVis)


############### load data
# old 
# parita_data_full <- read_delim("C:/CodigoR/AudubonPanama/data/all_records_audubon_panama_to_occu.csv", 
#                               delim = "\t", escape_double = FALSE, 
#                               col_types = cols(eventDate = col_date(format = "%Y-%m-%d")), 
#                               trim_ws = TRUE)

# new
parita_data_full <- read_excel("C:/CodigoR/AudubonPanama/data/all_records_audubon_panama_Darwin_Core_revision.xlsx", 
    col_types = c("text", "text", "text", 
        "date", "text", "text", "numeric", 
        "numeric", "text", "text", "numeric", 
        "text", "numeric", "numeric", "text", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric", "text", "text", 
        "numeric", "text", "numeric", "text", 
        "text", "text", "text", "text", "numeric", 
        "text", "numeric", "text", "numeric", 
        "text", "text"))

covs <- read_csv("C:/CodigoR/AudubonPanama/shp/sites_covs_parita_nona.csv")

## Nyctidromus albicollis
## Aramus guarauna
## Buteogallus anthracinus
## Dryocopus lineatus
## Piranga rubra

# filter by threshold and species
parita_data_5sp <-  parita_data_full %>% 
                      filter(confidence >= threshold) %>%
                      filter(scientificName == c("Nyctidromus albicollis", 
                                                 "Aramus guarauna",
                                                 "Buteogallus anthracinus",
                                                 "Dryocopus lineatus",
                                                 "Piranga rubra"
                                                 ))  

# insert Max and min to get matrix limits
# start_date
parita_data2 <- parita_data_5sp %>% 
  group_by(locationID) %>% 
  mutate(start_date = min(eventDate),
         end_date= max(eventDate))

# apply function to get matrix per species
parita <- f.matrix.creator2(data = parita_data2, year = 2023)


# centroide for terra
# centoide <- centroids(puntos, TRUE)
centroide <- c(mean(as.numeric(parita_data_5sp$decimalLongitude)), mean(as.numeric(parita_data_5sp$decimalLatitude)))
clip_window <- extent(-80.67105, -80.05822, 7.713744, 8.489888) # 7.932311, -80.612233  8.360073, -80.180109
bb <- c(-80.612233, -80.180109, 7.932311, 8.360073)
srtm <- raster::getData('SRTM', lon=centroide[1], lat=centroide[2])

# crop the  raster using the vector extent
srtm_crop <- raster::crop(srtm, clip_window)
 #rast(srtm_crop) # convert to terra


# altitud <- elevation_3s(-72.893262, 7.664081007, path="data")
# altitud <- as.numeric(Camptostoma_obsoletum_occu_dia$Altitude)

# convierte covs a puntos terra
puntos <- vect(covs, geom=c("Longitude", "Latitude"), crs="EPSG:4326")
# convierte a sf
puntos_sf <- sf::st_as_sf(puntos)
# extract elev from raster
cam_covs <- raster::extract(srtm_crop, puntos_sf)

BGB_Spawn <- rast("C:/CodigoR/AudubonPanama/raster/BGB Spawn.tif")
AGB_Spawn <- rast("C:/CodigoR/AudubonPanama/raster/AGB Spawn.tif")
NDVI <- rast("C:/CodigoR/AudubonPanama/raster/S2_NDVI_median_v2.tif")
roads <- rast("C:/CodigoR/AudubonPanama/raster/roads_final_v2.tif")
carbon_stock <- rast("C:/CodigoR/AudubonPanama/raster/soil_organic_carbon_stock_0-30m.tif")
canopy <- rast("C:/CodigoR/AudubonPanama/raster/canopy_height_jetz2.tif")
human_foot <- rast("C:/CodigoR/AudubonPanama/raster/human_footprint.tif")
river <- rast("C:/CodigoR/AudubonPanama/raster/rivers_final_v2.tif")

# make elevation equal
# srtm_projected <- projectRaster(srtm_crop, crs=projection(NDVI), method="ngb") 
# elev <- mask(resample(rast(srtm_projected), NDVI), NDVI)



# list of terras SpatRasters  
many_rasters <- list(BGB_Spawn, human_foot, NDVI, river, carbon_stock, canopy)
# terra stack
covs_raster <- rast(many_rasters)
names(covs_raster) <- c("BGB_Spawn", "human_foot",  "NDVI", "river", "carbon_stock",  "canopy")

# change NA to 0 
covs_raster0 <- subst(covs_raster, NA, 0)
# extract covariates
covs_raster_val <- extract(covs_raster0, puntos_sf)

plot(covs_raster)

Code
##########
# chk names of species
# dete es hora, occur es presencia en el dia
# names(parita$dete[]) 

Buteogallus anthracinus

One species, single season

Code
# Make UMF object
umf_y_full_2<- unmarkedFrameOccu(y=f.shrink.matrix.to9( 
                                   as.data.frame(parita$occur[[2]])
                                   )) #"Aramus guarauna"
siteCovs(umf_y_full_2) <- data.frame(BGB_Spawn = covs_raster_val$BGB_Spawn,
                                   human_foot = covs_raster_val$human_foot,
                                   NDVI = covs_raster_val$NDVI,
                                   river = covs_raster_val$river,
                                   carbon = covs_raster_val$carbon_stock,
                                   canopy = covs_raster_val$canopy
) # data.frame(Elev=full_covs$Elev) # Full


plot(umf_y_full_2, main="Buteogallus anthracinus")  #""Buteogallus anthracinus 

Code
# obscanop<-matrix(rep(covs_raster_val$canopy_height_jetz2,27),ncol = 27) # make elevetion per observation

# detection
obsCovs(umf_y_full_2) <- list(hour = data.frame(f.shrink.matrix.h.to9(parita$dete[[2]]), 16, 9),
                              canopy = covs_raster_val$canopy ) # hora


# fit unmarked
fit_unm_sp2_0 <- unmarked::occu(~1~1, data=umf_y_full_2) # ok!
#fit_unm_sp2_0h <- unmarked::occu(~scale(hour)~1, data=umf_y_full_2) # It work!
# fit_unm_sp2_0c <- unmarked::occu(~scale(canopy)~1, data=umf_y_full_2) # It work!
fit_unm_sp2_1 <- unmarked::occu(~1~scale(BGB_Spawn), data=umf_y_full_2)
fit_unm_sp2_2 <- unmarked::occu(~1~scale(human_foot), data=umf_y_full_2)
fit_unm_sp2_3 <- unmarked::occu(~1~scale(NDVI), data=umf_y_full_2) # ok!
fit_unm_sp2_4 <- unmarked::occu(~1~scale(river), data=umf_y_full_2)
fit_unm_sp2_4s <- unmarked::occu(~1~scale(I(river^2)), data=umf_y_full_2)
fit_unm_sp2_5 <- unmarked::occu(~1~scale(carbon), data=umf_y_full_2)
fit_unm_sp2_6 <- unmarked::occu(~1~scale(canopy), data=umf_y_full_2)
fit_unm_sp2_7 <- unmarked::occu(~1~scale(canopy)+
                                scale(BGB_Spawn), data=umf_y_full_2)
fit_unm_sp2_8 <- unmarked::occu(~1~scale(BGB_Spawn)+
                                  scale(human_foot), data=umf_y_full_2, 
                                starts=c(1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp2_9 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn)+
                                  scale(carbon)+
                                  scale(river)+
                                  scale(NDVI)+
                                  scale(human_foot), data=umf_y_full_2)


# # fit list for detection
# fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
#               #"p(h) Ocu(.)"=fit_unm_sp2_0h, # NA problem!
#               #"p(c) Ocu(.)"=fit_unm_sp2_0c)#,
#               "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
#               "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
#               "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
#               "p(.) Ocu(river)"=fit_unm_sp2_4,
#               "p() Ocu(carbon)"=fit_unm_sp2_5,
#               "p() Ocu(canopy)"=fit_unm_sp2_6,
#               )

# model selection detection unmarked
# modSel(fms1) #problem!.. null model ranks better

# fit list for occupancy
fms2<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
              #"p(h) Ocu(.)"=fit_unm_sp2_0h, #,
              #"p(c) Ocu(.)"=fit_unm_sp2_0c,
              "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
              "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
              "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
              "p(.) Ocu(river)"=fit_unm_sp2_4,
              #"p(.) Ocu(river^2)"=fit_unm_sp2_4s,
              "p() Ocu(carbon)"=fit_unm_sp2_5,
              "p() Ocu(canopy)"=fit_unm_sp2_6,
              "p() Ocu(canopy+BGB_Spawn)"=fit_unm_sp2_7,
              "p() Ocu(BGB_Spawn+human_foot)"=fit_unm_sp2_8,
              "p() Ocu(ALL)"=fit_unm_sp2_9
              )

# model selection detection unmarked
modSel(fms2)
                              nPars    AIC delta  AICwt cumltvWt
p(.) Ocu(.)                       2  98.04  0.00 0.2861     0.29
p(.) Ocu(river)                   3 100.04  2.00 0.1053     0.39
p() Ocu(carbon)                   3 100.04  2.00 0.1053     0.50
p(.) Ocu(human_foot)              3 100.04  2.00 0.1053     0.60
p(.) Ocu(BGB_Spawn)               3 100.04  2.00 0.1053     0.71
p() Ocu(canopy)                   3 100.04  2.00 0.1053     0.81
p(.) Ocu(NDVI)                    3 100.04  2.00 0.1053     0.92
p() Ocu(BGB_Spawn+human_foot)     4 102.03  4.00 0.0388     0.96
p() Ocu(canopy+BGB_Spawn)         4 102.04  4.00 0.0387     1.00
p() Ocu(ALL)                      8 106.29  8.25 0.0046     1.00
Code
summary(fit_unm_sp2_0) #### Null MODEL !!!!!!!!!!

Call:
unmarked::occu(formula = ~1 ~ 1, data = umf_y_full_2)

Occupancy (logit-scale):
 Estimate   SE     z P(>|z|)
     9.08 30.2 0.301   0.764

Detection (logit-scale):
 Estimate    SE    z  P(>|z|)
     1.68 0.264 6.35 2.16e-10

AIC: 98.03685 
Number of sites: 16
optim convergence code: 0
optim iterations: 15 
Bootstrap iterations: 0 
Code
print("Predicted Occupancy values")
[1] "Predicted Occupancy values"
Code
predict(fit_unm_sp2_0, type="state")
   Predicted          SE        lower upper
1  0.9998864 0.003429411 1.715484e-22     1
2  0.9998864 0.003429411 1.715484e-22     1
3  0.9998864 0.003429411 1.715484e-22     1
4  0.9998864 0.003429411 1.715484e-22     1
5  0.9998864 0.003429411 1.715484e-22     1
6  0.9998864 0.003429411 1.715484e-22     1
7  0.9998864 0.003429411 1.715484e-22     1
8  0.9998864 0.003429411 1.715484e-22     1
9  0.9998864 0.003429411 1.715484e-22     1
10 0.9998864 0.003429411 1.715484e-22     1
11 0.9998864 0.003429411 1.715484e-22     1
12 0.9998864 0.003429411 1.715484e-22     1
13 0.9998864 0.003429411 1.715484e-22     1
14 0.9998864 0.003429411 1.715484e-22     1
15 0.9998864 0.003429411 1.715484e-22     1
16 0.9998864 0.003429411 1.715484e-22     1
Code
# ######## ubms, bayesian occupancy
# # Double formula: first part is for detection, second for occupancy
# #  form <- ~detCov1 + detCov2 ~habCov1 + habCov2 + RSR(x, y, threshold=1)
# 
# # fit stan
# fit_stan_sp2_0 <- stan_occu(~1~1, data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_0h <- stan_occu(~scale(hour)~1, data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_0c <- stan_occu(~scale(canopy)~1, data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_1 <- stan_occu(~scale(hour)~scale(elevation), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_2 <- stan_occu(~scale(hour)~scale(AGB_Spawn), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_3 <- stan_occu(~scale(hour)~scale(roads), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_4 <- stan_occu(~scale(hour)~scale(carbon), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_5 <- stan_occu(~scale(hour)~scale(NDVI), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# fit_stan_sp2_6 <- stan_occu(~scale(hour)~scale(canopy), data=umf_y_full_2, chains=3, iter=1000, cores=3)
# 
# 
# mods2 <- fitList(fit_stan_sp2_0,
#                 fit_stan_sp2_0h,
#                 fit_stan_sp2_0c,
#                 fit_stan_sp2_1,
#                 fit_stan_sp2_2,
#                 fit_stan_sp2_3,
#                 fit_stan_sp2_4,
#                 fit_stan_sp2_5,
#                 fit_stan_sp2_6)
# 
# # model selection
# round(modSel(mods2), 3)

# # plot top model
# print("Hour")
# plot_effects(fit_stan_sp2_5, "det") # Detection
# print("NDVI")
# plot_effects(fit_stan_sp2_5, "state") # Occupancy

Spatial Prediction

Code
#### predict

# 
# # srtm_crop_s <- mask(elev, NDVI)#stack(raster(elev))#, 
# #scale(roughness)) # scale altitud
# # names(srtm_crop_s) <- c("elevation")#, "roughness")
# # crs(srtm_crop_s) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# nd2 <- NDVI # mask(elev, NDVI) #data.frame(srtm_crop_s, hour=0.5)
# names(nd2) <- "NDVI"
# # newd <- data.frame(srtm_crop_s, hour=0.5)
# pred_psi_s_unm <-predict(fit_unm_sp2_5, type="state", newdata=nd2)
# #pred_psi_s_stan <-predict(fit_stan_sp2_1, submodel="state", newdata=nd, na.rm=FALSE)
# #pred_psi_ras <- raster(srtm_crop_s) # make raster = to elev 
# #pred_psi_ras[] <- pred_psi_s_stan$Predicted # drape on top
# 
# 
# # pred_psi_r <- pred_psi_s # * sd(full_covs$elevation) + mean(full_covs$elevation)
# # crs(pred_psi_ras) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# clr <- colorRampPalette(brewer.pal(9, "YlGn"))
# # mapview (pred_psi_r[[1]], col.regions = clr,  legend = TRUE, alpha=0.7)
# # plot(pred_psi_s[[1]], main="Occupancy")
# levelplot(pred_psi_s_unm[[1]], par.settings = YlOrRdTheme(), margin=FALSE, main="Ocupancy")
# 
# pal = mapviewPalette("mapviewSpectralColors")
# mapview(raster(pred_psi_s_unm[[1]]), map.types = c("Esri.WorldImagery"), alpha=0.5, col.regions = pal(10)) + mapview(puntos_sf)

Dryocopus lineatus

One species, single season

Code
# Make UMF object
# Make UMF object
umf_y_full_3<- unmarkedFrameOccu(y=f.shrink.matrix.to9( 
                                   as.data.frame(parita$occur[[3]])
                                   )) #"Aramus guarauna"
siteCovs(umf_y_full_3) <- data.frame(BGB_Spawn = covs_raster_val$BGB_Spawn,
                                   human_foot = covs_raster_val$human_foot,
                                   NDVI = covs_raster_val$NDVI,
                                   river = covs_raster_val$river,
                                   carbon_stock = covs_raster_val$carbon_stock,
                                   canopy = covs_raster_val$canopy
) # data.frame(Elev=full_covs$Elev) # Full


plot(umf_y_full_3, main="Dryocopus lineatus")  #""Buteogallus anthracinus 

Code
# obscanop<-matrix(rep(covs_raster_val$canopy_height_jetz2,27),ncol = 27) # make elevetion per observation

# detection
obsCovs(umf_y_full_3) <- list(hour = data.frame(f.shrink.matrix.h.to9(parita$dete[[3]]), 16, 9),
                              canopy = covs_raster_val$canopy ) # hora


# fit unmarked
fit_unm_sp3_0 <- unmarked::occu(~1~1, data=umf_y_full_3) # ok!
#fit_unm_sp2_0h <- unmarked::occu(~scale(hour)~1, data=umf_y_full_2) # It work!
# fit_unm_sp2_0c <- unmarked::occu(~scale(canopy)~1, data=umf_y_full_2) # It work!
fit_unm_sp3_1 <- unmarked::occu(~1~scale(BGB_Spawn), data=umf_y_full_3,  starts=c(1,1,1), method = "BFGS",
               control = list(maxit = 20000),
               engine = "C")

fit_unm_sp3_2 <- unmarked::occu(~1~scale(human_foot), data=umf_y_full_3)
fit_unm_sp3_3 <- unmarked::occu(~1~scale(NDVI), data=umf_y_full_3) # ok!
fit_unm_sp3_4 <- unmarked::occu(~1~scale(river), data=umf_y_full_3)
fit_unm_sp3_4s <- unmarked::occu(~1~scale(I(river^2)), data=umf_y_full_3)
fit_unm_sp3_5 <- unmarked::occu(~1~scale(carbon_stock), data=umf_y_full_3)
fit_unm_sp3_6 <- unmarked::occu(~1~scale(canopy), data=umf_y_full_3)
fit_unm_sp3_7 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn), data=umf_y_full_3)
fit_unm_sp3_8 <- unmarked::occu(~1~scale(BGB_Spawn)+
                                  scale(human_foot), data=umf_y_full_3, 
                                starts=c(1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp3_9 <- unmarked::occu(~1~scale(river)+ # non carbon
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_3)
fit_unm_sp3_10 <- unmarked::occu(~1~scale(canopy)+ # all carbon
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock), data=umf_y_full_3,
                                starts=c(1,1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp3_11 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock)+
                                  scale(river)+ 
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_3)

# # fit list for detection
# fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
#               #"p(h) Ocu(.)"=fit_unm_sp2_0h, # NA problem!
#               #"p(c) Ocu(.)"=fit_unm_sp2_0c)#,
#               "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
#               "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
#               "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
#               "p(.) Ocu(river)"=fit_unm_sp2_4,
#               "p() Ocu(carbon)"=fit_unm_sp2_5,
#               "p() Ocu(canopy)"=fit_unm_sp2_6,
#               )

# model selection detection unmarked
# modSel(fms1) #problem!.. null model ranks better

# fit list for occupancy
fms3<-fitList("p(.) Ocu(.)"=fit_unm_sp3_0,
              #"p(h) Ocu(.)"=fit_unm_sp2_0h, #,
              #"p(c) Ocu(.)"=fit_unm_sp2_0c,
              "p(.) Ocu(BGB_Spawn)"=fit_unm_sp3_1,
              "p(.) Ocu(human_foot)"=fit_unm_sp3_2,
              "p(.) Ocu(NDVI)"=fit_unm_sp3_3,
              "p(.) Ocu(river)"=fit_unm_sp3_4,
              #"p(.) Ocu(river^2)"=fit_unm_sp2_4s,
              "p() Ocu(carbon)"=fit_unm_sp3_5,
              "p() Ocu(canopy)"=fit_unm_sp3_6,
              "p() Ocu(canopy+BGB_Spawn)"=fit_unm_sp3_7,
              "p() Ocu(BGB_Spawn+human_foot)"=fit_unm_sp3_8,
              "p() Ocu(river+NDVI+human_foot)"=fit_unm_sp3_9,
              "p() Ocu(canopy+BGB_Spawn+carbon)"=fit_unm_sp3_10,
              "p() Ocu(ALL)"=fit_unm_sp3_11
              )

# model selection detection unmarked
modSel(fms3)
                                 nPars   AIC delta AICwt cumltvWt
p() Ocu(canopy+BGB_Spawn+carbon)     5 40.68  0.00 0.277     0.28
p() Ocu(BGB_Spawn+human_foot)        4 41.37  0.69 0.196     0.47
p(.) Ocu(BGB_Spawn)                  3 41.61  0.92 0.175     0.65
p(.) Ocu(human_foot)                 3 43.91  3.23 0.055     0.70
p() Ocu(river+NDVI+human_foot)       5 44.05  3.36 0.052     0.76
p() Ocu(canopy+BGB_Spawn)            4 44.32  3.64 0.045     0.80
p(.) Ocu(.)                          2 44.49  3.81 0.041     0.84
p(.) Ocu(NDVI)                       3 44.52  3.84 0.041     0.88
p(.) Ocu(river)                      3 44.63  3.94 0.039     0.92
p() Ocu(canopy)                      3 44.63  3.95 0.039     0.96
p() Ocu(carbon)                      3 45.26  4.58 0.028     0.99
p() Ocu(ALL)                         8 46.91  6.23 0.012     1.00
Code
summary(fit_unm_sp3_10)

Call:
unmarked::occu(formula = ~1 ~ scale(canopy) + scale(BGB_Spawn) + 
    scale(carbon_stock), data = umf_y_full_3, starts = c(1, 1, 
    1, 1, 1), method = "BFGS", engine = "C", control = list(maxit = 20000))

Occupancy (logit-scale):
                    Estimate    SE      z P(>|z|)
(Intercept)            -14.1  34.2 -0.412   0.680
scale(canopy)           89.8 171.0  0.525   0.599
scale(BGB_Spawn)       -50.2  96.2 -0.522   0.602
scale(carbon_stock)    -91.7 171.3 -0.535   0.593

Detection (logit-scale):
 Estimate    SE    z  P(>|z|)
       -2 0.477 -4.2 2.68e-05

AIC: 40.68389 
Number of sites: 16
optim convergence code: 0
optim iterations: 707 
Bootstrap iterations: 0 
Code
pb_f <- parboot(fit_unm_sp3_10, nsim=500, report=10) 
plot (pb_f)

Code
newdat_range<-data.frame(BGB_Spawn=seq(min(covs_raster_val$BGB_Spawn),                                       max(covs_raster_val$BGB_Spawn),length=100),
                         human_foot=seq(min(covs_raster_val$human_foot),                                       max(covs_raster_val$human_foot),length=100),
                         NDVI=seq(min(covs_raster_val$NDVI),                                                  max(covs_raster_val$NDVI),length=100),
                         river=seq(min(covs_raster_val$river),                                                  max(covs_raster_val$river),length=100),
                         carbon_stock=seq(min(covs_raster_val$carbon),                                                  max(covs_raster_val$carbon),length=100),
                         canopy=seq(min(covs_raster_val$canopy),                                                  max(covs_raster_val$canopy),length=100)
                         )


pred_psi <-predict(fit_unm_sp3_10, type="state", newdata=newdat_range, appendData=TRUE) 

###  Plot occupancy en escala original
plot(Predicted~BGB_Spawn, pred_psi,type="l",col="blue", ylim=c(0,0.95),
     xlab="canopy",
     ylab="Probabilidad de ocupación")
lines(lower~canopy, pred_psi,type="l",col=gray(0.5))
lines(upper~canopy, pred_psi,type="l",col=gray(0.5))

Code
# # fit stan
# fit_stan_sp3_0 <- stan_occu(~1~1, data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_0h <- stan_occu(~scale(hour)~1, data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_1 <- stan_occu(~scale(hour)~scale(elevation), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_2 <- stan_occu(~scale(hour)~scale(AGB_Spawn), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_3 <- stan_occu(~scale(hour)~scale(roads), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_4 <- stan_occu(~scale(hour)~scale(carbon), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_5 <- stan_occu(~scale(hour)~scale(NDVI), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_6 <- stan_occu(~scale(hour)~scale(canopy), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# 
# 
# mods3 <- fitList(fit_stan_sp3_0,
#                 fit_stan_sp3_0h,
#                 fit_stan_sp3_1,
#                 fit_stan_sp3_2,
#                 fit_stan_sp3_3,
#                 fit_stan_sp3_4,
#                 fit_stan_sp3_5,
#                 fit_stan_sp3_6)
# 
# # model selection
# round(modSel(mods3), 3) #AGB_Spawn
# 
# # plot top model
# print("Hour")
# plot_effects(fit_stan_sp3_5, "det") # Detectiom
# print("NDVI")
# plot_effects(fit_stan_sp3_5, "state") # Ocupancy

Spatial Prediction

Code
#spatial
# occuPred <- unmarked::predict(fit_unm_sp3_10,
#                     type = "state",
#                     newdata = stack(raster(covs_raster[[1]]),
#                                     raster(covs_raster[[5]]),
#                                     raster(covs_raster[[6]])), 
#                     na.rm = TRUE,
#                     inf.rm = TRUE,
#                     progress = 'text')

newdata_terra = c(covs_raster[[1]],
                 covs_raster[[5]],
                 covs_raster[[6]])

occuPred <- terra::predict(newdata_terra, fit_unm_sp3_10, type="state", cores=3, na.rm=TRUE, progress = 'text')


#### predict

# srtm_crop_s <- mask(elev, NDVI)#stack(raster(elev))#, 
# scale(roughness)) # scale altitud
# names(srtm_crop_s) <- c("elevation")#, "roughness")
# crs(srtm_crop_s) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#nd3 <- NDVI #mask(AGB_Spawn, NDVI) #data.frame(srtm_crop_s, hour=0.5)
#names(nd3) <- "NDVI"
# newd <- data.frame(srtm_crop_s, hour=0.5)
#pred_psi_s_unm <-predict(fit_unm_sp3_5, type="state", newdata=nd3)
#pred_psi_s_stan <-predict(fit_stan_sp2_1, submodel="state", newdata=nd, na.rm=FALSE)
#pred_psi_ras <- raster(srtm_crop_s) # make raster = to elev 
#pred_psi_ras[] <- pred_psi_s_stan$Predicted # drape on top


# pred_psi_r <- pred_psi_s # * sd(full_covs$elevation) + mean(full_covs$elevation)
# crs(pred_psi_ras) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
clr <- colorRampPalette(brewer.pal(9, "YlGn"))
# mapview (pred_psi_r[[1]], col.regions = clr,  legend = TRUE, alpha=0.7)
# plot(pred_psi_s[[1]], main="Occupancy")
levelplot(occuPred[[1]], par.settings = YlOrRdTheme(), margin=FALSE, main="Ocupancy")

Code
pal = mapviewPalette("mapviewSpectralColors")
mapview(raster(occuPred[[1]]), map.types = c("Esri.WorldImagery"), alpha=0.5, col.regions = pal(10)) + mapview(puntos_sf)

Nyctidromus albicollis

One species, single season

Code
umf_y_full_4<- unmarkedFrameOccu(y=f.shrink.matrix.to9( 
                                   as.data.frame(parita$occur[[4]])
                                   )) #"Aramus guarauna"
siteCovs(umf_y_full_4) <- data.frame(BGB_Spawn = covs_raster_val$BGB_Spawn,
                                   human_foot = covs_raster_val$human_foot,
                                   NDVI = covs_raster_val$NDVI,
                                   river = covs_raster_val$river,
                                   carbon_stock = covs_raster_val$carbon_stock,
                                   canopy = covs_raster_val$canopy
) # data.frame(Elev=full_covs$Elev) # Full


plot(umf_y_full_4, main="Nyctidromus albicollis")  #""Buteogallus anthracinus 

Code
# obscanop<-matrix(rep(covs_raster_val$canopy_height_jetz2,27),ncol = 27) # make elevetion per observation

# detection
obsCovs(umf_y_full_4) <- list(hour = data.frame(f.shrink.matrix.h.to9(parita$dete[[4]]), 16, 9),
                              canopy = covs_raster_val$canopy ) # hora


# fit unmarked
fit_unm_sp4_0 <- unmarked::occu(~1~1, data=umf_y_full_4) # ok!
#fit_unm_sp2_0h <- unmarked::occu(~scale(hour)~1, data=umf_y_full_2) # It work!
# fit_unm_sp2_0c <- unmarked::occu(~scale(canopy)~1, data=umf_y_full_2) # It work!
fit_unm_sp4_1 <- unmarked::occu(~1~scale(BGB_Spawn), data=umf_y_full_4,  starts=c(1,1,1), method = "BFGS",
               control = list(maxit = 20000),
               engine = "C")

fit_unm_sp4_2 <- unmarked::occu(~1~scale(human_foot), data=umf_y_full_4)
fit_unm_sp4_3 <- unmarked::occu(~1~scale(NDVI), data=umf_y_full_4) # ok!
fit_unm_sp4_4 <- unmarked::occu(~1~scale(river), data=umf_y_full_4)
fit_unm_sp4_4s <- unmarked::occu(~1~scale(I(river^2)), data=umf_y_full_4)
fit_unm_sp4_5 <- unmarked::occu(~1~scale(carbon_stock), data=umf_y_full_4)
fit_unm_sp4_6 <- unmarked::occu(~1~scale(canopy), data=umf_y_full_4)
fit_unm_sp4_7 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn), data=umf_y_full_4)
fit_unm_sp4_8 <- unmarked::occu(~1~scale(BGB_Spawn)+
                                  scale(human_foot), data=umf_y_full_4, 
                                starts=c(1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp4_9 <- unmarked::occu(~1~scale(river)+ # non carbon
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_4)
fit_unm_sp4_10 <- unmarked::occu(~1~scale(canopy)+ # all carbon
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock), data=umf_y_full_4,
                                starts=c(1,1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp4_11 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock)+
                                  scale(river)+ 
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_4)


# # fit list for detection
# fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
#               #"p(h) Ocu(.)"=fit_unm_sp2_0h, # NA problem!
#               #"p(c) Ocu(.)"=fit_unm_sp2_0c)#,
#               "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
#               "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
#               "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
#               "p(.) Ocu(river)"=fit_unm_sp2_4,
#               "p() Ocu(carbon)"=fit_unm_sp2_5,
#               "p() Ocu(canopy)"=fit_unm_sp2_6,
#               )

# model selection detection unmarked
# modSel(fms1) #problem!.. null model ranks better

# fit list for occupancy
fms4<-fitList("p(.) Ocu(.)"=fit_unm_sp4_0,
              #"p(h) Ocu(.)"=fit_unm_sp2_0h, #,
              #"p(c) Ocu(.)"=fit_unm_sp2_0c,
              "p(.) Ocu(BGB_Spawn)"=fit_unm_sp4_1,
              "p(.) Ocu(human_foot)"=fit_unm_sp4_2,
              "p(.) Ocu(NDVI)"=fit_unm_sp4_3,
              "p(.) Ocu(river)"=fit_unm_sp4_4,
              #"p(.) Ocu(river^2)"=fit_unm_sp4_4s,
              "p() Ocu(carbon)"=fit_unm_sp4_5,
              "p() Ocu(canopy)"=fit_unm_sp4_6,
              "p() Ocu(canopy+BGB_Spawn)"=fit_unm_sp4_7,
              "p() Ocu(BGB_Spawn+human_foot)"=fit_unm_sp4_8,
              "p() Ocu(river+NDVI+human_foot)"=fit_unm_sp4_9,
              "p() Ocu(canopy+BGB_Spawn+carbon)"=fit_unm_sp4_10,
              "p() Ocu(ALL)"=fit_unm_sp4_11
              )

# model selection detection unmarked
modSel(fms4)
                                 nPars   AIC delta AICwt cumltvWt
p(.) Ocu(river)                      3 86.85  0.00 0.369     0.37
p() Ocu(canopy+BGB_Spawn)            4 89.41  2.56 0.103     0.47
p(.) Ocu(.)                          2 89.67  2.82 0.090     0.56
p() Ocu(canopy)                      3 89.75  2.90 0.087     0.65
p(.) Ocu(BGB_Spawn)                  3 90.14  3.29 0.071     0.72
p() Ocu(carbon)                      3 90.41  3.56 0.062     0.78
p() Ocu(river+NDVI+human_foot)       5 90.47  3.62 0.060     0.84
p() Ocu(canopy+BGB_Spawn+carbon)     5 91.18  4.33 0.042     0.88
p(.) Ocu(human_foot)                 3 91.41  4.56 0.038     0.92
p(.) Ocu(NDVI)                       3 91.66  4.81 0.033     0.96
p() Ocu(BGB_Spawn+human_foot)        4 91.87  5.02 0.030     0.99
p() Ocu(ALL)                         8 93.34  6.49 0.014     1.00
Code
summary(fit_unm_sp4_4)

Call:
unmarked::occu(formula = ~1 ~ scale(river), data = umf_y_full_4)

Occupancy (logit-scale):
             Estimate   SE      z P(>|z|)
(Intercept)     -1.43 1.92 -0.745   0.456
scale(river)    -3.68 3.75 -0.982   0.326

Detection (logit-scale):
 Estimate    SE     z P(>|z|)
   -0.506 0.313 -1.62   0.106

AIC: 86.85087 
Number of sites: 16
optim convergence code: 0
optim iterations: 25 
Bootstrap iterations: 0 
Code
#pb_f <- parboot(fit_unm_sp4_4, nsim=500, report=10) 
#plot (pb_f)

############## function to evaluate, model
fitstats <- function(model.name, 
                     method = "nonparboot") {
  observed <- getY(model.name@data)
  expected <- fitted(model.name)
  resids <- residuals(model.name,
                      method = "nonparboot")
  sse <- sum(resids^2,
             na.rm = TRUE)
  chisq <- sum((observed - expected)^2 / expected,
               na.rm = TRUE)
  freeTuke <- sum((sqrt(observed) - sqrt(expected))^2, 
                  na.rm = TRUE)
  out <- c(SSE = sse,
           Chisq = chisq,
           freemanTukey = freeTuke)
  return(out)
}

# wait!...
pb <- parboot(fit_unm_sp4_4,
              fitstats,
              nsim = 1000,
              report = TRUE,
              method = "nonparboot")
pb

Call: 
parboot(object = fit_unm_sp4_4, statistic = fitstats, nsim = 1000, report = TRUE, method = "nonparboot")

Parametric Bootstrap Statistics:
               t0 mean(t0 - t_B) StdDev(t0 - t_B) Pr(t_B > t0)
SSE          15.3          0.941             3.26        0.420
Chisq        75.3          9.338            12.43        0.185
freemanTukey 21.0          1.732             3.75        0.363

t_B quantiles:
               0% 2.5% 25% 50% 75% 97.5% 100%
SSE           2.9  7.4  12  15  17    20   22
Chisq        20.4 32.6  61  67  73    88  115
freemanTukey  4.6 10.8  17  20  22    25   26

t0 = Original statistic computed from data
t_B = Vector of bootstrap samples
Code
par(mfrow = c(3,1)) # 3 en una
plot(pb,
     main = "All",
     xlab = c("SSE", "Chisq", "FT"))

Code
dev.off() # reset par
null device 
          1 
Code
############# histogram evaluation
pb_f <- parboot(fit_unm_sp4_4, nsim=500, report=10) 
plot (pb_f)



newdat_range<-data.frame(BGB_Spawn=seq(min(covs_raster_val$BGB_Spawn),                                       max(covs_raster_val$BGB_Spawn),length=100),
                         human_foot=seq(min(covs_raster_val$human_foot),                                       max(covs_raster_val$human_foot),length=100),
                         NDVI=seq(min(covs_raster_val$NDVI),                                                  max(covs_raster_val$NDVI),length=100),
                         river=seq(min(covs_raster_val$river),                                                  max(covs_raster_val$river),length=100),
                         carbon_stock=seq(min(covs_raster_val$carbon_stock),                                                  max(covs_raster_val$carbon),length=100),
                         canopy=seq(min(covs_raster_val$canopy),                                                  max(covs_raster_val$canopy),length=100)
                         )

pred_psi <-predict(fit_unm_sp4_4, type="state", newdata=newdat_range, appendData=TRUE) 

###  Plot occupancy en escala original
plot(Predicted~river, pred_psi,type="l",col="blue", ylim=c(0,0.95),
     xlab="river",
     ylab="Probabilidad de ocupación")
lines(lower~river, pred_psi,type="l",col=gray(0.5))
lines(upper~river, pred_psi,type="l",col=gray(0.5))

# # fit stan
# fit_stan_sp3_0 <- stan_occu(~1~1, data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_0h <- stan_occu(~scale(hour)~1, data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_1 <- stan_occu(~scale(hour)~scale(elevation), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_2 <- stan_occu(~scale(hour)~scale(AGB_Spawn), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_3 <- stan_occu(~scale(hour)~scale(roads), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_4 <- stan_occu(~scale(hour)~scale(carbon), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_5 <- stan_occu(~scale(hour)~scale(NDVI), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# fit_stan_sp3_6 <- stan_occu(~scale(hour)~scale(canopy), data=umf_y_full_3, chains=3, iter=1000, cores=3)
# 
# 
# mods3 <- fitList(fit_stan_sp3_0,
#                 fit_stan_sp3_0h,
#                 fit_stan_sp3_1,
#                 fit_stan_sp3_2,
#                 fit_stan_sp3_3,
#                 fit_stan_sp3_4,
#                 fit_stan_sp3_5,
#                 fit_stan_sp3_6)
# 
# # model selection
# round(modSel(mods3), 3) #AGB_Spawn
# 
# # plot top model
# print("Hour")
# plot_effects(fit_stan_sp3_5, "det") # Detectiom
# print("NDVI")
# plot_effects(fit_stan_sp3_5, "state") # Ocupancy

Spatial Prediction

Code
#### predict

# srtm_crop_s <- mask(elev, NDVI)#stack(raster(elev))#, 
# scale(roughness)) # scale altitud
# names(srtm_crop_s) <- c("elevation")#, "roughness")
# crs(srtm_crop_s) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# nd4 <- mask(AGB_Spawn, NDVI) #data.frame(srtm_crop_s, hour=0.5)
# names(nd4) <- "AGB_Spawn"
# newd <- data.frame(srtm_crop_s, hour=0.5)
# pred_psi_s_unm <-predict(fit_unm_sp4_4, type="state", newdata=nd4)


# occuPred <- unmarked::predict(fit_unm_sp4_4,
#                     type = "state",
#                     newdata = covs_raster[[4]], 
#                     na.rm = TRUE,
#                     inf.rm = TRUE)

newdata_terra = c(covs_raster[[4]])#,
                 #covs_raster[[5]],
                 #covs_raster[[6]])

occuPred <- terra::predict(newdata_terra, fit_unm_sp4_4, type="state", cores=3, na.rm=TRUE, progress = 'text')



#pred_psi_s_stan <-predict(fit_stan_sp2_1, submodel="state", newdata=nd, na.rm=FALSE)
#pred_psi_ras <- raster(srtm_crop_s) # make raster = to elev 
#pred_psi_ras[] <- pred_psi_s_stan$Predicted # drape on top


# pred_psi_r <- pred_psi_s # * sd(full_covs$elevation) + mean(full_covs$elevation)
# crs(pred_psi_ras) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
clr <- colorRampPalette(brewer.pal(9, "YlGn"))
# mapview (pred_psi_r[[1]], col.regions = clr,  legend = TRUE, alpha=0.7)
# plot(pred_psi_s[[1]], main="Occupancy")
levelplot(occuPred[[1]], par.settings = YlOrRdTheme(), margin=FALSE, main="Ocupancy")

Code
pal = mapviewPalette("mapviewSpectralColors")
mapview(raster(occuPred[[1]]), map.types = c("Esri.WorldImagery"), alpha=0.5, col.regions = pal(10)) + mapview(puntos_sf)

Piranga rubra

One species, single season

Code
# Make UMF object
umf_y_full_5<- unmarkedFrameOccu(y= as.data.frame(parita$occur[[5]])) #Dryocopus lineatus

siteCovs(umf_y_full_5) <- data.frame(BGB_Spawn = covs_raster_val$BGB_Spawn,
                                   human_foot = covs_raster_val$human_foot,
                                   NDVI = covs_raster_val$NDVI,
                                   river = covs_raster_val$river,
                                   carbon_stock = covs_raster_val$carbon_stock,
                                   canopy = covs_raster_val$canopy
                                   )


plot(umf_y_full_5, main="Piranga rubra")  #""Buteogallus anthracinus no converge

Code
# detection
obsCovs(umf_y_full_5) <- list(hour = matrix(parita$dete[[5]]),
                              canopy = covs_raster_val$canopy ) # hora

# Double formula: first part is for detection, second for occupancy
#  form <- ~detCov1 + detCov2 ~habCov1 + habCov2 + RSR(x, y, threshold=1)

# fit unmarked
# fit unmarked
fit_unm_sp5_0 <- unmarked::occu(~1~1, data=umf_y_full_5) # ok!
#fit_unm_sp2_0h <- unmarked::occu(~scale(hour)~1, data=umf_y_full_2) # It work!
# fit_unm_sp2_0c <- unmarked::occu(~scale(canopy)~1, data=umf_y_full_2) # It work!
fit_unm_sp5_1 <- unmarked::occu(~1~scale(BGB_Spawn), data=umf_y_full_5,  starts=c(1,1,1), method = "BFGS",
               control = list(maxit = 20000),
               engine = "C")

fit_unm_sp5_2 <- unmarked::occu(~1~scale(human_foot), data=umf_y_full_5)
fit_unm_sp5_3 <- unmarked::occu(~1~scale(NDVI), data=umf_y_full_5) # ok!
fit_unm_sp5_4 <- unmarked::occu(~1~scale(river), data=umf_y_full_5)
fit_unm_sp5_4s <- unmarked::occu(~1~scale(I(river^2)), data=umf_y_full_5)
fit_unm_sp5_5 <- unmarked::occu(~1~scale(carbon_stock), data=umf_y_full_5)
fit_unm_sp5_6 <- unmarked::occu(~1~scale(canopy), data=umf_y_full_5)
fit_unm_sp5_7 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn), data=umf_y_full_5)
fit_unm_sp5_8 <- unmarked::occu(~1~scale(BGB_Spawn)+
                                  scale(human_foot), data=umf_y_full_5, 
                                starts=c(1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp5_9 <- unmarked::occu(~1~scale(river)+ # non carbon
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_5)
fit_unm_sp5_10 <- unmarked::occu(~1~scale(canopy)+ # all carbon
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock), data=umf_y_full_5,
                                starts=c(1,1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp5_11 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock)+
                                  scale(river)+ 
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_5)


# # fit list for detection
# fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
#               #"p(h) Ocu(.)"=fit_unm_sp2_0h, # NA problem!
#               #"p(c) Ocu(.)"=fit_unm_sp2_0c)#,
#               "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
#               "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
#               "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
#               "p(.) Ocu(river)"=fit_unm_sp2_4,
#               "p() Ocu(carbon)"=fit_unm_sp2_5,
#               "p() Ocu(canopy)"=fit_unm_sp2_6,
#               )

# model selection detection unmarked
# modSel(fms1) #problem!.. null model ranks better

# fit list for occupancy
fms5<-fitList("p(.) Ocu(.)"=fit_unm_sp5_0,
              #"p(h) Ocu(.)"=fit_unm_sp2_0h, #,
              #"p(c) Ocu(.)"=fit_unm_sp2_0c,
              "p(.) Ocu(BGB_Spawn)"=fit_unm_sp5_1,
              "p(.) Ocu(human_foot)"=fit_unm_sp5_2,
              "p(.) Ocu(NDVI)"=fit_unm_sp5_3,
              "p(.) Ocu(river)"=fit_unm_sp5_4,
              #"p(.) Ocu(river^2)"=fit_unm_sp4_4s,
              "p() Ocu(carbon)"=fit_unm_sp5_5,
              "p() Ocu(canopy)"=fit_unm_sp5_6,
              "p() Ocu(canopy+BGB_Spawn)"=fit_unm_sp5_7,
              "p() Ocu(BGB_Spawn+human_foot)"=fit_unm_sp5_8,
              "p() Ocu(river+NDVI+human_foot)"=fit_unm_sp5_9,
              "p() Ocu(canopy+BGB_Spawn+carbon)"=fit_unm_sp5_10,
              "p() Ocu(ALL)"=fit_unm_sp5_11
              )

# model selection detection unmarked
modSel(fms5)
                                 nPars    AIC delta   AICwt cumltvWt
p() Ocu(canopy+BGB_Spawn)            4 230.01  0.00 0.44935     0.45
p() Ocu(canopy)                      3 231.32  1.31 0.23314     0.68
p() Ocu(canopy+BGB_Spawn+carbon)     5 231.77  1.76 0.18639     0.87
p() Ocu(ALL)                         8 232.88  2.87 0.10711     0.98
p() Ocu(carbon)                      3 236.58  6.57 0.01684     0.99
p(.) Ocu(BGB_Spawn)                  3 240.89 10.88 0.00195     0.99
p(.) Ocu(.)                          2 240.99 10.98 0.00186     1.00
p(.) Ocu(river)                      3 242.30 12.29 0.00096     1.00
p() Ocu(BGB_Spawn+human_foot)        4 242.62 12.61 0.00082     1.00
p(.) Ocu(human_foot)                 3 242.80 12.79 0.00075     1.00
p(.) Ocu(NDVI)                       3 242.99 12.98 0.00068     1.00
p() Ocu(river+NDVI+human_foot)       5 246.19 16.18 0.00014     1.00
Code
summary(fit_unm_sp5_7)

Call:
unmarked::occu(formula = ~1 ~ scale(canopy) + scale(BGB_Spawn), 
    data = umf_y_full_5)

Occupancy (logit-scale):
                 Estimate   SE      z P(>|z|)
(Intercept)          1.89 2.26  0.834   0.404
scale(canopy)       -6.14 5.57 -1.104   0.270
scale(BGB_Spawn)     2.52 2.68  0.939   0.348

Detection (logit-scale):
 Estimate    SE     z  P(>|z|)
   -0.684 0.163 -4.21 2.54e-05

AIC: 230.0089 
Number of sites: 16
optim convergence code: 0
optim iterations: 13 
Bootstrap iterations: 0 
Code
############# histogram evaluation
pb_f <- parboot(fit_unm_sp5_7, nsim=500, report=10) 
plot (pb_f)

Code
pred_psi <-predict(fit_unm_sp5_7, type="state", newdata=newdat_range, appendData=TRUE) 

###  Plot occupancy en escala original
plot(Predicted~canopy, pred_psi,type="l",col="blue", ylim=c(0,0.95),
     xlab="canopy",
     ylab="Probabilidad de ocupación")
lines(lower~canopy, pred_psi,type="l",col=gray(0.5))
lines(upper~canopy, pred_psi,type="l",col=gray(0.5))

Code
###  Plot occupancy en escala original
plot(Predicted~BGB_Spawn, pred_psi,type="l",col="blue", ylim=c(0,0.95),
     xlab="BGB_Spawn",
     ylab="Probabilidad de ocupación")
lines(lower~BGB_Spawn, pred_psi,type="l",col=gray(0.5))
lines(upper~BGB_Spawn, pred_psi,type="l",col=gray(0.5))

Code
# 
# # fit stan
# fit_stan_sp5_0 <- stan_occu(~1~1, data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_0h <- stan_occu(~scale(hour)~1, data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_0c <- stan_occu(~scale(canopy)~1, data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_1 <- stan_occu(~scale(hour)~scale(elevation), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_2 <- stan_occu(~scale(hour)~scale(AGB_Spawn), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_3 <- stan_occu(~scale(hour)~scale(roads), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_4 <- stan_occu(~scale(hour)~scale(carbon), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_5 <- stan_occu(~scale(hour)~scale(NDVI), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# fit_stan_sp5_6 <- stan_occu(~scale(hour)~scale(canopy), data=umf_y_full_5, chains=3, iter=1000, cores=3)
# 
# 
# mods5 <- fitList(fit_stan_sp5_0,
#                 fit_stan_sp5_0h,
#                 fit_stan_sp5_0c,
#                 fit_stan_sp5_1,
#                 fit_stan_sp5_2,
#                 fit_stan_sp5_3,
#                 fit_stan_sp5_4,
#                 fit_stan_sp5_5)
# 
# # model selection
# round(modSel(mods5), 3)
# 
# # plot top model
# print("Hour")
# plot_effects(fit_stan_sp5_2, "det") # Detection
# print("AGB_Spawn")
# plot_effects(fit_stan_sp5_2, "state") # Occupancy

Spatial Prediction

Code
#### predict

# srtm_crop_s <- mask(elev, NDVI)#stack(raster(elev))#, 
# scale(roughness)) # scale altitud
# names(srtm_crop_s) <- c("elevation")#, "roughness")
# crs(srtm_crop_s) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# nd5 <- mask(AGB_Spawn, NDVI) #data.frame(srtm_crop_s, hour=0.5)
# names(nd5) <- "AGB_Spawn"
# newd <- data.frame(srtm_crop_s, hour=0.5)
#pred_psi_s_unm <-predict(fit_unm_sp5_2b, type="state", newdata=nd5)
#pred_psi_s_stan <-predict(fit_stan_sp2_1, submodel="state", newdata=nd, na.rm=FALSE)
#pred_psi_ras <- raster(srtm_crop_s) # make raster = to elev 
#pred_psi_ras[] <- pred_psi_s_stan$Predicted # drape on top

newdata_terra = c(covs_raster[[1]],#,
                 covs_raster[[6]])#,
                 #covs_raster[[6]])

occuPred <- terra::predict(newdata_terra, fit_unm_sp5_7, type="state", cores=3, na.rm=TRUE, progress = 'text')



# pred_psi_r <- pred_psi_s # * sd(full_covs$elevation) + mean(full_covs$elevation)
# crs(pred_psi_ras) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
clr <- colorRampPalette(brewer.pal(9, "YlGn"))
# mapview (pred_psi_r[[1]], col.regions = clr,  legend = TRUE, alpha=0.7)
# plot(pred_psi_s[[1]], main="Occupancy")
levelplot(occuPred[[1]], par.settings = YlOrRdTheme(), margin=FALSE, main="Ocupancy")

Code
pal = mapviewPalette("mapviewSpectralColors")
mapview(raster(occuPred[[1]]), map.types = c("Esri.WorldImagery"), alpha=0.5, col.regions = pal(10)) + mapview(puntos_sf)

Aramus guarauna

One species, single season

Code
# Make UMF object
umf_y_full_1<- unmarkedFrameOccu(y= as.data.frame(parita$occur[[1]])) #Aramus guarauna

siteCovs(umf_y_full_1) <- data.frame(BGB_Spawn = covs_raster_val$BGB_Spawn,
                                   human_foot = covs_raster_val$human_foot,
                                   NDVI = covs_raster_val$NDVI,
                                   river = covs_raster_val$river,
                                   carbon_stock = covs_raster_val$carbon_stock,
                                   canopy = covs_raster_val$canopy
                                   ) # data.frame(Elev=full_covs$Elev) # Full


plot(umf_y_full_1, main="Aramus guarauna")  #""Buteogallus anthracinus no converge

Code
# detection
obsCovs(umf_y_full_1) <- list(hour = matrix(parita$dete[[1]]),
                              canopy = covs_raster_val$canopy ) # hora

# Double formula: first part is for detection, second for occupancy
#  form <- ~detCov1 + detCov2 ~habCov1 + habCov2 + RSR(x, y, threshold=1)

# fit unmarked
# fit unmarked
fit_unm_sp1_0 <- unmarked::occu(~1~1, data=umf_y_full_1) # ok!
#fit_unm_sp2_0h <- unmarked::occu(~scale(hour)~1, data=umf_y_full_2) # It work!
# fit_unm_sp2_0c <- unmarked::occu(~scale(canopy)~1, data=umf_y_full_2) # It work!
fit_unm_sp1_1 <- unmarked::occu(~1~scale(BGB_Spawn), data=umf_y_full_1,  starts=c(1,1,1), method = "BFGS",
               control = list(maxit = 20000),
               engine = "C")

fit_unm_sp1_2 <- unmarked::occu(~1~scale(human_foot), data=umf_y_full_1)
fit_unm_sp1_3 <- unmarked::occu(~1~scale(NDVI), data=umf_y_full_1) # ok!
fit_unm_sp1_4 <- unmarked::occu(~1~scale(river), data=umf_y_full_1)
fit_unm_sp1_4s <- unmarked::occu(~1~scale(I(river^2)), data=umf_y_full_1)
fit_unm_sp1_5 <- unmarked::occu(~1~scale(carbon_stock), data=umf_y_full_1)
fit_unm_sp1_6 <- unmarked::occu(~1~scale(canopy), data=umf_y_full_1)
fit_unm_sp1_7 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn), data=umf_y_full_1)
fit_unm_sp1_8 <- unmarked::occu(~1~scale(BGB_Spawn)+
                                  scale(human_foot), data=umf_y_full_1, 
                                starts=c(1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp1_9 <- unmarked::occu(~1~scale(river)+ # non carbon
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_1)
fit_unm_sp1_10 <- unmarked::occu(~1~scale(canopy)+ # all carbon
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock), data=umf_y_full_1,
                                starts=c(1,1,1,1,1), method = "BFGS",
                                control = list(maxit = 20000),
                                engine = "C")
fit_unm_sp1_11 <- unmarked::occu(~1~scale(canopy)+
                                  scale(BGB_Spawn)+ 
                                  scale(carbon_stock)+
                                  scale(river)+ 
                                  scale(NDVI)+ 
                                  scale(human_foot), data=umf_y_full_1)


# # fit list for detection
# fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp2_0,
#               #"p(h) Ocu(.)"=fit_unm_sp2_0h, # NA problem!
#               #"p(c) Ocu(.)"=fit_unm_sp2_0c)#,
#               "p(.) Ocu(BGB_Spawn)"=fit_unm_sp2_1,
#               "p(.) Ocu(human_foot)"=fit_unm_sp2_2,
#               "p(.) Ocu(NDVI)"=fit_unm_sp2_3,
#               "p(.) Ocu(river)"=fit_unm_sp2_4,
#               "p() Ocu(carbon)"=fit_unm_sp2_5,
#               "p() Ocu(canopy)"=fit_unm_sp2_6,
#               )

# model selection detection unmarked
# modSel(fms1) #problem!.. null model ranks better

# fit list for occupancy
fms1<-fitList("p(.) Ocu(.)"=fit_unm_sp1_0,
              #"p(h) Ocu(.)"=fit_unm_sp2_0h, #,
              #"p(c) Ocu(.)"=fit_unm_sp2_0c,
              "p(.) Ocu(BGB_Spawn)"=fit_unm_sp1_1,
              "p(.) Ocu(human_foot)"=fit_unm_sp1_2,
              "p(.) Ocu(NDVI)"=fit_unm_sp1_3,
              "p(.) Ocu(river)"=fit_unm_sp1_4,
              #"p(.) Ocu(river^2)"=fit_unm_sp4_4s,
              "p() Ocu(carbon)"=fit_unm_sp1_5,
              "p() Ocu(canopy)"=fit_unm_sp1_6,
              "p() Ocu(canopy+BGB_Spawn)"=fit_unm_sp1_7,
              "p() Ocu(BGB_Spawn+human_foot)"=fit_unm_sp1_8,
              "p() Ocu(river+NDVI+human_foot)"=fit_unm_sp1_9,
              "p() Ocu(canopy+BGB_Spawn+carbon)"=fit_unm_sp1_10,
              "p() Ocu(ALL)"=fit_unm_sp1_11
              )

# model selection detection unmarked
modSel(fms1)
                                 nPars   AIC delta AICwt cumltvWt
p(.) Ocu(BGB_Spawn)                  3 37.51  0.00 0.152     0.15
p(.) Ocu(.)                          2 37.70  0.19 0.138     0.29
p() Ocu(canopy+BGB_Spawn+carbon)     5 37.81  0.30 0.131     0.42
p() Ocu(BGB_Spawn+human_foot)        4 37.85  0.34 0.128     0.55
p(.) Ocu(human_foot)                 3 38.44  0.93 0.095     0.64
p(.) Ocu(river)                      3 38.50  0.99 0.092     0.74
p(.) Ocu(NDVI)                       3 39.11  1.60 0.068     0.80
p() Ocu(carbon)                      3 39.49  1.98 0.056     0.86
p() Ocu(canopy+BGB_Spawn)            4 39.52  2.01 0.055     0.92
p() Ocu(canopy)                      3 39.70  2.19 0.051     0.97
p() Ocu(river+NDVI+human_foot)       5 40.96  3.45 0.027     0.99
p() Ocu(ALL)                         8 43.96  6.45 0.006     1.00
Code
summary(fit_unm_sp1_1)

Call:
unmarked::occu(formula = ~1 ~ scale(BGB_Spawn), data = umf_y_full_1, 
    starts = c(1, 1, 1), method = "BFGS", engine = "C", control = list(maxit = 20000))

Occupancy (logit-scale):
                 Estimate    SE      z P(>|z|)
(Intercept)          21.8  74.1  0.294   0.769
scale(BGB_Spawn)    -31.3 110.4 -0.284   0.776

Detection (logit-scale):
 Estimate    SE    z  P(>|z|)
    -4.24 0.582 -7.3 2.93e-13

AIC: 37.50955 
Number of sites: 16
optim convergence code: 0
optim iterations: 562 
Bootstrap iterations: 0 
Code
############# histogram evaluation
pb_f <- parboot(fit_unm_sp1_1, nsim=500, report=10) 
plot (pb_f)

Code
pred_psi <-predict(fit_unm_sp1_1, type="state", newdata=newdat_range, appendData=TRUE) 


###  Plot occupancy en escala original
plot(Predicted~BGB_Spawn, pred_psi,type="l",col="blue", ylim=c(-0.1,1.1),
     xlab="BGB_Spawn",
     ylab="Probabilidad de ocupación")
lines(lower~BGB_Spawn, pred_psi,type="l",col=gray(0.5))
lines(upper~BGB_Spawn, pred_psi,type="l",col=gray(0.5))

Spatial Prediction

Code
#### predict

# srtm_crop_s <- mask(elev, NDVI)#stack(raster(elev))#, 
# scale(roughness)) # scale altitud
# names(srtm_crop_s) <- c("elevation")#, "roughness")
# crs(srtm_crop_s) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
# nd1 <- NDVI #data.frame(srtm_crop_s, hour=0.5)
# names(nd1) <- "NDVI"
# newd <- data.frame(srtm_crop_s, hour=0.5)
# pred_psi_s_unm <-predict(fit_unm_sp1_5, type="state", newdata=nd1)
#pred_psi_s_stan <-predict(fit_stan_sp2_1, submodel="state", newdata=nd, na.rm=FALSE)
#pred_psi_ras <- raster(srtm_crop_s) # make raster = to elev 
#pred_psi_ras[] <- pred_psi_s_stan$Predicted # drape on top



newdata_terra = c(covs_raster[[1]])#,#,
                 #covs_raster[[6]])#,
                 #covs_raster[[6]])

occuPred <- terra::predict(newdata_terra, fit_unm_sp1_1, type="state", cores=3, na.rm=TRUE, progress = 'text')



# pred_psi_r <- pred_psi_s # * sd(full_covs$elevation) + mean(full_covs$elevation)
# crs(pred_psi_ras) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
clr <- colorRampPalette(brewer.pal(9, "YlGn"))
# mapview (pred_psi_r[[1]], col.regions = clr,  legend = TRUE, alpha=0.7)
# plot(pred_psi_s[[1]], main="Occupancy")
levelplot(occuPred[[1]], par.settings = YlOrRdTheme(), margin=FALSE, main="Ocupancy")

Code
pal = mapviewPalette("mapviewSpectralColors")
mapview(raster(occuPred[[1]]), map.types = c("Esri.WorldImagery"), alpha=0.5, col.regions = pal(10)) + mapview(puntos_sf)

Información de la sesión en R.

Code
print(sessionInfo(), locale = FALSE)
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default


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

other attached packages:
 [1] rasterVis_0.51.6   lattice_0.22-5     RColorBrewer_1.1-3 raster_3.6-26     
 [5] sp_2.1-3           terra_1.7-71       stocc_1.31         ubms_1.2.6        
 [9] mapview_2.11.2     sf_1.0-15          unmarked_1.4.1     lubridate_1.9.3   
[13] forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2       
[17] tidyr_1.3.1        tibble_3.2.1       ggplot2_3.5.0      tidyverse_2.0.0   
[21] readxl_1.4.3       readr_2.1.5       

loaded via a namespace (and not attached):
  [1] DBI_1.2.2               deldir_2.0-2            pbapply_1.7-2          
  [4] leafpop_0.1.0           gridExtra_2.3           inline_0.3.19          
  [7] rlang_1.1.3             magrittr_2.0.3          matrixStats_1.2.0      
 [10] e1071_1.7-14            compiler_4.3.2          loo_2.7.0              
 [13] systemfonts_1.0.5       png_0.1-8               vctrs_0.6.5            
 [16] maps_3.4.2              crayon_1.5.2            pkgconfig_2.0.3        
 [19] fastmap_1.1.1           ellipsis_0.3.2          leafem_0.2.3           
 [22] utf8_1.2.4              rmarkdown_2.26          tzdb_0.4.0             
 [25] nloptr_2.0.3            bit_4.0.5               xfun_0.42              
 [28] satellite_1.0.5         jsonlite_1.8.8          uuid_1.2-0             
 [31] jpeg_0.1-10             parallel_4.3.2          R6_2.5.1               
 [34] stringi_1.8.3           StanHeaders_2.32.5      boot_1.3-28.1          
 [37] jquerylib_0.1.4         cellranger_1.1.0        Rcpp_1.0.12            
 [40] rstan_2.32.5            knitr_1.45              fields_15.2            
 [43] zoo_1.8-12              base64enc_0.1-3         leaflet.providers_2.0.0
 [46] splines_4.3.2           Matrix_1.6-1.1          timechange_0.3.0       
 [49] tidyselect_1.2.0        rstudioapi_0.15.0       yaml_2.3.8             
 [52] codetools_0.2-19        pkgbuild_1.4.4          withr_3.0.0            
 [55] rARPACK_0.11-0          coda_0.19-4.1           evaluate_0.23          
 [58] units_0.8-5             proxy_0.4-27            RcppParallel_5.1.7     
 [61] pillar_1.9.0            KernSmooth_2.23-22      stats4_4.3.2           
 [64] generics_0.1.3          vroom_1.6.5             truncnorm_1.0-9        
 [67] hms_1.1.3               rstantools_2.4.0        munsell_0.5.0          
 [70] scales_1.3.0            minqa_1.2.6             class_7.3-22           
 [73] glue_1.7.0              tools_4.3.2             interp_1.1-6           
 [76] hexbin_1.28.3           lme4_1.1-35.1           RSpectra_0.16-1        
 [79] dotCall64_1.1-1         grid_4.3.2              QuickJSR_1.1.3         
 [82] crosstalk_1.2.1         latticeExtra_0.6-30     colorspace_2.1-0       
 [85] nlme_3.1-163            brew_1.0-10             cli_3.6.2              
 [88] spam_2.10-0             fansi_1.0.6             viridisLite_0.4.2      
 [91] svglite_2.1.3           gtable_0.3.4            digest_0.6.34          
 [94] classInt_0.4-10         farver_2.1.1            htmlwidgets_1.6.4      
 [97] htmltools_0.5.7         lifecycle_1.0.4         leaflet_2.2.1          
[100] bit64_4.0.5             MASS_7.3-60