From d52e522a9abea09fae1189b066e2a2e1dbb9cb34 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 14 Sep 2018 14:06:57 +0200 Subject: Avoid nls crashes by checking for a correlation first --- R/mkinfit.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/mkinfit.R b/R/mkinfit.R index ecbef67e..2c4e3268 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -415,6 +415,13 @@ mkinfit <- function(mkinmod, observed, # We need unweighted residuals to update the weighting cost_tmp <- cost(fit$par) + p_tmp <- cor.test(abs(cost_tmp$residuals$res.unweighted), + cost_tmp$residuals$obs, method = "kendall")$p.value + if (p_tmp > 0.1) { + stop("No correlation of absolute residuals with observed values found.\n", + "Try without reweighting or with reweight.method = 'obs'.") + } + tc_fit <- try( nls(abs(res.unweighted) ~ sigma_twocomp(obs, sigma_low, rsd_high), start = list(sigma_low = tc["sigma_low"], rsd_high = tc["rsd_high"]), -- cgit v1.2.1