Trouble with covariates for detection parameter sigma

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

Trouble with covariates for detection parameter sigma

Postby gpugesek » Fri Nov 06, 2015 2:06 pm

Hello Murray!

I posted a thread to this forum earlier this month (thank you for all your help, by the way). To reiterate, I am working with a lab group analyzing mark recapture data from a population of male butterflies. We are currently trying to run a model with habitat type as a covariate for density and the detection parameter sigma. We have successfully created a capthist object and a habitat mask which includes a covariate for habitat type (hab_type). We have also run models in which habitat type has been identified as a covariate for density.
Code: Select all
secr.fit2012.1 = secr.fit(capthist2012_2, model = D ~ hab_type, mask = habitatmask_new, buffer = 140, CL = T, method = "Nelder-Mead")

However, when we try to run the following code, in which a covariate is specified for sigma, an error message pops up.
Code: Select all
secr.fit2012.2 = secr.fit(capthist2012_2, model = list (D ~ hab_type, sigma ~ hab_type), mask = habitatmask_new, buffer = 140, CL = T, method = "Nelder-Mead")
Error in secr.design.MS(capthist, model, timecov, sessioncov, groups,  :
  covariate(s) hab_type not found

My impression from the manual is that it is possible to include user-defined predictors from trap covariates.
I am unsure of which steps to take in order to run a model which includes habitat type as a covariate for density and the detection parameter sigma.

Thank you for your time and your help.
Genevieve
gpugesek
 
Posts: 9
Joined: Thu Oct 29, 2015 10:32 am

Re: Trouble with covariates for detection parameter sigma

Postby murray.efford » Fri Nov 06, 2015 2:32 pm

I think the problem is that mask covariates can only be applied to density*, trap covariates may only be applied to detection parameters (g0, sigma). This was a software design decision way back, and mostly we've been able to live with it. There is some overlap with a covariate like habitat class: it makes sense for both. You need to create a trap covariate with the habitat for each trap. 'secr' is slow when trap covariates have many levels.
Murray

* an exception is possible with user-defined distance functions, as we used in a recent Ecography paper, but you don't want to go there.
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Trouble with covariates for detection parameter sigma

Postby gpugesek » Fri Nov 06, 2015 2:55 pm

Thanks so much for your help, as well as the speedy reply! As we are using a single polygon for our traps object, it would not be possible to create separate covariates for each individual trap. Are there any other ways to approach this issue in Secr?
Thanks so much,
Genevieve
gpugesek
 
Posts: 9
Joined: Thu Oct 29, 2015 10:32 am

Re: Trouble with covariates for detection parameter sigma

Postby murray.efford » Fri Nov 06, 2015 3:58 pm

I didn't think about polygon detectors - each polygon is a single 'detector' so trap covariates are at the level of the polygon i.e. not enough resolution for you (unless you break the polygon up into several, one per habitat, which is probably quite feasible). Or you could investigate the code for our Ecography paper and the noneuclidean pdf: that requires some lateral thinking because it fixes sigma=1 and then varies the effective distance (relative to sigma) as a function of habitat. You would have to be fairly serious to bother with that.
Murray
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Trouble with covariates for detection parameter sigma

Postby gpugesek » Mon Nov 09, 2015 10:51 am

I'm not sure that breaking up the polygon would be the best approach for us, as the shape of the habitat patches are too complex (and are concave is some places). I began to explore the code for the Ecography paper and the noneuclidean pdf (there are some really great applications for this package). However, according to the noneuclidean pdf, user-specified distances cannot be used with polygon or transect detectors. Is there a way to get around this?? Any suggestions would be appreciated greatly!

Thanks for your time!
Genevieve
gpugesek
 
Posts: 9
Joined: Thu Oct 29, 2015 10:32 am

Re: Trouble with covariates for detection parameter sigma

Postby murray.efford » Mon Nov 09, 2015 3:52 pm

Hi Genevieve
You are quite right - splitting the polygons is not going to work if the habitats are complexly intermingled (though we could argue about what resolution is appropriate). I had forgotten about the polygon limitation on user-defined distances - sorry for sending you off in that direction. The limitation arises because the polygon code uses an integration algorithm that needs to evaluate at arbitrary points of its own choosing, rather than pre-determined points, so a pre-computed distance matrix won't work.

I am sceptical that habitat-specific sigma means very much if the habitats are intermingled. The only remaining solution I can think of right off is to rasterize your polygon detectors. I'm thinking of including the following code in the next version of 'secr' - you could use it to convert yours data from polygon to e.g. proximity detector type, and procede down one of the previously suggested paths. You will quickly find that making spacing too small yields capthist objects with far too many detector pixels for comfort. Attaching covariates to the traps component should be easy with addCovariates.
Murray

Code: Select all
discretize <- function (CH, spacing = 5, outputdetector = c('proximity','count','multi'), tol = 0.001) {
    ## convert capthist data from polygon detectors to point detector
    outputdetector <- match.arg(outputdetector)
    if (ms(CH)) {
        CHlist <- lapply(CH, discretize, spacing, outputdetector, tol)
    }
    else {
        trps <- traps(CH)
        if (!(detector(trps) %in% c('polygon', 'polygonX')))
            stop ("discretize is for polygon data")
        nd <- length(levels(polyID(trps)))
        onepoly <- function (CH) {
            trapsCH <- traps(CH)
            poly <- inflate(traps(CH), 1+tol)  ## inflate is fn in utility.r
            trps <- make.mask(trapsCH, buffer = 0, spacing = spacing, poly = poly)
            trpnum <- nearesttrap(xy(CH), trps)
            temptraps <- read.traps(data = trps, detector = outputdetector, spacing = spacing)
            rownames(temptraps) <- 1:nrow(temptraps)
            if (!is.null(usage(trapsCH)))
                usage(temptraps) <- matrix (usage(trapsCH), byrow = TRUE,
                                            nrow = nrow(trps), ncol = ncol(CH))
            if (!is.null(covariates(trapsCH))) {
                covdf <- as.data.frame(covariates(trapsCH)[rep(1,nrow(temptraps)),])
                rownames(covdf) <- rownames(temptraps)
                covariates(temptraps) <- covdf
            }
           
            df <- data.frame(
                trap = factor(trpnum, levels =1:nrow(trps)),
                occ = factor(occasion(CH), levels = 1:ncol(CH)),
                ID = factor(animalID(CH, names = F)),
                alive = alive(CH))
           
            if (outputdetector %in% c('multi')) {
                alivesign <- df$alive*2 - 1
                tempnew <- matrix(0, nrow = nrow(CH), ncol = ncol(CH))
                tempnew[cbind(df$ID, df$occ)] <- trpnum * alivesign
                if (detector(trapsCH) %in% 'polygon')
                    warning("polygon data converted to multi-catch; ",
                            "information may be lost", call. = FALSE)
            }
            else {
                tempnew <- table(df$ID, df$occ, df$trap)
                alivesign <- tapply(df$alive, list(df$ID,df$occ,df$trap),all)
                alivesign[is.na(alivesign)] <- TRUE
                alivesign <- alivesign * 2 - 1
                if (! (outputdetector %in% c('count'))
                    && (length(tempnew)>0)) {
                    ## convert 'proximity' to binary
                    tempnew[tempnew>0] <- 1
                    warning("count data converted to binary; ",
                            "information may be lost", call. = FALSE)
                }
                tempnew <- tempnew * alivesign
            }
            rownames(tempnew) <- rownames(CH)
            class(tempnew) <- 'capthist'
            session(tempnew) <- session(CH)
            attr(tempnew, 'n.mash') <- attr(CH, 'n.mash')
            attr(tempnew, 'centres') <- attr(CH, 'centres')
            covariates(tempnew) <- covariates(CH)
            traps(tempnew) <- temptraps
            tempnew
        }
        polyCH <- split(CH, levels(polyID(trps)), bytrap = TRUE)
        tmp <- lapply (polyCH, onepoly)
        rbind.capthist(tmp)
    }
}
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Trouble with covariates for detection parameter sigma

Postby murray.efford » Mon Nov 09, 2015 4:36 pm

You would also need this function called by discretize
Code: Select all
## inflate a convex outline along all radii by linear factor 'rmult'
## 2013-06-15
inflate <- function (xy, rmult = 1) {
    xy <- as.matrix(xy)
    centre <- apply(xy, 2, mean)
    xy <- sweep(xy, MARGIN = 2, STATS = centre, FUN = '-')
    r <- apply(xy, 1, function(z) sqrt(sum(z^2)))
    theta <- atan2 (xy[,2], xy[,1])
    r <- r * rmult
    xy <- cbind(r * cos(theta), r * sin(theta))
    sweep(xy, MARGIN = 2, STATS = centre, FUN = '+')
}
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Trouble with covariates for detection parameter sigma

Postby gpugesek » Fri Nov 20, 2015 12:39 pm

Hello!

Thank you so much for all of your help! I am currently using the functions you provided me with to rasterize my polygon. I have tried to run the following code:
Code: Select all
raster.2012 = discretize(capthist2012_2, outputdetector = c("proximity"))

However, I receive this error message:
Code: Select all
Error in split.capthist(CH, levels(polyID(trps)), bytrap = TRUE) :
  length of f should match number of traps


My number of traps should be 1, as I am only using 1 polygon. I used traps1=traps(capthist2012_2) to extract my traps object from the capture history object, and used levels(polyID(traps1)) to determine the length of f, which was 1. I'm not sure why I am getting this error message. Any direction you could give me would be greatly appreciated.
Thank you for your help,
Genevieve
gpugesek
 
Posts: 9
Joined: Thu Oct 29, 2015 10:32 am

Re: Trouble with covariates for detection parameter sigma

Postby murray.efford » Fri Nov 20, 2015 2:49 pm

Indeed, if there is only one polygon you have only one detector. I cannot see what might be going wrong. I included (and documented) discretize() in the provisional copy of 2.10.0 on the Density website; the example there (see ?discretize) has just one polygon and works fine. I suggest you download the provisional 2.10.0 and install the package from a local zip file (assuming you are using Windows). If you still get the error message I'd be interested to see your dataset.
Murray
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 18 guests