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: 3
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: 712
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: openCR Error in hclust...

Postby murray.efford » Tue Apr 07, 2020 3:15 am

I have found a bug in join() that may have been the cause of this problem. I invite you to send me offline the datasets mentioned in the code so that I can confirm that.
Murray
murray.efford
 
Posts: 712
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: openCR Error in hclust...

Postby nfaught » Fri Apr 17, 2020 4:36 pm

Hi Murray,

Thanks for making that fix, I'm no longer getting the error from my previous post!

As a follow-up though, I recently encountered the following error when trying to fit two models in parallel using par.openCR.fit(), with per-capita recruitment parameterized as: f~t.

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels

The error stops being thrown when I remove the built-in session covariate.
nfaught
 
Posts: 3
Joined: Mon Mar 09, 2020 3:55 pm

Re: openCR Error in hclust...

Postby murray.efford » Fri Apr 17, 2020 5:49 pm

Thanks for confirming the fix worked. Regarding the new issue: Is this specific to par.openCR.fit? Does it happen when you use openCR.fit with f~t? Here 't' is a synonym for 'session', so there should be multiple levels of t as long as there are at least 2 sessions.
Murray
murray.efford
 
Posts: 712
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: openCR Error in hclust...

Postby nfaught » Mon Apr 20, 2020 4:49 pm

Hi Murray,

Ignore that last part. Silly mistake on my end, had a brain fart and forgot that two primary occasions only corresponds to 1 value of f...

Everything works fine once I have 3 or more years of data.

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

Re: openCR Error in hclust...

Postby murray.efford » Tue Apr 21, 2020 3:06 am

Thanks for clarifying that. Hope all goes smoothly now.
Murray
murray.efford
 
Posts: 712
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 4 guests