Factor levels for multisession trap covariates

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

Factor levels for multisession trap covariates

Postby JohnNettles » Fri Apr 14, 2023 1:42 pm

Hello,

I'm trying to model bobcat densities across a number of sessions and trap covariates. Read.capthist gives me no issues but when I try to run secr.fit, I get the following error:

Error in findvars.MS(trapcov, vars, c(4, 1)) :
covariate factor levels differ between sessions


Here is my code for read.capthist.
Code: Select all
nocc <- c(rep(3,42),2,3,3,3,3,3,3)
CH <- read.capthist(captfile="STATE_Events_Scat.txt",trapfile=c("ABB500.txt","ANF500.txt","ASB500.txt","BCF500.txt","BOD500.txt","CACK500.txt","CDCO500.txt",
                                 "CGO500.txt","CHO500.txt","CHSP500.txt","CKW500.txt","CMP500.txt","CPER500.txt","DG500.txt",
                                 "DHSV500.txt","FIAB500.txt","FRES500.txt","HC500.txt","HJWA500.txt","HVWA500.txt","JF500.txt",
                                 "JLD500.txt","JOTR500.txt","LDSF500.txt","MAM500.txt","MCC500.txt","MOJ500.txt","OG500.txt",
                                 "PAN500.txt","PR500.txt","PW500.txt","RED500.txt","SA500.txt","SBNF500.txt","SC500.txt",
                                 "SEQ500.txt","SHRS500.txt","SKE500.txt","SMNF500.txt","SPUR500.txt","SRWA500.txt","TEHA500.txt",
                                 "TL500.txt","TNC500.txt","TUN500.txt","TUO500.txt","UBBW500.txt","WIS500.txt", "YBWA500.txt"),
                    detector='count', fmt='XY',noccasions = nocc, noncapt="NONE",tol=300,snapXY=T,
                    trapcovnames=c('Site','Region','Season','Connectivity','Transect_Type','Interval'))

session(CH) <- c("ABB","ANF","ASB","BCF","BOD","CACK","CDCO",
                    "CGO","CHO","CHSP","CKW","CMP","CPER","DG",
                    "DHSV","FIAB","FRES","HC","HJWA","HVWA","JF",
                    "JLD","JOTR","LDSF","MAM","MCC","MOJ","OG",
                    "PAN","PR","PW","RED","SA","SBNF","SC",
                    "SEQ","SHRS","SKE","SMNF","SPUR","SRWA","TEHA",
                    "TL","TNC","TUN","TUO","UBBW","WIS","YBWA")


Annoyingly, 4 of the 5 trap covariates are factors. I first tried this code for each of the factor covariates but ran into the same error.
Code: Select all
for (i in 1:length(traps(CH)))
  levels(covariates(traps(CH[[i]]))$Transect_Type) <- c("ATV","ATV-Unmaintained","Game","Hike","Multiuse","Other","Road","Road-Unmaintained","Wash")
sapply(traps(CH),function(x) levels(covariates(x)$Transect_Type))

for (i in 1:length(traps(CH)))
  levels(covariates(traps(CH[[i]]))$Site) <- c("ABB","ANF","ASB","BCF","BOD","CACK","CDCO",
                                               "CGO","CHO","CHSP","CKW","CMP","CPER","DG",
                                               "DHSV","FIAB","FRES","HC","HJWA","HVWA","JF",
                                               "JLD","JOTR","LDSF","MAM","MCC","MOJ","OG",
                                               "PAN","PR","PW","RED","SA","SBNF","SC",
                                               "SEQ","SHRS","SKE","SMNF","SPUR","SRWA","TEHA",
                                               "TL","TNC","TUN","TUO","UBBW","WIS","YBWA")
sapply(traps(CH),function(x) levels(covariates(x)$Site))

for (i in 1:length(traps(CH)))
  levels(covariates(traps(CH[[i]]))$Season) <- c("Spring","Summer","Fall","Winter")
sapply(traps(CH),function(x) levels(covariates(x)$Season))

for (i in 1:length(traps(CH)))
  levels(covariates(traps(CH[[i]]))$Connectivity) <- c("Connections.with.Implementation.Flexibility","Conservation.Planning.Linkages","Large.Natural.Habitat.Areas","Limited.Connectivity.Opportunities","Irreplaceable.and.Essential.Corridors")
sapply(traps(CH),function(x) levels(covariates(x)$Connectivity))


I also tried using shareFactorLevels ( traps(CH) <- shareFactorLevels(traps(CH)) ) but it seems to convert my capture history (CH) into a traps object. When I run secr.fit it provides an error saying a 'capthist' object is required.

Code: Select all
> traps(CH) <- shareFactorLevels(traps(CH))
> class(CH)
[1] "traps" "list"
> resultsdHEX <- secr.fit(
+   CH,
+   detectfn = 'HEX',
+   mask = msk,
+   binomN = 0,
+   trace = TRUE,
+   model = list(D~Elevation+HumanPopulation+NLCD+session, sigma~Connectivity+Season, lambda0~Region+Interval+Transect_Type+Season),
+   start=list(D = 0.001, lambda0=0.07, sigma=700),
+   verify=F
+ )
Error in secr.fit(CH, detectfn = "HEX", mask = msk, binomN = 0, trace = TRUE,  :
  requires 'capthist' object


What do you recommend? Would it be best to convert these factors to numeric? How would that impact the results?

Thanks in advance!
-John
JohnNettles
 
Posts: 8
Joined: Tue Dec 07, 2021 3:21 pm

Re: Factor levels for multisession trap covariates

Postby murray.efford » Tue Apr 18, 2023 11:21 pm

Hi John
Frustrating, eh? I am fairly sure this is just a problem with the quirky replacement rules for multi-session objects (as I originally programmed them).

Does this example help? The primary use of shareFactorLevels is for the [individual-level] covariates of a multisession capthist object. Secondarily it may be applied to the covariates of a list of trap or mask objects (as indicated in the help). I'll think about automating this for trap covariates of a multi-session object.

Murray

Code: Select all
library(secr)
tmp <- ovenCHp # copy a 5-session capthist object as a basis
# generate some messy trap covariates
for (i in 1:3) covariates(traps(tmp[[i]])) <- data.frame(a = factor(trunc(0:43/10)))
for (i in 4:5) covariates(traps(tmp[[i]])) <- data.frame(a = factor(trunc(0:43/20)))
# show inconsistent levels of trap covariate 'a'
sapply(traps(tmp), function(x) levels(covariates(x)$a))

# coerce to shared levels
tr <- shareFactorLevels(traps(tmp))
# replace inside the capthist object
for (i in 1:5) covariates(traps(tmp[[i]])) <- covariates(tr)[[i]]
# show consistent levels
sapply(traps(tmp), function(x) levels(covariates(x)$a))

murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Factor levels for multisession trap covariates

Postby JohnNettles » Tue May 02, 2023 9:04 pm

Hi Murray,

Thanks for your help with this. I tried implementing the example you sent but am still running into the same problem. I have several categorical mask covariates as well that I ran through the same code. I've included them both below. Hopefully its just a mistake on my end but I can't seem to figure it out.

Code: Select all
#Mask covariates
mc <- shareFactorLevels(msk)
for (i in 1:51) covariates(msk[[i]]) <- covariates(mc)[[i]]
sapply(msk, function(x) levels(covariates(x)$Elevation))
sapply(msk, function(x) levels(covariates(x)$HumanPopulation))
sapply(msk, function(x) levels(covariates(x)$Ecoregion))
sapply(msk, function(x) levels(covariates(x)$Ownership))
sapply(msk, function(x) levels(covariates(x)$NLCD))
sapply(msk, function(x) levels(covariates(x)$Connectivity))

#Trap covariates
tr <- shareFactorLevels(traps(CH))
for (i in 1:51) covariates(traps(CH[[i]])) <- covariates(tr)[[i]]
sapply(traps(CH), function(x) levels(covariates(x)$Site))
sapply(traps(CH), function(x) levels(covariates(x)$Season))
sapply(traps(CH), function(x) levels(covariates(x)$Connectivity))
sapply(traps(CH), function(x) levels(covariates(x)$Transect_Type))


Thanks again,
John
JohnNettles
 
Posts: 8
Joined: Tue Dec 07, 2021 3:21 pm

Re: Factor levels for multisession trap covariates

Postby JohnNettles » Fri May 12, 2023 12:06 pm

Okay, ignore my last reply, I think I solved all the issues. The code you provided did in fact work, it just uncovered some other issues. I removed the sex covariate I was using as some sessions had 0 detections. I also used the 'copy nearest' function described in the 'Habitat Masks' document to fill in any missing values across the masks. Applied that function to each session and it all seems to be working now.

Code: Select all
copynearest <- function (mask, covariate) {
  NAcov <- is.na(covariates(mask)[,covariate])
  OK <- subset(mask, !NAcov)
  i <- nearesttrap(mask, OK)
  covariates(mask)[,covariate][NAcov] <- covariates(OK)[i[NAcov],covariate]
  mask
}

for (i in 1:51) msk[[i]] <- copynearest((msk)[[i]], "Elevation")


Thanks again,
John
JohnNettles
 
Posts: 8
Joined: Tue Dec 07, 2021 3:21 pm

Re: Factor levels for multisession trap covariates

Postby murray.efford » Sun May 14, 2023 6:16 pm

Congrats on struggling through this...
murray.efford
 
Posts: 686
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