openCR Error in hclust...

questions concerning anlysis/theory using program DENSITY and R package secr. Focus on spatially-explicit analysis.

openCR Error in hclust...

Postby nfaught » 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

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)


nfaught
 
Posts: 1
Joined: Mon Mar 09, 2020 3:55 pm

Re: openCR Error in hclust...

Postby murray.efford » Fri Mar 13, 2020 4:40 pm

Hello Neil
Your data preparation is fairly complex and I can't put my finger on what is going wrong, plus openCR is not supported to the same extent as 'secr' etc., but
1. Make sure you are using the current version of openCR (1.2.1 is way old)
2. The error does not come from openCR.fit itself. Only reduce.traps in 'secr' calls hclus. I suspect that can be called by join(), which is called by openCR.fit() only if needed.
3. Therefore I suggest you pre-process the capthist object(s) with join() and see where that leads. I see that plyr also has a function join(), so use secr::join(). I think remove.dupl.sites = FALSE in join() bypasses the call to reduce.traps, but that ultimately may not be what you want to do.
Murray
murray.efford
 
Posts: 597
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand


Return to analysis help

Who is online

Users browsing this forum: No registered users and 1 guest