read.capthist help -- covariates

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

read.capthist help -- covariates

Postby KPhillips » Fri Sep 27, 2019 1:19 pm

Hello, I need some help with text file inputs into read.capthist when my detector data has covariates. My detectors have both a covariate that varies across occasion (baited vs. non-baited), and a covariate that is constant across occasions (detector "type"). First year of my study had 5 occasions and varying effort across occasions.

I poured over both the data input and the varying effort vignettes, and was not sure whether the covariates should be a separate column from occasion effort, or in the same column as effort and separated by "/". Thus, I created two separate text files and tried read.capthist with both formats.

The first 5 lines of my two detector files look like this:

FILE "det_testA_txt3"

#Detector X Y Effort Covariates
1281-3COR15 401073 5264994 0 0 13 11 82 /B B B B NB/C
1281-4COR15 402931 5265632 10 12 0 0 58 /B B B B NB/C
1334-1COR15 401508 5264050 10 12 0 0 58 /B B B B NB/C
1334-3COR15 400382 5261316 0 0 13 11 41 /B B B B NB/C
1335-1COR15 406150 5262906 10 12 0 0 90 /B B B B NB/C

FILE "det_testA_txt4"

#Detector X Y Covariates
1281-3COR15 401073 5264994 0 0 13 11 82/B B B B NB/C
1281-4COR15 402931 5265632 10 12 0 0 58/B B B B NB/C
1334-1COR15 401508 5264050 10 12 0 0 58/B B B B NB/C
1334-3COR15 400382 5261316 0 0 13 11 41/B B B B NB/C
1335-1COR15 406150 5262906 10 12 0 0 90/B B B B NB/C

When I try to generate my capture history file, I keep receiving an error stating that the number of covariates doesn't match the number of columns:

> CH.a<- read.capthist("cap_testA_txt.txt", "det_testA_txt3.txt", detector = "proximity", fmt = c("trapID","XY"),
+ noccasions = 5, covnames="Sex", trapcovnames = c("Bait","Type"), binary.usage = FALSE)
Error in read.traps(file = "det_testA_txt3.txt", detector = "proximity", :
number of covariate names does not match number of columns 5
>
> CH.b<- read.capthist("cap_testA_txt.txt", "det_testA_txt4.txt", detector = "proximity", fmt = c("trapID","XY"),
+ noccasions = 5, covnames="Sex", trapcovnames = c("Bait","Type"), binary.usage = FALSE)
Error in read.traps(file = "det_testA_txt4.txt", detector = "proximity", :
number of covariate names does not match number of columns 5
>

I was able to proceed if I used a detector text file without the covariates, and successfully fit a variety of basic models (including sex-specific model) to familiarize myself with the software. why

> CH2<- read.capthist("cap_testA_txt.txt", "det_testA_txt3_NOCOV.txt", detector = "proximity", fmt = c("trapID","XY"),
+ noccasions = 5, covnames="Sex", binary.usage = FALSE)
No errors found :-)
>
> summary(CH2)
Object class capthist
Detector type proximity
Detector number 83
Average spacing 2138.345 m
x-range 400382 430407 m
y-range 5237742 5265632 m

Usage range by occasion
1 2 3 4 5
min 0 0 0 0 0
max 14 14 16 17 123

Counts by occasion
1 2 3 4 5 Total
n 24 14 23 9 68 138
u 24 12 23 8 52 119
f 100 19 0 0 0 119
M(t+1) 24 36 59 67 119 119
losses 0 0 0 0 0 0
detections 24 14 23 9 71 141
detectors visited 19 14 15 7 45 100
detectors used 41 41 41 40 82 245
>


Could you please advise on why read.capthist won't accept my detector covariates? Thank you.
KPhillips
 
Posts: 2
Joined: Thu Sep 26, 2019 3:14 pm

Re: read.capthist help -- covariates

Postby murray.efford » Sun Sep 29, 2019 5:23 am

I think you have 6 columns of detector covariates, and each needs a name. Also, use only one '/' per line.
Five detector covariates are later defined as a single time-varying covariate. I tried this with a modified 'trap.txt' file below and it worked. It seems a newdata dataframe must be constructed manually to include the time-varying covariate.

Code: Select all
ch <- read.capthist('capt.txt', 'trapmod.txt', trapcovnames =
    c('d1','d2','d3','d4','d5','cd'), fmt = 'XY')
timevaryingcov(traps(ch)) <- list(baited = 1:5)
fit <- secr.fit(ch, model = g0~baited)
predict(fit, newdata = data.frame(baited = factor(c('B','NB'))))


trapmod.txt has lines like this
Code: Select all
1     365     365 1 1 1 1 1 / B B B B NB C
2     365     395 1 1 1 1 1 / B B B B NB C
3     365     425 1 1 1 1 1 / NB B B B NB C
4     365     455 1 1 1 1 1 / B NB B B NB C
5     365     485 1 1 1 1 1 / B B NB B NB C
6     365     515 1 1 1 1 1 / B B B B NB C
murray.efford
 
Posts: 582
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: read.capthist help -- covariates

Postby KPhillips » Tue Oct 01, 2019 1:51 pm

Thank you! It works now, and I successfully fit a model with g0 ~ baited.

However, I am having a different issue with fitting a model with g0 ~ both an individual covariate (sex, male vs. female) and a trap-level covariate (trap "type", does not vary over time). I had no issue fitting models with sigma ~ sex, g0 ~ sex, etc.:

Code: Select all
fit.bait <- secr.fit(CH16.testD, mask=mask16.testD, trace = FALSE, model = g0 ~ baited)

fit.type <- secr.fit(CH16.testD, mask=mask16.testD, trace = FALSE, hcov= "Sex", model = list(sigma ~ h2, g0 ~ "Type"))

fit2 <- secr.fit(CH16.testD, mask=mask16.testD, trace = FALSE, hcov= "Sex", model = list(sigma ~ h2, g0 ~ h2))


But when i try to fit a model with g0~ sex and type, i receive the error Error in terms.formula(formula) : invalid model formula in ExtractVars

Code: Select all
> fit.type <- secr.fit(CH16.testD, mask=mask16.testD, trace = FALSE, hcov= "Sex", model = list(sigma ~ h2, g0 ~ h2 + "Type"))
Error in terms.formula(formula) : invalid model formula in ExtractVar


I do not receive the same error if i attempt to fit model = list (sigma ~ h2, g0 ~ h2 + baited), although i haven't let this run all the way through yet (will be slow).

Do I need to do something different with the trap "type" covariate? I should have mentioned that i am analyzing black bear hair-snag data (baited barbed wire hair-snag corrals, as well as barbed wires strands strung across game trails for passive hair collection. This latter method was added in year 2 in an effort to increase recapture rates. With bears, i strongly expect detection probability to differ for males and females, given their disparate home range sizes and daily movement rates).

To provide context for the trap "type" covariate, this is what my capthist file looks like (there were 10 sampling occasions in year 2. My first post referred to year 1 data, with only 5 sampling occasions):

Code: Select all
> CH16.testD<- read.capthist("cap_testA_2016_txt1.txt", "det_testA_2016_BaitByOcc.txt", detector = "proximity", fmt = c("trapID","XY"),
+                            noccasions = 10, covnames="Sex",
+                            trapcovname=c("Type","Bait1","Bait2","Bait3","Bait4","Bait5","Bait6","Bait7","Bait8","Bait9","Bait10"), binary.usage = FALSE)
No errors found :-)
> head(covariates(CH16.testD))
         Sex
A629005B   F
A629030B   F
A629060B   F
A629079B   M
A629105B   M
A629175B   F
> head(covariates(traps(CH16.testD)))
            Type Bait1 Bait2 Bait3 Bait4 Bait5 Bait6 Bait7 Bait8 Bait9 Bait10
1334-2COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
1334-4COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
1335-2COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
1335-4COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
1336-2COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
1336-4COR16    C    NB     B     B     B     B    NB    NB    NB    NB     NB
> head(usage(traps(CH16.testD)))
             1  2  3  4  5 6 7 8 9 10
1334-2COR16 19 14 13  0  0 0 0 0 0  0
1334-4COR16 46  0  0 14 15 0 0 0 0  0
1335-2COR16 20 13 14  0  0 0 0 0 0  0
1335-4COR16 47  0  0 14 15 0 0 0 0  0
1336-2COR16 50 14 13  0  0 0 0 0 0  0
1336-4COR16 75  0  0 14 11 0 0 0 0  0


Thanks!
KPhillips
 
Posts: 2
Joined: Thu Sep 26, 2019 3:14 pm

Re: read.capthist help -- covariates

Postby murray.efford » Tue Oct 01, 2019 2:46 pm

Variable names in formulae should not be in quotes, and take care with capitalisation
murray.efford
 
Posts: 582
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