openCR Error in hclust...
Posted: Thu Mar 12, 2020 5:53 pm
I've recently been receiving the following error when fitting models in openCR (using the openCR.fit() function) to a set of detection histories:
Error in hclust(dist(object), ...) : must have n >= 2 objects to cluster
In addition: Warning message:
In (function (..., deparse.level = 1) :
number of columns of result is not a multiple of vector length (arg 1)
I've looked more closely at the detection history and everything appears to be in order (although it's quite possible I'm missing something). The read.capthist() and verify() functions also aren't flagging anything. Code that previously worked has also started throwing this error.
Below's the code I'm using.
Cheers,
Neil
Error in hclust(dist(object), ...) : must have n >= 2 objects to cluster
In addition: Warning message:
In (function (..., deparse.level = 1) :
number of columns of result is not a multiple of vector length (arg 1)
I've looked more closely at the detection history and everything appears to be in order (although it's quite possible I'm missing something). The read.capthist() and verify() functions also aren't flagging anything. Code that previously worked has also started throwing this error.
Below's the code I'm using.
Cheers,
Neil
- Code: Select all
###Load Data
##Load Packages
#install.packages(here::here('openCR_1.2.1.zip'), repos = NULL, type = "win.binary")
library(openCR)
library(rgdal)
library(plyr)
spat.subset.capthist <- function(capthist, spatialboundary){
traps <- cbind(TrapID=row.names(traps(capthist)),as.data.frame(traps(capthist)))
coordinates(traps) <- c("x", "y")
proj4string(traps) <- CRS(proj4string(spatialboundary))
traps.in <- raster::intersect(traps,spatialboundary)
plot(bound)
plot(traps.in, add=TRUE)
traps.in <- as.data.frame(traps.in)
return(subset(capthist, traps=row.names(traps(capthist)) %in% traps.in$TrapID, dropnullCH=TRUE))
}
bound = readOGR(dsn="./CL/SECR_ch/SRGBPU_plus_Boundary/SR_StudyAreaBoundary_ForMac.kml")
bound = spTransform( bound,CRS("+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
###################################
##### FIT MODEL TO FULL DATA SET
###################################
#list to populate
ch.list <- list()
#projects
projects <- list.files("./CL/SECR_ch/cap3")
#projects = projects[-3] # Exclude the 2007 flathead inventory
projects = projects[1:2]
for(i in 1:length(projects)){
##Read capture history files
a <- read.capthist(paste0("./CL/SECR_ch/cap3 - NE/", projects[[i]]),
paste0("./CL/SECR_ch/trap3 - NE/", projects[[i]]),
detector ="proximity",
trapcovnames="Trap_Type",
covnames="Sex",
sep = ",")
a <- spat.subset.capthist(a, bound)
ch.list[[i]] <- a
names(ch.list)[i]<- stringr::str_replace(projects[[i]],".txt","")
}
traps(ch.list) <- shareFactorLevels(traps(ch.list), columns="Trap_Type")
covariates(ch.list) = shareFactorLevels(covariates(ch.list), columns="Sex")
grizzCH <- MS.capthist(ch.list)
names(grizzCH) <- stringr::str_replace(projects, pattern=".txt", replacement="")
remove(a)
#open population mask, single mask but big enough to cover all years of sampling
GBmask.1 <- make.mask(rbind(traps(grizzCH), checkdetector=FALSE), buffer = 30000, type = 'trapbuffer', spacing = 3000)
######
##Full data model
######
fit1 = openCR.fit(capthist = grizzCH, mask = GBmask.1, type = 'JSSAsecrfCL', list(lambda0~1, sigma~1, phi ~ 1, f~1), ncores = 8)