Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #13

Merged
merged 2 commits into from
Aug 5, 2024
Merged

Dev #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ Imports:
admtools (>= 0.3.0)
Suggests:
knitr,
rmarkdown
rmarkdown,
testthat (>= 3.0.0)
VignetteBuilder: knitr
Depends:
R (>= 4.2)
LazyData: true
URL: https://mindthegap-erc.github.io/StratPal/ , https://github.com/MindTheGap-ERC/StratPal
BugReports: https://github.com/MindTheGap-ERC/StratPal/issues
Config/testthat/edition: 3
7 changes: 7 additions & 0 deletions R/snd_niche.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ snd_niche = function(opt, tol, prob_modifier = 1, cutoff_val = NULL){
#' The collection probability follows the shape of a bell curve across a gradient, where `opt` determines the peak (mean) of the bell curve, and `tol` the standard deviation. "snd" stands for "scaled normal distribution", as the collection probability has the shape of the probability density of the normal distribution.
#' @references * Holland, Steven M. and Patzkowsky, Mark E. 1999. "Models for simulating the fossil record." Geology. https://doi.org/10.1130/0091-7613(1999)027%3C0491:MFSTFR%3E2.3.CO;2

if (tol <= 0){
stop("Need positive \"tol\" parameter.")
}
if (prob_modifier <= 0){
stop("Need positive \"prob_modifier\" parameter.")
}

fa = function(x){
f_inv = stats::dnorm(opt, opt, tol)
y = pmin(prob_modifier/f_inv * stats::dnorm(x, opt, tol), rep(1, length(x)))
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(StratPal)

test_check("StratPal")
12 changes: 12 additions & 0 deletions tests/testthat/test_bounded_niche.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("disordered boundaries throw error", {
expect_error(bounded_niche(1,0))
})

test_that("correct values are returned",{
x1 = 0
x2 = 1
f = bounded_niche(x1, x2)
expect_equal(f(x1 - 1), 0)
expect_equal(f(x2 + 1), 0)
expect_equal(f(mean(c(x1, x2))), 1)
})
6 changes: 6 additions & 0 deletions tests/testthat/test_ornstein_uhlenbeck.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("initial value is correct", {
y0 = 1
t = seq(0, 1, by = 0.05)
r = ornstein_uhlenbeck(t, y0 = y0)
expect_equal(r$y[1],y0)
})
12 changes: 12 additions & 0 deletions tests/testthat/test_p3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("throws error for negative rates", {
expect_error(p3(0, 0, 1))
})

test_that("throws error for flipped boundaries", {
expect_error(p3(1, 1, 0))
})

test_that("contitioning on number of samples works",{
n = 10
expect_equal(length(p3(1, 0, 1, n = n)), n )
})
12 changes: 12 additions & 0 deletions tests/testthat/test_p3_var_rate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("throws error for flipped boundary",{
expect_error(p3_var_rate(x = function(x) 1, from = 1, to = 0))
})

test_that("throws warning for too low values of f_max",{
expect_warning(p3_var_rate(x = function(x) 1000, from = 0,to = 1, n = 1000))
})

test_that("retruns correct number of events under conditioning",{
n = 10
expect_equal(length(p3_var_rate(function(x) 1, from = 0, to = 1, n = n)), n)
})
17 changes: 17 additions & 0 deletions tests/testthat/test_random_walk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("deterministic case is correct",{
t = seq(0, 1, by = 0.05)
r = random_walk(t, sigma = 0)
expect_equal(r$y, rep(0, length(t)))
mu = 2
r = random_walk(t, sigma = 0, mu = mu)
expect_equal(r$y, mu * t)
})

test_that("initial value is correct",{
y0 = 2
t = seq(0, 1, by = 0.05)
r = random_walk(t, y0 = y0)
expect_equal(r$y[1], y0)
r = random_walk(t - 2, y0 = y0)
expect_equal(r$y[1], y0)
})
12 changes: 12 additions & 0 deletions tests/testthat/test_rej_samp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("boundary flips throw error",{
expect_error(rej_samp(function (x) 1, 1,0))
})

test_that("retruns correct no of samples",{
n = 10
expect_equal(length(rej_samp(function(x) 1, 0, 1)), 1)
})

test_that("throws warning if density is too high",{
expect_warning(rej_samp(function(x) 10^6, 0, 1, n = 1000))
})
27 changes: 27 additions & 0 deletions tests/testthat/test_snd_niche.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("values at highest points are correct",{
opt = 0
prob_mod = 1
f = snd_niche(opt,tol = 1, prob_modifier = prob_mod)
expect_equal(f(opt), prob_mod)
opt = 0
prob_mod = 0.5
f = snd_niche(opt,tol = 1, prob_modifier = prob_mod)
expect_equal(f(opt), prob_mod)
opt = 0
prob_mod = 2
f = snd_niche(opt,tol = 1, prob_modifier = prob_mod)
expect_equal(f(opt), 1)
})

test_that("cut off works", {
opt = 1
tol = 1
cut_off = 0
f = snd_niche(opt, tol, cutoff_val = cut_off )
expect_equal(f(cut_off), 0)
})

test_that("errors for negative tolerances and prob modifiers are thrown", {
expect_error(snd_niche(1,0))
expect_error(snd_niche(1,1,0))
})
10 changes: 10 additions & 0 deletions tests/testthat/test_thin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("thinning reduces number of events",{
x = p3(1, 0, 1, n = 100)
th = thin(x, function(x) 0.5)
expect_true(length(th) < length(x))
})

test_that("thinning with 1 keeps data identical",{
x = p3(1, 0, 1, n = 100)
expect_equal(thin(x, function(x) 1), x)
})