aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <ken@kenkellner.com>2022-04-12 11:25:34 -0400
committerKen Kellner <ken@kenkellner.com>2022-04-12 11:25:34 -0400
commit3a84ea4000014c7240f00c3d1cc1b46605a441aa (patch)
treeb2221722cf814d844390f39309db00214b3321f2
parent41180ec32e011a952b4d255031aba1e85dfe8a23 (diff)
parent9213a62c177ce9556a7a55fb87e900557e434e5f (diff)
Merge branch 'master' into kfold
-rw-r--r--DESCRIPTION2
-rw-r--r--NAMESPACE1
-rw-r--r--R/priors.R10
-rw-r--r--inst/stan/include/functions_priors.stan2
-rw-r--r--man/priors.Rd3
-rw-r--r--tests/testthat/test_priors.R12
6 files changed, 29 insertions, 1 deletions
diff --git a/DESCRIPTION b/DESCRIPTION
index 6720287..545d68d 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,5 +1,5 @@
Package: ubms
-Version: 1.1.9003
+Version: 1.1.9004
Date: 2022-04-11
Title: Bayesian Models for Data from Unmarked Animals using 'Stan'
Authors@R: person("Ken", "Kellner", email="contact@kenkellner.com",
diff --git a/NAMESPACE b/NAMESPACE
index 66e95af..2032258 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -5,6 +5,7 @@ export(cauchy)
export(extract_log_lik)
export(gamma)
export(gof)
+export(laplace)
export(logistic)
export(normal)
export(plot_spatial)
diff --git a/R/priors.R b/R/priors.R
index 81e5560..9313d0d 100644
--- a/R/priors.R
+++ b/R/priors.R
@@ -88,6 +88,16 @@ gamma <- function(shape=1, rate=1){
list(dist=5, par1=shape, par2=rate, par3=0, autoscale=FALSE)
}
+#' @rdname priors
+#' @export
+laplace <- function(location=0, scale=2.5, autoscale=TRUE){
+ stopifnot(all(scale > 0))
+ if((length(location) > 1) & (length(scale) > 1)){
+ stopifnot(length(location) == length(scale))
+ }
+ list(dist=6, par1=location, par2=scale, par3=0, autoscale=autoscale)
+}
+
null_prior <- function(){
list(dist=0, par1=0, par2=0, par3=0, autoscale=FALSE)
}
diff --git a/inst/stan/include/functions_priors.stan b/inst/stan/include/functions_priors.stan
index ae573da..c0ef956 100644
--- a/inst/stan/include/functions_priors.stan
+++ b/inst/stan/include/functions_priors.stan
@@ -11,6 +11,8 @@ real lp_single_prior(vector x, int dist, row_vector pars1,
out += logistic_lpdf(x | pars1, pars2);
} else if(dist == 5){
out += gamma_lpdf(x | pars1, pars2);
+ } else if(dist == 6){
+ out += double_exponential_lpdf(x | pars1, pars2);
}
return out;
}
diff --git a/man/priors.Rd b/man/priors.Rd
index c5ec014..74cc4a0 100644
--- a/man/priors.Rd
+++ b/man/priors.Rd
@@ -8,6 +8,7 @@
\alias{logistic}
\alias{cauchy}
\alias{gamma}
+\alias{laplace}
\title{Prior distributions}
\usage{
normal(location = 0, scale = 2.5, autoscale = TRUE)
@@ -21,6 +22,8 @@ logistic(location = 0, scale = 1)
cauchy(location = 0, scale = 2.5, autoscale = TRUE)
gamma(shape = 1, rate = 1)
+
+laplace(location = 0, scale = 2.5, autoscale = TRUE)
}
\arguments{
\item{location}{The mean of the distribution. If setting the priors for
diff --git a/tests/testthat/test_priors.R b/tests/testthat/test_priors.R
index be7f622..2453140 100644
--- a/tests/testthat/test_priors.R
+++ b/tests/testthat/test_priors.R
@@ -67,6 +67,18 @@ test_that("gamma prior can be specified",{
expect_error(gamma(1,-1))
})
+test_that("laplace prior can be specified",{
+ lp <- laplace()
+ expect_equal(lp, list(dist=6, par1=0, par2=2.5, par3=0, autoscale=TRUE))
+ lp <- laplace(2, 0.5, autoscale=FALSE)
+ expect_equal(lp, list(dist=6, par1=2, par2=0.5, par3=0, autoscale=FALSE))
+ lp <- laplace(c(0.5,1), c(0.1,0.2))
+ expect_equal(lp, list(dist=6, par1=c(0.5,1), par2=c(0.1,0.2), par3=0, autoscale=TRUE))
+ lp <- laplace(c(0.5,1), 0.1) # no error
+ expect_error(laplace(c(0.5,1), c(0.1,0.2,0.3)))
+ expect_error(laplace(0.5, -0.2))
+})
+
test_that("null prior can be specified",{
expect_equal(null_prior(), list(dist=0, par1=0, par2=0, par3=0, autoscale=FALSE))
})