From 42171ba55222383a0d47e5aacd46a972819e7812 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 15 Apr 2020 18:13:04 +0200 Subject: Include random effects in starting parameters - mean_degparms() now optionally returns starting values for fixed and random effects, which makes it possible to obtain acceptable fits also in more difficult cases (with more parameters) - Fix the anova method, as it is currently not enough to inherit from lme: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17761 - Show fit information, and per default also errmin information in plot.nlme.mmkin() - Examples for nlme.mmkin: Decrease tolerance and increase the number of iterations in the PNLS step in order to be able to fit FOMC-SFO and DFOP-SFO --- man/nlme.Rd | 78 +++++++------------------------------------------- man/nlme.mmkin.Rd | 63 +++++++++++++++++++++++++++++++++++++--- man/plot.mmkin.Rd | 3 +- man/plot.nlme.mmkin.Rd | 15 +++++++++- 4 files changed, 86 insertions(+), 73 deletions(-) (limited to 'man') diff --git a/man/nlme.Rd b/man/nlme.Rd index f31c7a4f..4a668ac0 100644 --- a/man/nlme.Rd +++ b/man/nlme.Rd @@ -8,17 +8,22 @@ \usage{ nlme_function(object) -mean_degparms(object) +mean_degparms(object, random = FALSE) nlme_data(object) } \arguments{ \item{object}{An mmkin row object containing several fits of the same model to different datasets} + +\item{random}{Should a list with fixed and random effects be returned?} } \value{ A function that can be used with nlme -A named vector containing mean values of the fitted degradation model parameters +If random is FALSE (default), a named vector containing mean values + of the fitted degradation model parameters. If random is TRUE, a list with + fixed and random effects, in the format required by the start argument of + nlme for the case of a single grouping variable ds? A \code{\link{groupedData}} object } @@ -67,71 +72,10 @@ m_nlme <- nlme(value ~ nlme_f(name, time, parent_0, log_k_parent_sink), start = mean_dp) summary(m_nlme) plot(augPred(m_nlme, level = 0:1), layout = c(3, 1)) +# augPred does not seem to work on fits with more than one state +# variable -\dontrun{ - # 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"), - A1 = mkinsub("SFO"), use_of_ff = "min") - m_sfo_sfo_ff <- mkinmod(parent = mkinsub("SFO", "A1"), - A1 = mkinsub("SFO"), use_of_ff = "max") - m_fomc_sfo <- mkinmod(parent = mkinsub("FOMC", "A1"), - A1 = mkinsub("SFO")) - m_dfop_sfo <- mkinmod(parent = mkinsub("DFOP", "A1"), - A1 = mkinsub("SFO")) - m_sforb_sfo <- mkinmod(parent = mkinsub("SFORB", "A1"), - A1 = mkinsub("SFO")) - - f_2 <- mmkin(list("SFO-SFO" = m_sfo_sfo, - "SFO-SFO-ff" = m_sfo_sfo_ff, - "FOMC-SFO" = m_fomc_sfo, - "DFOP-SFO" = m_dfop_sfo, - "SFORB-SFO" = m_sforb_sfo), - ds_2) - - grouped_data_2 <- nlme_data(f_2["SFO-SFO", ]) - - mean_dp_sfo_sfo <- mean_degparms(f_2["SFO-SFO", ]) - mean_dp_sfo_sfo_ff <- mean_degparms(f_2["SFO-SFO-ff", ]) - mean_dp_fomc_sfo <- mean_degparms(f_2["FOMC-SFO", ]) - mean_dp_dfop_sfo <- mean_degparms(f_2["DFOP-SFO", ]) - mean_dp_sforb_sfo <- mean_degparms(f_2["SFORB-SFO", ]) - - nlme_f_sfo_sfo <- nlme_function(f_2["SFO-SFO", ]) - nlme_f_sfo_sfo_ff <- nlme_function(f_2["SFO-SFO-ff", ]) - nlme_f_fomc_sfo <- nlme_function(f_2["FOMC-SFO", ]) - assign("nlme_f_sfo_sfo", nlme_f_sfo_sfo, globalenv()) - assign("nlme_f_sfo_sfo_ff", nlme_f_sfo_sfo_ff, globalenv()) - assign("nlme_f_fomc_sfo", nlme_f_fomc_sfo, globalenv()) - - # Allowing for correlations between random effects (not shown) - # leads to non-convergence - f_nlme_sfo_sfo <- nlme(value ~ nlme_f_sfo_sfo(name, time, - parent_0, log_k_parent_sink, log_k_parent_A1, log_k_A1_sink), - data = grouped_data_2, - fixed = parent_0 + log_k_parent_sink + log_k_parent_A1 + log_k_A1_sink ~ 1, - random = pdDiag(parent_0 + log_k_parent_sink + log_k_parent_A1 + log_k_A1_sink ~ 1), - start = mean_dp_sfo_sfo) - # augPred does not see to work on this object, so no plot is shown - - # The same model fitted with transformed formation fractions does not converge - f_nlme_sfo_sfo_ff <- nlme(value ~ nlme_f_sfo_sfo_ff(name, time, - parent_0, log_k_parent, log_k_A1, f_parent_ilr_1), - data = grouped_data_2, - fixed = parent_0 + log_k_parent + log_k_A1 + f_parent_ilr_1 ~ 1, - random = pdDiag(parent_0 + log_k_parent + log_k_A1 + f_parent_ilr_1 ~ 1), - start = mean_dp_sfo_sfo_ff) - - f_nlme_fomc_sfo <- nlme(value ~ nlme_f_fomc_sfo(name, time, - parent_0, log_alpha, log_beta, log_k_A1, f_parent_ilr_1), - data = grouped_data_2, - fixed = parent_0 + log_alpha + log_beta + log_k_A1 + f_parent_ilr_1 ~ 1, - random = pdDiag(parent_0 + log_alpha + log_beta + log_k_A1 + f_parent_ilr_1 ~ 1), - start = mean_dp_fomc_sfo) - - # DFOP-SFO and SFORB-SFO did not converge with full random effects - - anova(f_nlme_fomc_sfo, f_nlme_sfo_sfo) } +\seealso{ +\code{\link{nlme.mmkin}} } diff --git a/man/nlme.mmkin.Rd b/man/nlme.mmkin.Rd index 1fecb5dd..26dcce66 100644 --- a/man/nlme.mmkin.Rd +++ b/man/nlme.mmkin.Rd @@ -2,6 +2,8 @@ % Please edit documentation in R/nlme.mmkin.R \name{nlme.mmkin} \alias{nlme.mmkin} +\alias{print.nlme.mmkin} +\alias{update.nlme.mmkin} \title{Create an nlme model for an mmkin row object} \usage{ \method{nlme}{mmkin}( @@ -20,11 +22,15 @@ control = list(), verbose = FALSE ) + +\method{print}{nlme.mmkin}(x, ...) + +\method{update}{nlme.mmkin}(object, ...) } \arguments{ \item{model}{An \code{\link{mmkin}} row object.} -\item{data}{Ignored, data are taken from the mmkin model} +\item{data}{Should the data be printed?} \item{fixed}{Ignored, all degradation parameters fitted in the mmkin model are used as fixed parameters} @@ -52,6 +58,12 @@ parameters taken from the mmkin object are used} \item{control}{passed to nlme} \item{verbose}{passed to nlme} + +\item{x}{An nlme.mmkin object to print} + +\item{...}{Update specifications passed to update.nlme} + +\item{object}{An nlme.mmkin object to update} } \value{ Upon success, a fitted nlme.mmkin object, which is an nlme object @@ -68,9 +80,52 @@ ds <- lapply(experimental_data_for_UBA_2019[6:10], f <- mmkin("SFO", ds, quiet = TRUE, cores = 1) library(nlme) f_nlme <- nlme(f) -nlme(f, random = parent_0 ~ 1) -f_nlme <- nlme(f, start = c(parent_0 = 100, log_k_parent_sink = 0.1)) -update(f_nlme, random = parent_0 ~ 1) +print(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{ + # 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"), + A1 = mkinsub("SFO"), use_of_ff = "min", quiet = TRUE) + m_sfo_sfo_ff <- mkinmod(parent = mkinsub("SFO", "A1"), + A1 = mkinsub("SFO"), use_of_ff = "max", quiet = TRUE) + m_fomc_sfo <- mkinmod(parent = mkinsub("FOMC", "A1"), + A1 = mkinsub("SFO"), quiet = TRUE) + m_dfop_sfo <- mkinmod(parent = mkinsub("DFOP", "A1"), + A1 = mkinsub("SFO"), quiet = TRUE) + + f_2 <- mmkin(list("SFO-SFO" = m_sfo_sfo, + "SFO-SFO-ff" = m_sfo_sfo_ff, + "FOMC-SFO" = m_fomc_sfo, + "DFOP-SFO" = m_dfop_sfo), + ds_2, quiet = TRUE) + plot(f_2["SFO-SFO", 3:4]) # Separate fits for datasets 3 and 4 + + f_nlme_sfo_sfo <- nlme(f_2["SFO-SFO", ]) + # plot(f_nlme_sfo_sfo) # not feasible with pkgdown figures + plot(f_nlme_sfo_sfo, 3:4) # Global mixed model: Fits for datasets 3 and 4 + + # With formation fractions + f_nlme_sfo_sfo_ff <- nlme(f_2["SFO-SFO-ff", ]) + plot(f_nlme_sfo_sfo_ff, 3:4) # chi2 different due to different df attribution + + # For more parameters, we need to increase pnlsMaxIter and the tolerance + # to get convergence + f_nlme_fomc_sfo <- nlme(f_2["FOMC-SFO", ], + control = list(pnlsMaxIter = 100, tolerance = 1e-4), verbose = TRUE) + f_nlme_dfop_sfo <- nlme(f_2["DFOP-SFO", ], + control = list(pnlsMaxIter = 120, tolerance = 5e-4), verbose = TRUE) + plot(f_2["FOMC-SFO", 3:4]) + plot(f_nlme_fomc_sfo, 3:4) + + plot(f_2["DFOP-SFO", 3:4]) + plot(f_nlme_dfop_sfo, 3:4) + + anova(f_nlme_dfop_sfo, f_nlme_fomc_sfo, f_nlme_sfo_sfo) + anova(f_nlme_dfop_sfo, f_nlme_sfo_sfo) # if we ignore FOMC +} } \seealso{ \code{\link{nlme_function}} diff --git a/man/plot.mmkin.Rd b/man/plot.mmkin.Rd index 982e8db6..600881ea 100644 --- a/man/plot.mmkin.Rd +++ b/man/plot.mmkin.Rd @@ -33,7 +33,7 @@ column.} values, with the error model, using \code{\link{mkinerrplot}}.} \item{standardized}{Should the residuals be standardized? This option -is passed to \code{\link{mkinresplot}}, it only takes effect if +is passed to \code{\link{mkinresplot}}, it only takes effect if `resplot = "time"`.} \item{show_errmin}{Should the chi2 error level be shown on top of the plots @@ -76,6 +76,7 @@ latex is being used for the formatting of the chi2 error level. cores = 1, quiet = TRUE, error_model = "tc") plot(fits[, "FOCUS C"]) plot(fits["FOMC", ]) + plot(fits["FOMC", ], show_errmin = FALSE) # We can also plot a single fit, if we like the way plot.mmkin works, but then the plot # height should be smaller than the plot width (this is not possible for the html pages diff --git a/man/plot.nlme.mmkin.Rd b/man/plot.nlme.mmkin.Rd index c0e749aa..9bea7013 100644 --- a/man/plot.nlme.mmkin.Rd +++ b/man/plot.nlme.mmkin.Rd @@ -11,6 +11,9 @@ legends = 1, resplot = c("time", "errmod"), standardized = FALSE, + show_errmin = TRUE, + errmin_var = "All data", + errmin_digits = 3, cex = 0.7, rel.height.middle = 0.9, ymax = "auto", @@ -35,6 +38,15 @@ values, with the error model, using \code{\link{mkinerrplot}}.} is passed to \code{\link{mkinresplot}}, it only takes effect if `resplot = "time"`.} +\item{show_errmin}{Should the chi2 error level be shown on top of the plots +to the left?} + +\item{errmin_var}{The variable for which the FOCUS chi2 error value should +be shown.} + +\item{errmin_digits}{The number of significant digits for rounding the FOCUS +chi2 error percentage.} + \item{cex}{Passed to the plot functions and \code{\link{mtext}}.} \item{rel.height.middle}{The relative height of the middle plot, if more @@ -56,11 +68,12 @@ ds <- lapply(experimental_data_for_UBA_2019[6:10], function(x) subset(x$data[c("name", "time", "value")], name == "parent")) f <- mmkin("SFO", ds, quiet = TRUE, cores = 1) #plot(f) # too many panels for pkgdown +plot(f[, 3:4]) library(nlme) f_nlme <- nlme(f) #plot(f_nlme) # too many panels for pkgdown -plot(f_nlme, 1:2) +plot(f_nlme, 3:4) } \author{ Johannes Ranke -- cgit v1.2.1