Skip to content
David Lawrence Miller edited this page Sep 12, 2016 · 5 revisions

(in)Frequently Asked Questions

Limiting observations from a fitted detection function

Question: "Is it possible in dsm in R to use the global detection function limited to the observations for a specific strata? I found a post/response on the distance list that has a solution in Distance to use the "table for values" option to select the detection function for each sighting. Is it possible to do the same in R?"

So, in general, how does one fit a detection function to all observations but then fit a DSM to a subset (say, in a strata)?

Answer: This is relatively straight-forward. The easiest way of dealing with this is to take the fitted ddf object (using Distance or mrds) and remove the observations that you don't want. dsm only cares about the $fitted element of the detection function so the following code should work if your detection function was fitted using mrds

ddf.obj$fitted <- ddf.obj$fitted[index]

or using the following if the detection function was fitted with Distance:

ddf.obj$ddf$fitted <- ddf.obj$ddf$fitted[index]

where index is a vector of the elements you want to keep.

Using dsm with arbitrary detection probabilities

Question: Is it possible to use dsm with any probabilities of detection from any package not just using Distance or mrds?

Answer: To use results from other software one simply has to "fake" the object that dsm expects to be passed to it.

Note: there are no guarantees with this approach, if this doesn't work then please e-mail me and I'll see if I can get somewhere. In general one shouldn't expect that any of the methods other than model fitting and prediction should work within dsm, certainly variance estimation will not work. It is not currently my intention to officially support any other detection function fitting packages but if you author such a package and are interested in compatibility, get in touch.

Code to fake a ddf object:

fake.ddf <- list()
fake.ddf$meta.data <- list()

# set the right and left truncation (left can be 0 for no left truncation)
fake.ddf$meta.data$width <- 100 
fake.ddf$meta.data$left <- 5

# if the per-observation probabilities are stored in a vector
# called probs... 
fake.ddf$fitted <- probs
# and the object IDs are stored in a vector called object.ids
names(fake.ddf$fitted) <- object.ids

Note that the names of the fitted object are matched against the observation table (see ?dsm-data), so cannot simply be a set of arbitrary numbers.

So, as a concrete example, for the Mexico pantropical spotted dolphin data which is shipped with dsm (mexdolphins):

library(Distance)
library(dsm)
data(mexdolphins)

# fit the model in the dsm way
hr.model <- ds(mexdolphins$distdata, max(mexdolphins$distdata$distance),
                 key = "hr", adjustment = NULL)
mod1<-dsm(n~s(x,y), hr.model, mexdolphins$segdata, mexdolphins$obsdata)


# if we didn't use ds()...
probs <- hr.model$ddf$fitted
object.ids <- names(hr.model$ddf$fitted)


fake.ddf <- list()
fake.ddf$meta.data <- list()
fake.ddf$meta.data$width <- max(mexdolphins$distdata$distance)
fake.ddf$meta.data$left <- 0
fake.ddf$fitted <- probs
names(fake.ddf$fitted) <- object.ids

# this works!
mod1<-dsm(n~s(x,y), fake.ddf, mexdolphins$segdata, mexdolphins$obsdata)