summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Ranke <johannes.ranke@jrwb.de>2026-09-10 12:34:50 +0200
committerJohannes Ranke <johannes.ranke@jrwb.de>2026-09-10 12:34:50 +0200
commita034219e2edbbf82b3cc586c611a41b625ca8a82 (patch)
tree77489b24b0c5f12b8b6eb58d1c2ccee40f3af3af
parent49285cb526a891013334ddb7b8123f3e5cd8b1a7 (diff)
Better support metabolites in `sawtooth`
Also, add more example code to make it easier to use `one_box`, `sawtooth` and `pfm_degradation`. However, for generating sawtooth curves with specified output times, overlaying the output of several calls to `mkinpredict` is probably still easier.
-rw-r--r--DESCRIPTION3
-rw-r--r--NAMESPACE1
-rw-r--r--NEWS.md2
-rw-r--r--R/pfm_degradation.R35
-rw-r--r--R/twa.R33
-rw-r--r--log/check.log28
-rw-r--r--man/one_box.Rd23
-rw-r--r--man/pfm_degradation.Rd27
8 files changed, 129 insertions, 23 deletions
diff --git a/DESCRIPTION b/DESCRIPTION
index 696eb3e..b0eeac3 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -2,7 +2,7 @@ Package: pfm
Type: Package
Title: Utilities for Pesticide Fate Modelling
Version: 0.6.5
-Date: 2026-06-22
+Date: 2026-09-10
Authors@R: c(
person("Johannes Ranke", email = "johannes.ranke@agroscope.admin.ch",
role = c("aut", "cre"),
@@ -25,5 +25,6 @@ LazyLoad: true
LazyData: true
Encoding: UTF-8
URL: https://pkgdown.jrwb.de/pfm, https://github.com/jranke/pfm, http://jranke.github.io/pfm/
+Additional_repositories: https://agroscope-ch.r-universe.dev, https://jranke.r-universe.dev
Roxygen: list(markdown = TRUE, r6 = TRUE)
Config/roxygen2/version: 8.0.0
diff --git a/NAMESPACE b/NAMESPACE
index 499e471..3507706 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -6,6 +6,7 @@ S3method(max_twa,mkinfit)
S3method(max_twa,one_box)
S3method(one_box,character)
S3method(one_box,mkinfit)
+S3method(one_box,mkinmod)
S3method(one_box,numeric)
S3method(plot,TOXSWA_cwa)
S3method(plot,one_box)
diff --git a/NEWS.md b/NEWS.md
index 516ac58..91319ec 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,7 @@
## version 0.6.5
+- Add a `one_box` method for `mkinmod` objects to facilitate generating `sawtooth` curves also for metabolites
+- R/pfm_degradation.R: Fix using an mkinmod model and add an SFO-SFO example
- R/PEC_sw_drift.R: Support specifying units for distances and widths using the units package.
- R/TOXSWA_cwa.R: Adapt to readr (>= 2.2.0) requiring to wrap literal input as used for reading .out files with I().
- R/PEC_sw_drainage_UK.R: Create a function `drainage_date_UK` that does not only respect the beginning of the drainage period on 1 October, but also the end of the drainage period on 30 April, and use it for determining the degradation time. Applications early in the year before 1 May will now correctly be calculated without degradation time.
diff --git a/R/pfm_degradation.R b/R/pfm_degradation.R
index 832a797..63a6e19 100644
--- a/R/pfm_degradation.R
+++ b/R/pfm_degradation.R
@@ -9,21 +9,48 @@
#' @param years For how many years should the degradation be predicted?
#' @param step_days What step size in days should the output have?
#' @param times The output times
+#' @return A data frame containing the output times and the concentrations
+#' assuming initial concentrations of 1 for the parent and zero for
+#' metabolites, if any.
#' @export
#' @author Johannes Ranke
#' @examples
-#' head(pfm_degradation("SFO", DT50 = 10))
+#' # Simple example of an SFO decline curve
+#' sfo_out <- pfm_degradation("SFO", DT50 = 10)
+#' head(sfo_out)
+#'
+#' # Fictive example with a metabolite where we first generate an SFO-SFO model
+#' sfo_sfo <- mkinmod(
+#' parent = mkinsub("SFO", to = "metabolite"),
+#' metabolite = mkinsub("SFO"))
+#'
+#' sfo_sfo_out <- pfm_degradation(sfo_sfo,
+#' parms = c(k_parent = 0.1, f_parent_to_metabolite = 0.5, k_metabolite = 0.02))
+#'
+#' plot(
+#' sfo_sfo_out[, "time"],
+#' sfo_sfo_out[, "parent"], type = "l",
+#' xlab = "Time", ylab = "Relative concentration",
+#' xlim = c(0, 100))
+#' lines(
+#' sfo_sfo_out[, "time"],
+#' sfo_sfo_out[, "metabolite"], lty = 2)
+#'
pfm_degradation <- function(model = "SFO",
DT50 = 1000, parms = c(k_parent = log(2)/DT50),
years = 1, step_days = 1,
times = seq(0, years * 365, by = step_days))
{
- if (model %in% c("SFO", "FOMC", "DFOP", "HS", "IORE")) {
- model <- mkinmod(parent = list(type = model))
+ if (!inherits(model, "mkinmod")) {
+ if (model[1] %in% c("SFO", "FOMC", "DFOP", "HS", "IORE")) {
+ model <- mkinmod(parent = list(type = model))
+ } else {
+ stop("Please specify the model with a suitable name or an mkinmod object")
+ }
}
initial_state = c(1, rep(0, length(model$diffs) - 1))
names(initial_state) <- names(model$diffs)
- time_course <- mkinpredict(model, odeparms = parms,
+ time_course <- mkinpredict(model, odeparms = parms,
odeini = initial_state,
outtimes = times,
solution_type = ifelse(length(model$spec) == 1, "analytical", "deSolve"))
diff --git a/R/twa.R b/R/twa.R
index 0506334..8ca490d 100644
--- a/R/twa.R
+++ b/R/twa.R
@@ -82,8 +82,41 @@ one_box.character <- function(x, ini = 1, parms, ...,
}
#' @rdname one_box
+#' @param odeparms Will be passed to [mkinpredict]
+#' @param solution_type Will be passed to [mkinpredict]
#' @importFrom mkin mkinpredict
#' @export
+#' @examples
+#' library(mkin)
+#' SFO_SFO <- mkinmod(
+#' parent = mkinsub("SFO", to = "m1"),
+#' m1 = mkinsub("SFO"))
+#' c_0 = c(parent = 100, m1 = 0)
+#' deg_parms = c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01)
+#' sfo_sfo_box <- one_box(SFO_SFO, odeparms = deg_parms, ini = c_0)
+#' head(sfo_sfo_box)
+one_box.mkinmod <- function(x,
+ ini = c(1, rep(0, length(x$diffs) - 1)),
+ odeparms,
+ solution_type = "deSolve",
+ ...,
+ t_end = 100, res = 0.01)
+{
+
+ # Set names for initial amounts to be compatible with the model
+ if (is.null(names(ini))) names(ini) <- names(x$diffs)
+
+ t_out = seq(0, t_end, by = res)
+
+ tmp <- mkinpredict(x, odeparms = odeparms, odeini = ini,
+ outtimes = t_out, solution_type = solution_type)[, -1, drop = FALSE]
+ result <- ts(tmp, 0, t_end, frequency = 1/res)
+ class(result) <- c("one_box", "ts")
+ return(result)
+}
+
+#' @rdname one_box
+#' @export
one_box.mkinfit <- function(x, ini = "model", ..., t_end = 100, res = 0.01) {
fit <- x
if (ini[1] == "model") {
diff --git a/log/check.log b/log/check.log
index 3c0c083..0897357 100644
--- a/log/check.log
+++ b/log/check.log
@@ -1,12 +1,12 @@
-* using log directory ‘/home/f80868656/projects/pfm/pfm.Rcheck’
-* using R version 4.6.0 (2026-04-24)
+* using log directory ‘/home/jranke/git/pfm/pfm.Rcheck’
+* using R version 4.6.1 (2026-06-24)
* using platform: x86_64-pc-linux-gnu
* R was compiled by
- gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
- GNU Fortran (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
-* running under: Ubuntu 24.04.4 LTS
+ x86_64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0
+ GNU Fortran (Debian 14.2.0-19) 14.2.0
+* running under: Debian GNU/Linux 13 (trixie)
* using session charset: UTF-8
-* current time: 2026-06-22 16:48:14 UTC
+* current time: 2026-09-10 10:31:00 UTC
* using options ‘--no-tests --as-cran’
* checking for file ‘pfm/DESCRIPTION’ ... OK
* checking extension type ... Package
@@ -23,9 +23,9 @@
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘pfm’ can be installed ... OK
* checking installed package size ... INFO
- installed size is 10.4Mb
+ installed size is 10.3Mb
sub-directories of 1Mb or more:
- testdata 10.0Mb
+ testdata 9.9Mb
* checking package directory ... OK
* checking for future file timestamps ... OK
* checking DESCRIPTION meta-information ... OK
@@ -60,20 +60,14 @@
* checking data for non-ASCII characters ... OK
* checking LazyData ... OK
* checking data for ASCII and uncompressed saves ... OK
-* checking examples ... [11s/10s] OK
+* checking examples ... [10s/10s] OK
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ... SKIPPED
* checking PDF version of manual ... OK
-* checking HTML version of manual ... NOTE
-Skipping checking HTML validation: no command 'tidy' found.
-Please obtain a recent version of HTML Tidy by downloading a binary
-release or compiling the source code from <https://www.html-tidy.org/>.
+* checking HTML version of manual ... OK
* checking for non-standard things in the check directory ... OK
* checking for detritus in the temp directory ... OK
* DONE
-Status: 1 NOTE
-See
- ‘/home/f80868656/projects/pfm/pfm.Rcheck/00check.log’
-for details.
+Status: OK
diff --git a/man/one_box.Rd b/man/one_box.Rd
index 16f4859..cf8a91a 100644
--- a/man/one_box.Rd
+++ b/man/one_box.Rd
@@ -4,6 +4,7 @@
\alias{one_box}
\alias{one_box.numeric}
\alias{one_box.character}
+\alias{one_box.mkinmod}
\alias{one_box.mkinfit}
\title{Create a time series of decline data}
\usage{
@@ -13,6 +14,16 @@ one_box(x, ini, ..., t_end = 100, res = 0.01)
\method{one_box}{character}(x, ini = 1, parms, ..., t_end = 100, res = 0.01)
+\method{one_box}{mkinmod}(
+ x,
+ ini = c(1, rep(0, length(x$diffs) - 1)),
+ odeparms,
+ solution_type = "deSolve",
+ ...,
+ t_end = 100,
+ res = 0.01
+)
+
\method{one_box}{mkinfit}(x, ini = "model", ..., t_end = 100, res = 0.01)
}
\arguments{
@@ -35,6 +46,10 @@ all observed variables.}
\item{res}{Resolution of the time series}
\item{parms}{A named numeric vector containing the model parameters}
+
+\item{odeparms}{Will be passed to \link[mkin:mkinpredict]{mkinpredict}}
+
+\item{solution_type}{Will be passed to \link[mkin:mkinpredict]{mkinpredict}}
}
\value{
An object of class \code{one_box}, inheriting from \link{ts}.
@@ -58,4 +73,12 @@ m_2 <- mkinmod(parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"))
fit_2 <- mkinfit(m_2, FOCUS_2006_D, quiet = TRUE)
pred_2 <- one_box(fit_2, ini = "model")
plot(pred_2)
+library(mkin)
+SFO_SFO <- mkinmod(
+ parent = mkinsub("SFO", to = "m1"),
+ m1 = mkinsub("SFO"))
+c_0 = c(parent = 100, m1 = 0)
+deg_parms = c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01)
+sfo_sfo_box <- one_box(SFO_SFO, odeparms = deg_parms, ini = c_0)
+head(sfo_sfo_box)
}
diff --git a/man/pfm_degradation.Rd b/man/pfm_degradation.Rd
index 81f2e81..163bcee 100644
--- a/man/pfm_degradation.Rd
+++ b/man/pfm_degradation.Rd
@@ -28,11 +28,36 @@ is calculated (SFO model).}
\item{times}{The output times}
}
+\value{
+A data frame containing the output times and the concentrations
+assuming initial concentrations of 1 for the parent and zero for
+metabolites, if any.
+}
\description{
Calculate a time course of relative concentrations based on an mkinmod model
}
\examples{
-head(pfm_degradation("SFO", DT50 = 10))
+# Simple example of an SFO decline curve
+sfo_out <- pfm_degradation("SFO", DT50 = 10)
+head(sfo_out)
+
+# Fictive example with a metabolite where we first generate an SFO-SFO model
+sfo_sfo <- mkinmod(
+ parent = mkinsub("SFO", to = "metabolite"),
+ metabolite = mkinsub("SFO"))
+
+sfo_sfo_out <- pfm_degradation(sfo_sfo,
+ parms = c(k_parent = 0.1, f_parent_to_metabolite = 0.5, k_metabolite = 0.02))
+
+plot(
+ sfo_sfo_out[, "time"],
+ sfo_sfo_out[, "parent"], type = "l",
+ xlab = "Time", ylab = "Relative concentration",
+ xlim = c(0, 100))
+lines(
+ sfo_sfo_out[, "time"],
+ sfo_sfo_out[, "metabolite"], lty = 2)
+
}
\author{
Johannes Ranke

Contact - Imprint