jhhughes256 / optinterval

Raw R code for manuscript: Hughes JH, Upton RN, Reuter SE, Phelps MA, Foster DJR. Optimising time samples for determining AUC of pharmacokinetic data using non-compartmental analysis.
0 stars 0 forks source link

GA error; if (object@run >= run) break : missing value where TRUE/FALSE needed #6

Closed jhhughes256 closed 7 years ago

jhhughes256 commented 7 years ago

Issue Description

When running through particular sets of data the GA function can produce NAs that results in error. This should be isolated using try() or other methods and contingency functions put in place in case of error occurence.

Reproducible Example

So far seems to occur most commonly when running study03_broad.R for the d2a and d3a datasets. The error itself must be located within the optim.sumexp function as ga() is used nowhere else.

jhhughes256 commented 7 years ago

Error and Warning Log

Error in if (object@run >= run) break : 
  missing value where TRUE/FALSE needed
Warning messages:
1: In runif(object@popSize, min[j], max[j]) : NAs produced
2: In runif(object@popSize, min[j], max[j]) : NAs produced
3: In max(Fitness, na.rm = TRUE) :
  no non-missing arguments to max; returning -Inf
4: In runif(1, object@min[j], object@max[j]) : NAs produced
5: In runif(1, object@min[j], object@max[j]) : NAs produced
6: In runif(1, object@min[j], object@max[j]) : NAs produced
7: In runif(1, object@min[j], object@max[j]) : NAs produced
8: In runif(1, object@min[j], object@max[j]) : NAs produced
9: In runif(1, object@min[j], object@max[j]) : NAs produced
10: In runif(1, object@min[j], object@max[j]) : NAs produced
11: In runif(1, object@min[j], object@max[j]) : NAs produced
12: In runif(1, object@min[j], object@max[j]) : NAs produced
13: In runif(1, object@min[j], object@max[j]) : NAs produced
14: In runif(1, object@min[j], object@max[j]) : NAs produced
15: In runif(1, object@min[j], object@max[j]) : NAs produced
16: In max(x, na.rm = TRUE) : no non-missing arguments to max; returning -Inf
jhhughes256 commented 7 years ago

Reproducible Example

Set of arguments that cause issues are the following:

> i
[1] 1
> x
[1]  3  6  9 12 15 18 21 24
> y
[1] 6.399365 8.396419 9.076711 8.429958 7.992421 7.987683 8.607468 7.583892
> lmres
[1] 2.035131253 0.003476933

Therefore the following should replicate the error.

x <- c(3, 6, 9, 12, 15, 18, 21, 24)
y <- c(6.4, 8.4, 9.1, 8.4, 8, 8, 8.6, 7.6)

Explanation of Issue

lm() is used to determine the boundaries used in ga(). For the cases where this error occurs, lm() gives a set of boundaries that are impossible causing NAs to be produced.

In the case shown above, lm() fits a linear model with a positive slope, which causes numerical difficulties when trying to fit a curve showing exponential decline.

Potential Solutions

Ensure that lm() does not give a negative value.

jhhughes256 commented 7 years ago

Current Solution

Implemented linear model being calculated from the cmax, finding the terminal slope. Testing required to ensure that function works.

Old code:

lmres <- unname(lm(log(y) ~ x)$coefficients)

New code:

lm.sub <- which(y == max(y))[1]:length(y)
lmres <- unname(lm(log(y[lm.sub]) ~ x[lm.sub])$coefficients)