From 6e1d7ea464c2b4c1f6a545a3d10c15880fa77a83 Mon Sep 17 00:00:00 2001 From: Niklas Hohmann <67792281+NiklasHohmann@users.noreply.github.com> Date: Mon, 9 Sep 2024 07:59:34 +0200 Subject: [PATCH 1/2] fix typos in documentation --- R/tp_helpers.R | 2 +- R/tp_to_adm.R | 8 ++++---- man/tp_time_det.Rd | 2 +- man/tp_to_adm.Rd | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/R/tp_helpers.R b/R/tp_helpers.R index 383b374..15ad554 100644 --- a/R/tp_helpers.R +++ b/R/tp_helpers.R @@ -91,7 +91,7 @@ tp_time_det = function(times){ #' #' @seealso [tp_height_det()] for deterministic tie points in height, [tp_time_norm()] for tie points following a normal distribution #' - #' @returns a function for usage with _strat_cont_to_multiadm_ and_sedrate_to_mulitadm_ as t_tp input + #' @returns a function for usage with _strat_cont_to_multiadm_ and _sedrate_to_mulitadm_ as t_tp input #' if (is.unsorted(times, strictly = TRUE)){ stop("Need strictly increasing times") diff --git a/R/tp_to_adm.R b/R/tp_to_adm.R index b9b022e..3732dc6 100644 --- a/R/tp_to_adm.R +++ b/R/tp_to_adm.R @@ -11,8 +11,8 @@ tp_to_adm = function(t, h, T_unit = NULL, L_unit = NULL){ #' #' @param t Vector, tie points in time #' @param h Vector, tie points in height - #' @param T_unit time unit - #' @param L_unit length unit + #' @param T_unit character, time unit + #' @param L_unit character, length unit #' #' @details #' by default, intervals with no sediment accumulation are marked as destructive. @@ -26,11 +26,11 @@ tp_to_adm = function(t, h, T_unit = NULL, L_unit = NULL){ #' #' #' @examples - #' \dontrun{ + #' #' my_adm = tp_to_adm(t = 1:4, h = c(1,2,2,3), T_unit = "kyr", L_unit = "m") #' plot(my_adm) #' # see vignette("admtools") for other examples - #' } + #' #' #' diff --git a/man/tp_time_det.Rd b/man/tp_time_det.Rd index fe015a4..d2de7b6 100644 --- a/man/tp_time_det.Rd +++ b/man/tp_time_det.Rd @@ -10,7 +10,7 @@ tp_time_det(times) \item{times}{numeric vector, times of the tie points} } \value{ -a function for usage with \emph{strat_cont_to_multiadm} and_sedrate_to_mulitadm_ as t_tp input +a function for usage with \emph{strat_cont_to_multiadm} and \emph{sedrate_to_mulitadm} as t_tp input } \description{ defines deterministc tie points in time. diff --git a/man/tp_to_adm.Rd b/man/tp_to_adm.Rd index 1b07f66..83cd7b9 100644 --- a/man/tp_to_adm.Rd +++ b/man/tp_to_adm.Rd @@ -11,9 +11,9 @@ tp_to_adm(t, h, T_unit = NULL, L_unit = NULL) \item{h}{Vector, tie points in height} -\item{T_unit}{time unit} +\item{T_unit}{character, time unit} -\item{L_unit}{length unit} +\item{L_unit}{character, length unit} } \value{ object of class \code{adm} @@ -27,11 +27,11 @@ by default, intervals with no sediment accumulation are marked as destructive. \code{is_adm} } \examples{ -\dontrun{ + my_adm = tp_to_adm(t = 1:4, h = c(1,2,2,3), T_unit = "kyr", L_unit = "m") plot(my_adm) # see vignette("admtools") for other examples -} + } From be7a59c2b1526e8034aed05cacc8959179775ce5 Mon Sep 17 00:00:00 2001 From: Niklas Hohmann <67792281+NiklasHohmann@users.noreply.github.com> Date: Wed, 11 Sep 2024 09:24:39 +0200 Subject: [PATCH 2/2] add anchoring of ADMs --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 6 +++- R/anchor.R | 63 ++++++++++++++++++++++++++++++++++++ R/merge_adm_to_multiadm.R | 8 ++--- man/anchor.Rd | 30 +++++++++++++++++ tests/testthat/test_anchor.R | 13 ++++++++ vignettes/admtools_doc.Rmd | 4 ++- 8 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 R/anchor.R create mode 100644 man/anchor.Rd create mode 100644 tests/testthat/test_anchor.R diff --git a/DESCRIPTION b/DESCRIPTION index 59c5625..5ff130d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: admtools Title: Estimate and Manipulate Age-Depth Models -Version: 0.3.1 +Version: 0.3.1.9000 Authors@R: person("Niklas", "Hohmann", , "N.H.Hohmann@uu.nl", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1559-1838")) diff --git a/NAMESPACE b/NAMESPACE index 452c079..191a55b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ S3method(time_to_strat,phylo) export(L_axis_lab) export(T_axis_lab) export(add_adm_to_multiadm) +export(anchor) export(condensation) export(condensation_fun) export(flux_const) diff --git a/NEWS.md b/NEWS.md index 8dd06f3..feb900e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,12 @@ +# admtools (development version) + +* added anchoring of age-depth models via `anchor` + # admtools 0.3.1 * more options for sedimentation rate generator `sed_rate_from_matrix` -* abstracted exctraction of tie points +* abstracted extraction of tie points * added utility functions for S3 class `sac` diff --git a/R/anchor.R b/R/anchor.R new file mode 100644 index 0000000..1d998d8 --- /dev/null +++ b/R/anchor.R @@ -0,0 +1,63 @@ +anchor = function(x, index = "last", t_anchor = NULL, n = 1000L){ + #' @export + #' + #' @title anchor age-depth model + #' + #' @param x age-depth model + #' @param index "last" or "first", specifying at which tie point the age-depth model will be anchored + #' @param t_anchor time at which the adm is anchored. must be a function that takes no arguments and returns the timing of the tie point. see example or vignettes for details + #' @param n integer, number of samples drawn from the tie point + #' + #' @description + #' anchors a deterministic age-depth model (adm object) at a tie point that is associated with uncertainty. + #' + #' + #' + #' @returns a collection of age-depth models (a multiadm object) + #' + #' @examples + #' t_anchor = function() rnorm(1) # normally distributed uncertainty + #' x = tp_to_adm(t = c(1,2), h = c(2,3)) # simple age-depth model + #' m = anchor(x, index = "last", t_anchor = t_anchor, n = 100) # anchor age-depth model + #' plot(m) + #' + if (! index %in% c("last", "first")){ + stop("can only anchor at first or last tie point") + } + + if (! (is.function(t_anchor))){ + stop("expect functions as inputs for t_anchor") + } + adm_list = list() + if (index == "last"){ + t_ref = max_time(x) + } + if (index == "first"){ + t_ref = min_time(x) + } + + for (i in seq_len(n)){ + t_offset = t_anchor() + t = get_T_tp(x) - t_ref + t_offset + h = get_L_tp(x) + L_unit = get_L_unit(x) + T_unit = get_T_unit(x) + adm_list[[i]] = tp_to_adm(t, h, T_unit, L_unit) + } + + multiadm = list("t" = list(), + "h" = list(), + "destr" = list(), + "no_of_entries" = length(adm_list), + "T_unit" = get_T_unit(x), + "L_unit" = get_L_unit(x)) + + for (i in seq_along(adm_list)){ + adm = adm_list[[i]] + multiadm[["t"]][[i]] = adm$t + multiadm[["h"]][[i]] = adm$h + multiadm[["destr"]][[i]] = adm$destr + } + class(multiadm) = "multiadm" + return(multiadm) +} \ No newline at end of file diff --git a/R/merge_adm_to_multiadm.R b/R/merge_adm_to_multiadm.R index 035b2bd..e09bf51 100644 --- a/R/merge_adm_to_multiadm.R +++ b/R/merge_adm_to_multiadm.R @@ -20,11 +20,11 @@ merge_adm_to_multiadm = function(...){ "L_unit" = NULL) T_units = unlist(sapply(adm_list, get_T_unit)) L_units = unlist(sapply(adm_list, get_L_unit)) - - if (any(rep(T_units[1], length(adm_list)) != T_units)){ - stop("Inconsistent time units, can not merge adms") + + if (any(rep(T_units[1], length(adm_list)) != T_units)){ + stop("Inconsistent time units, can not merge adms") } - if (any(rep(L_units[1], length(adm_list)) != L_units)){ + if (any(rep(L_units[1], length(adm_list)) != L_units)){ stop("Inconsistent length units, can not merge adms") } diff --git a/man/anchor.Rd b/man/anchor.Rd new file mode 100644 index 0000000..3e5e89a --- /dev/null +++ b/man/anchor.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/anchor.R +\name{anchor} +\alias{anchor} +\title{anchor age-depth model} +\usage{ +anchor(x, index = "last", t_anchor = NULL, n = 1000L) +} +\arguments{ +\item{x}{age-depth model} + +\item{index}{"last" or "first", specifying at which tie point the age-depth model will be anchored} + +\item{t_anchor}{time at which the adm is anchored. must be a function that takes no arguments and returns the timing of the tie point. see example or vignettes for details} + +\item{n}{integer, number of samples drawn from the tie point} +} +\value{ +a collection of age-depth models (a multiadm object) +} +\description{ +anchors a deterministic age-depth model (adm object) at a tie point that is associated with uncertainty. +} +\examples{ +t_anchor = function() rnorm(1) # normally distributed uncertainty +x = tp_to_adm(t = c(1,2), h = c(2,3)) # simple age-depth model +m = anchor(x, index = "last", t_anchor = t_anchor, n = 100) # anchor age-depth model +plot(m) + +} diff --git a/tests/testthat/test_anchor.R b/tests/testthat/test_anchor.R new file mode 100644 index 0000000..3a92d19 --- /dev/null +++ b/tests/testthat/test_anchor.R @@ -0,0 +1,13 @@ +test_that("incorrect input for index paramter is caught", { + t_anchor = function() rnorm(n = 1) + x = tp_to_adm(c(0,1), c(0,1), "Myr", "m") + expect_no_condition(anchor(x, index = "last", t_anchor, n = 10)) + expect_no_condition(anchor(x, index = "first", t_anchor, n = 10)) + expect_error(anchor(x, index = 2, t_anchor, n = 10)) +}) + +test_that("incorrect input for t_anchor parameter is caught", { + x = tp_to_adm(c(0,1), c(0,1), "Myr", "m") + t_anchor = 2 + expect_error(anchor(x, "last", t_anchor, n = 10)) +}) diff --git a/vignettes/admtools_doc.Rmd b/vignettes/admtools_doc.Rmd index 1d5f7d0..bab5ba9 100644 --- a/vignettes/admtools_doc.Rmd +++ b/vignettes/admtools_doc.Rmd @@ -101,6 +101,7 @@ The following functions transform `adm` objects into other S3 classes: - `merge_adm_to_multiadm` into `multiamd` - `add_adm_to_multiadm` to add `adm` to `multiadm` +- `anchor` to transform anchor `adm` at a tie point with uncertainty, resulting in a `multiadm` ### S3 class `sac` @@ -177,7 +178,8 @@ The S3 class `multiadm` represents **multi**ple **a**ge **d**epth **m**odels.. S The following functions construct `multiadm` objects: -- `merge_adm_to_multiadm` to contruct `multiadm` from `adm` objects +- `anchor` to construct `multiadm` from uncertain tie points and `adm` objects. +- `merge_adm_to_multiadm` to construct `multiadm` from `adm` objects - `sedrate_to_multiadm` construct `multiadm` from info on sedimentation rates, see `vignette("adm_from_sedrate")` - `strat_cont_to_multiadm` construct `multiadm` from tracer information, see `vignette("adm_from_trace_cont")`