From 518a801a9a0f1aef36df1f201b6a3f0f3a84b779 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Sat, 18 Apr 2020 00:38:26 +0200 Subject: Avoid assignment to .GlobalEnv for CRAN --- NAMESPACE | 2 + R/nlme.mmkin.R | 35 +++-- R/summary.mkinfit.R | 12 +- _pkgdown.yml | 1 + check.log | 28 ++-- docs/reference/get_deg_func.html | 176 ++++++++++++++++++++++++ docs/reference/index.html | 6 + docs/reference/nlme.mmkin.html | 12 +- docs/sitemap.xml | 3 + man/get_deg_func.Rd | 15 ++ man/nlme.mmkin.Rd | 4 +- test.log | 16 +-- tests/testthat/FOCUS_2006_D.csf | 2 +- tests/testthat/summary_DFOP_FOCUS_C.txt | 5 - tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt | 5 - tests/testthat/summary_DFOP_FOCUS_D_eigen.txt | 5 - 16 files changed, 271 insertions(+), 56 deletions(-) create mode 100644 docs/reference/get_deg_func.html create mode 100644 man/get_deg_func.Rd diff --git a/NAMESPACE b/NAMESPACE index ef610857..c5528662 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -40,6 +40,7 @@ export(add_err) export(aw) export(backtransform_odeparms) export(endpoints) +export(get_deg_func) export(ilr) export(invilr) export(loftest) @@ -109,4 +110,5 @@ importFrom(stats,qt) importFrom(stats,residuals) importFrom(stats,rnorm) importFrom(stats,update) +importFrom(utils,getFromNamespace) importFrom(utils,write.table) diff --git a/R/nlme.mmkin.R b/R/nlme.mmkin.R index 1d6c2e75..e58f11cb 100644 --- a/R/nlme.mmkin.R +++ b/R/nlme.mmkin.R @@ -1,3 +1,21 @@ +# Code inspired by nlme::nlme.nlsList and R/nlme_fit.R from nlmixr + +# We need to assign the degradation function created in nlme.mmkin to an +# environment that is always accessible, also e.g. when evaluation is done by +# testthat or pkgdown. Therefore parent.frame() is not good enough. The +# following environment will be in the mkin namespace. +.nlme_env <- new.env(parent = emptyenv()) + +#' Retrieve a degradation function from the mmkin namespace +#' +#' @importFrom utils getFromNamespace +#' @return A function that was likely previously assigned from within +#' nlme.mmkin +#' @export +get_deg_func <- function() { + return(get("deg_func", getFromNamespace(".nlme_env", "mkin"))) +} + #' Create an nlme model for an mmkin row object #' #' This functions sets up a nonlinear mixed effects model for an mmkin row @@ -35,9 +53,9 @@ #' f_nlme <- nlme(f) #' print(f_nlme) #' endpoints(f_nlme) -#' f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) -#' update(f_nlme_2, random = parent_0 ~ 1) #' \dontrun{ +#' f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) +#' update(f_nlme_2, random = parent_0 ~ 1) #' # Test on some real data #' ds_2 <- lapply(experimental_data_for_UBA_2019[6:10], #' function(x) x$data[c("name", "time", "value")]) @@ -83,7 +101,6 @@ #' endpoints(f_nlme_sfo_sfo) #' endpoints(f_nlme_dfop_sfo) #' } -# Code inspired by nlme.nlsList nlme.mmkin <- function(model, data = sys.frame(sys.parent()), fixed, random = fixed, groups, start, correlation = NULL, weights = NULL, @@ -95,19 +112,21 @@ nlme.mmkin <- function(model, data = sys.frame(sys.parent()), thisCall <- as.list(match.call())[-1] - # warn in case of use of arguments that are overriden + # Warn in case arguments were used that are overriden if (any(!is.na(match(names(thisCall), c("fixed", "data"))))) { warning("'nlme.mmkin' will redefine 'fixed' and 'data'") } deg_func <- nlme_function(model) - assign("deg_func", deg_func, globalenv()) - # specify the model formula - this_model_text <- paste0("value ~ deg_func(", + assign("deg_func", deg_func, getFromNamespace(".nlme_env", "mkin")) + + # For the formula, get the degradation function from the mkin namespace + this_model_text <- paste0("value ~ mkin::get_deg_func()(", paste(names(formals(deg_func)), collapse = ", "), ")") - this_model <- eval(parse(text = this_model_text)) + this_model <- as.formula(this_model_text) + thisCall[["model"]] <- this_model mean_dp <- mean_degparms(model) diff --git a/R/summary.mkinfit.R b/R/summary.mkinfit.R index a67f17ee..2dc74bd7 100644 --- a/R/summary.mkinfit.R +++ b/R/summary.mkinfit.R @@ -138,13 +138,11 @@ summary.mkinfit <- function(object, data = TRUE, distimes = TRUE, alpha = 0.05, if (!is.null(object$version)) { ans$fit_version <- object$version ans$fit_Rversion <- object$Rversion - } - - AIC <- try(AIC(object)) - if (!inherits(AIC, "try-error")) { - ans$AIC = AIC(object) - ans$BIC = BIC(object) - ans$logLik = logLik(object) + if (ans$fit_version >= "0.9.49.6") { + ans$AIC = AIC(object) + ans$BIC = BIC(object) + ans$logLik = logLik(object) + } } ans$diffs <- object$mkinmod$diffs diff --git a/_pkgdown.yml b/_pkgdown.yml index 2ede6857..0623c6ed 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -39,6 +39,7 @@ reference: - nlme.mmkin - plot.nlme.mmkin - nlme_function + - get_deg_func - title: Datasets and known results contents: - FOCUS_2006_A diff --git a/check.log b/check.log index 43cc5f64..a50286a5 100644 --- a/check.log +++ b/check.log @@ -41,12 +41,28 @@ Maintainer: ‘Johannes Ranke ’ * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK -* checking R code for possible problems ... OK +* checking R code for possible problems ... NOTE +get_deg_func: no visible global function definition for + ‘getFromNamespace’ +nlme.mmkin: no visible global function definition for + ‘getFromNamespace’ +nlme.mmkin: no visible global function definition for ‘as.formula’ +Undefined global functions or variables: + as.formula getFromNamespace +Consider adding + importFrom("stats", "as.formula") + importFrom("utils", "getFromNamespace") +to your NAMESPACE file. * checking Rd files ... OK * checking Rd metadata ... OK * checking Rd line widths ... OK * checking Rd cross-references ... OK -* checking for missing documentation entries ... OK +* checking for missing documentation entries ... WARNING +Undocumented code objects: + ‘get_deg_func’ +All user-level objects in a package should have documentation entries. +See chapter ‘Writing R documentation files’ in the ‘Writing R +Extensions’ manual. * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking Rd contents ... OK @@ -56,11 +72,7 @@ Maintainer: ‘Johannes Ranke ’ * checking data for ASCII and uncompressed saves ... OK * checking installed files from ‘inst/doc’ ... OK * checking files in ‘vignettes’ ... OK -* checking examples ... NOTE -Examples with CPU or elapsed time > 5s - user system elapsed -nlme.mmkin 4.801 1.040 4.497 -plot.nlme.mmkin 4.751 0.285 4.680 +* checking examples ... OK * checking for unstated dependencies in ‘tests’ ... OK * checking tests ... SKIPPED * checking for unstated dependencies in vignettes ... OK @@ -70,7 +82,7 @@ plot.nlme.mmkin 4.751 0.285 4.680 * checking for detritus in the temp directory ... OK * DONE -Status: 1 NOTE +Status: 1 WARNING, 1 NOTE See ‘/home/jranke/git/mkin/mkin.Rcheck/00check.log’ for details. diff --git a/docs/reference/get_deg_func.html b/docs/reference/get_deg_func.html new file mode 100644 index 00000000..c5b8e38c --- /dev/null +++ b/docs/reference/get_deg_func.html @@ -0,0 +1,176 @@ + + + + + + + + +Retrieve a degradation function from the mmkin namespace — get_deg_func • mkin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + +
+ +
+
+ + +
+

Retrieve a degradation function from the mmkin namespace

+
+ +
get_deg_func()
+ + +

Value

+ +

A function that was likely previously assigned from within + nlme.mmkin

+ +
+ +
+ + +
+ + +
+

Site built with pkgdown 1.4.1.

+
+ +
+
+ + + + + + + + diff --git a/docs/reference/index.html b/docs/reference/index.html index f4de5bd8..04e3b8b9 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -296,6 +296,12 @@ of an mmkin object

nlme_function() mean_degparms() nlme_data()

Helper functions to create nlme models from mmkin row objects

+ + + +

get_deg_func()

+ +

Retrieve a degradation function from the mmkin namespace

diff --git a/docs/reference/nlme.mmkin.html b/docs/reference/nlme.mmkin.html index 9af9cbf7..33cc9164 100644 --- a/docs/reference/nlme.mmkin.html +++ b/docs/reference/nlme.mmkin.html @@ -258,7 +258,7 @@ parameters taken from the mmkin object are used

#> parent 11.96183 39.73634 #>
f_nlme <- nlme(f) print(f_nlme)
#> Nonlinear mixed-effects model fit by maximum likelihood -#> Model: value ~ deg_func(name, time, parent_0, log_k_parent_sink) +#> Model: value ~ (mkin::get_deg_func())(name, time, parent_0, log_k_parent_sink) #> Data: "Not shown" #> Log-likelihood: -307.5269 #> Fixed: list(parent_0 ~ 1, log_k_parent_sink ~ 1) @@ -276,9 +276,10 @@ parameters taken from the mmkin object are used

#> Number of Groups: 5
endpoints(f_nlme)
#> $distimes #> DT50 DT90 #> parent 17.51545 58.18505 -#>
f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) -update(f_nlme_2, random = parent_0 ~ 1)
#> Nonlinear mixed-effects model fit by maximum likelihood -#> Model: value ~ deg_func(name, time, parent_0, log_k_parent_sink) +#>
# \dontrun{ + f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) + update(f_nlme_2, random = parent_0 ~ 1)
#> Nonlinear mixed-effects model fit by maximum likelihood +#> Model: value ~ (mkin::get_deg_func())(name, time, parent_0, log_k_parent_sink) #> Data: "Not shown" #> Log-likelihood: -404.3729 #> Fixed: list(parent_0 ~ 1, log_k_parent_sink ~ 1) @@ -291,8 +292,7 @@ parameters taken from the mmkin object are used

#> StdDev: 0.002416792 21.63027 #> #> Number of Observations: 90 -#> Number of Groups: 5
# \dontrun{ - # Test on some real data +#> Number of Groups: 5
# Test on some real data ds_2 <- lapply(experimental_data_for_UBA_2019[6:10], function(x) x$data[c("name", "time", "value")]) m_sfo_sfo <- mkinmod(parent = mkinsub("SFO", "A1"), diff --git a/docs/sitemap.xml b/docs/sitemap.xml index d1c86707..5abcc894 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -66,6 +66,9 @@ https://pkgdown.jrwb.de/mkin/reference/experimental_data_for_UBA.html + + https://pkgdown.jrwb.de/mkin/reference/get_deg_func.html + https://pkgdown.jrwb.de/mkin/reference/ilr.html diff --git a/man/get_deg_func.Rd b/man/get_deg_func.Rd new file mode 100644 index 00000000..1d8a3de9 --- /dev/null +++ b/man/get_deg_func.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/nlme.mmkin.R +\name{get_deg_func} +\alias{get_deg_func} +\title{Retrieve a degradation function from the mmkin namespace} +\usage{ +get_deg_func() +} +\value{ +A function that was likely previously assigned from within + nlme.mmkin +} +\description{ +Retrieve a degradation function from the mmkin namespace +} diff --git a/man/nlme.mmkin.Rd b/man/nlme.mmkin.Rd index 97d444e4..430dbda1 100644 --- a/man/nlme.mmkin.Rd +++ b/man/nlme.mmkin.Rd @@ -83,9 +83,9 @@ endpoints(f[[1]]) f_nlme <- nlme(f) print(f_nlme) endpoints(f_nlme) -f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) -update(f_nlme_2, random = parent_0 ~ 1) \dontrun{ + f_nlme_2 <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) + update(f_nlme_2, random = parent_0 ~ 1) # Test on some real data ds_2 <- lapply(experimental_data_for_UBA_2019[6:10], function(x) x$data[c("name", "time", "value")]) diff --git a/test.log b/test.log index dd1a1cc0..0a14c115 100644 --- a/test.log +++ b/test.log @@ -7,29 +7,27 @@ Testing mkin ✔ | 4 | Fitting the SFORB model [1.7 s] ✔ | 5 | Calculation of Akaike weights ✔ | 10 | Confidence intervals and p-values [9.4 s] -✔ | 14 | Error model fitting [37.9 s] +✔ | 14 | Error model fitting [37.6 s] ✔ | 6 | Test fitting the decline of metabolites from their maximum [0.8 s] ✔ | 1 | Fitting the logistic model [0.9 s] ✔ | 1 | Test dataset class mkinds used in gmkin ✔ | 12 | Special cases of mkinfit calls [2.3 s] ✔ | 9 | mkinmod model generation and printing [0.2 s] -✔ | 3 | Model predictions with mkinpredict [0.3 s] +✔ | 3 | Model predictions with mkinpredict [0.4 s] ✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.1 s] -✔ | 9 | Nonlinear mixed-effects models [12.0 s] +✔ | 9 | Nonlinear mixed-effects models [11.9 s] ✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.2 s] ✔ | 3 | Summary ✔ | 14 | Plotting [4.9 s] ✔ | 4 | AIC calculation ✔ | 4 | Residuals extracted from mkinfit models -✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.4 s] - ⠏ | 0 | Summaries of old mkinfit objectsError in attr(val, "df") <- length(object$bparms.optim) + length(object$errparms) : - Versuch ein Attribut von NULL zu setzen - ✔ | 1 | Summaries of old mkinfit objects -✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.1 s] +✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.3 s] +✔ | 1 | Summaries of old mkinfit objects +✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.2 s] ✔ | 9 | Hypothesis tests [37.0 s] ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 132.2 s +Duration: 131.8 s OK: 154 Failed: 0 diff --git a/tests/testthat/FOCUS_2006_D.csf b/tests/testthat/FOCUS_2006_D.csf index c61b55c4..818c055a 100644 --- a/tests/testthat/FOCUS_2006_D.csf +++ b/tests/testthat/FOCUS_2006_D.csf @@ -5,7 +5,7 @@ Description: MeasurementUnits: % AR TimeUnits: days Comments: Created using mkin::CAKE_export -Date: 2020-04-17 +Date: 2020-04-18 Optimiser: IRLS [Data] diff --git a/tests/testthat/summary_DFOP_FOCUS_C.txt b/tests/testthat/summary_DFOP_FOCUS_C.txt index 4f96c8d3..14e00f62 100644 --- a/tests/testthat/summary_DFOP_FOCUS_C.txt +++ b/tests/testthat/summary_DFOP_FOCUS_C.txt @@ -33,11 +33,6 @@ g_ilr 0.000000 -Inf Inf Fixed parameter values: None -Results: - - AIC BIC logLik - 29.02372 30.00984 -9.511861 - Optimised, transformed parameters with symmetric confidence intervals: Estimate Std. Error Lower Upper parent_0 85.0000 0.66620 83.1500 86.8500 diff --git a/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt b/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt index 5a9093c3..66ab9348 100644 --- a/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt +++ b/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt @@ -33,11 +33,6 @@ Fixed parameter values: value type m1_0 0 state -Results: - - AIC BIC logLik - 204.4486 212.6365 -97.22429 - Optimised, transformed parameters with symmetric confidence intervals: Estimate Std. Error Lower Upper parent_0 99.600 1.57000 96.400 102.800 diff --git a/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt b/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt index f597cad5..1626c5da 100644 --- a/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt +++ b/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt @@ -33,11 +33,6 @@ Fixed parameter values: value type m1_0 0 state -Results: - - AIC BIC logLik - 204.4486 212.6365 -97.22429 - Optimised, transformed parameters with symmetric confidence intervals: Estimate Std. Error Lower Upper parent_0 99.600 1.57000 96.400 102.800 -- cgit v1.2.1