Skip to content

Instantly share code, notes, and snippets.

@dill
Created October 5, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dill/db6d049a504f52f4b414d7769beffbff to your computer and use it in GitHub Desktop.
Save dill/db6d049a504f52f4b414d7769beffbff to your computer and use it in GitHub Desktop.
point transect distance sampling in Nimble (half-normal with 2 detection function covariates)
# need to define half-normal point transect detection function pdf
dhnpt <<- nimbleFunction(
run = function(x = double(0), b0 = double(0), b1 = double(0),
covar = double(0), width = double(0),
log = integer(0, default = 0)) {
returnType(double(0))
# calculate scale parameter
sigma <- exp(b0 + b1*covar)
# analytic expression for integral of 2*r*g(r)/width^2 when
# g(r) is half-normal (Intro distance book eqn 3.45)
nu <- sigma^2 * (1 - exp(-(width^2)/(2*sigma^2)))
# evaluate the detection function at distance/chaparral combination
g <- exp(-(x^2)/(2*sigma^2))
# pdf
L <- (x * g)/nu
if(log) return(log(L))
else return(L)
}
)
rhnpt <<- nimbleFunction(
run = function(n = integer(0), b0 = double(0), b1 = double(0),
covar = double(0), width = double(0),
log = integer(0, default = 0)) {
returnType(double())
return(0)
}
)
nimble::registerDistributions(list(
dhnpt = list(BUGSdist="dhnpt(b0, b1, covar, width)",
pqAvail = FALSE,
range = c(0, 1000))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment