nlmixr2 / nlmixr2est

nlmixr2 estimation routines
https://nlmixr2.github.io/nlmixr2est/
GNU General Public License v2.0
7 stars 3 forks source link

initial estimates seeting issue for nls #485

Closed ZhonghuiHuang closed 1 month ago

ZhonghuiHuang commented 1 month ago

@mattfidler

Hi Matt,

I find an issue with the "nls" parameter estimation when setting all of the initial estimates to 1. It appears that this setting can result in the parameter values becoming NaN during the first iteration no matter what the dataset is, as reflected in the output shown below.

To address this, I was wondering if you have any suggestions. One potential solution I’ve considered is adjusting the initial estimates slightly to 1.01 or 0.99, as these values seem to prevent the NaN issue and avoid crashes.

Many thanks in advance

Run 1. Set all of initial estimates as 1

library(nlmixr2est)
one.cmt <- function() {
  ini({
    tka <- log(1.01)
    tcl <- log(1.01)
    tv <- log(1.01)
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka)
    cl <- exp(tcl)
    v <- exp(tv)
    linCmt() ~ add(add.sd)
  })
}

# Uses nlsLM from minpack.lm if available
fit1 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="LM"))
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# rxode2 2.1.2 using 4 threads (see ?getRxThreads)
# no cache: create with `rxCreateCache()`
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     0.0000000 |      -nan |      -nan |      -nan |
#   |    U|               |      -nan |      -nan |      -nan |
#   |    X|               |      -nan |      -nan |      -nan |
#   Error in minpack.lm::nls.lm(par = c(NaN, NaN, NaN), lower = c(NaN, NaN,  : 
#                                                                   Non-finite (or null) value for a parameter specified!
#                                                                   Error: Non-finite (or null) value for a parameter specified!

# Uses port and respect parameter boundaries
fit2 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="port"))
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     0.0000000 |      -nan |      -nan |      -nan |
#   |    U|               |      -nan |      -nan |      -nan |
#   |    X|               |      -nan |      -nan |      -nan |
#   Error in nlsModel(formula, mf, start, wts, upper, scaleOffset = scOff,  : 
#                       singular gradient matrix at initial parameter estimates
#                     Error: singular gradient matrix at initial parameter estimates

Run 2. Set all of initial estimates as 1.01

library(nlmixr2est)
one.cmt <- function() {
  ini({
    tka <- log(0.99)
    tcl <- log(0.99)
    tv <- log(0.99)
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka)
    cl <- exp(tcl)
    v <- exp(tv)
    linCmt() ~ add(add.sd)
  })
}

# Uses nlsLM from minpack.lm if available
fit1 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="LM"))

# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     381993.54 |   -0.7071 |   -0.7071 |   -0.7071 |
#   |    U|               |  -0.01005 |  -0.01005 |  -0.01005 |
#   |    X|               |  -0.01005 |  -0.01005 |  -0.01005 |
#   |    2|     4325.0243 |    -19.46 |    0.1353 |    -18.54 |
#   |    U|               |    -18.76 |    0.8324 |    -17.84 |
#   |    X|               |    -18.76 |    0.8324 |    -17.84 |
#   |    3| 3.7452530e+22 |     19.13 |    -30.50 |    -18.54 |
#   |    U|               |     19.83 |    -29.81 |    -17.84 |
#   |    X|               |     19.83 |    -29.81 |    -17.84 |
#   |    4|     4323.7328 |    -15.60 |    -2.928 |    -18.54 |
#   |    U|               |    -14.90 |    -2.231 |    -17.84 |
#   |    X|               |    -14.90 |    -2.231 |    -17.84 |
#   |    5| 1.2260809e+08 |    -7.882 |    -9.056 |    -18.54 |
#   |    U|               |    -7.185 |    -8.359 |    -17.84 |
#   |    X|               |    -7.185 |    -8.359 |    -17.84 |
#   |    6|     4319.8478 |    -14.83 |    -3.543 |    -18.54 |
#   |    U|               |    -14.13 |    -2.846 |    -17.84 |
#   |    X|               |    -14.13 |    -2.846 |    -17.84 |
#   |    7|     4242.3841 |    -13.28 |    -4.771 |    -18.54 |
#   |    U|               |    -12.58 |    -4.074 |    -17.84 |
#   |    X|               |    -12.58 |    -4.074 |    -17.84 |
#   |    8|     15131.486 |    -10.19 |    -7.228 |    -18.54 |
#   |    U|               |    -9.489 |    -6.531 |    -17.84 |
#   |    X|               |    -9.489 |    -6.531 |    -17.84 |
#   |    9|     4176.6469 |    -12.95 |    -5.032 |    -18.54 |
#   |    U|               |    -12.25 |    -4.335 |    -17.84 |
#   |    X|               |    -12.25 |    -4.335 |    -17.84 |
#   |   10|     3853.7568 |    -12.29 |    -5.554 |    -18.54 |
#   |    U|               |    -11.60 |    -4.857 |    -17.84 |
#   |    X|               |    -11.60 |    -4.857 |    -17.84 |
#   |   11|     1035.1555 |    -10.98 |    -6.599 |    -18.54 |
#   |    U|               |    -10.28 |    -5.902 |    -17.84 |
#   |    X|               |    -10.28 |    -5.902 |    -17.84 |
#   |   12|     775.75187 |    -8.424 |    -4.421 |    -18.54 |
#   |    U|               |    -7.727 |    -3.724 |    -17.84 |
#   |    X|               |    -7.727 |    -3.724 |    -17.84 |
#   |   13|     746.77365 |    -3.742 |    0.2999 |    -18.54 |
#   |    U|               |    -3.045 |    0.9970 |    -17.84 |
#   |    X|               |    -3.045 |    0.9970 |    -17.84 |
#   |   14|     575.02789 |    -4.143 |   -0.2979 |    -32.73 |
#   |    U|               |    -3.446 |    0.3991 |    -32.04 |
#   |    X|               |    -3.446 |    0.3991 |    -32.04 |
#   |   15|     570.99361 |    -3.962 |   -0.1199 |    -61.17 |
#   |    U|               |    -3.265 |    0.5772 |    -60.48 |
#   |    X|               |    -3.265 |    0.5772 |    -60.48 |
#   |   16|     570.74568 |    -4.015 |   -0.1667 |    -4.294 |
#   |    U|               |    -3.318 |    0.5304 |    -3.597 |
#   |    X|               |    -3.318 |    0.5304 |    -3.597 |
#   |   17|     4325.0256 |    -4.001 |   -0.1206 |     52.58 |
#   |    U|               |    -3.304 |    0.5765 |     53.28 |
#   |    X|               |    -3.304 |    0.5765 |     53.28 |
#   |   18|     1852.2179 |    -4.001 |   -0.1505 |     1.394 |
#   |    U|               |    -3.304 |    0.5466 |     2.091 |
#   |    X|               |    -3.304 |    0.5466 |     2.091 |
#   |   19|     570.67578 |    -4.001 |   -0.1535 |    -3.725 |
#   |    U|               |    -3.304 |    0.5436 |    -3.028 |
#   |    X|               |    -3.304 |    0.5436 |    -3.028 |
#   |   20|     542.93301 |    -4.006 |   -0.1569 |    -2.587 |
#   |    U|               |    -3.309 |    0.5402 |    -1.890 |
#   |    X|               |    -3.309 |    0.5402 |    -1.890 |
#   |   21|     490.30807 |    -3.916 |   -0.1347 |   -0.2950 |
#   |    U|               |    -3.219 |    0.5624 |    0.4020 |
#   |    X|               |    -3.219 |    0.5624 |    0.4020 |
#   |   22|     282.82274 |    -3.110 |    0.4241 |   -0.2483 |
#   |    U|               |    -2.413 |     1.121 |    0.4488 |
#   |    X|               |    -2.413 |     1.121 |    0.4488 |
#   |   23|     249.96386 |    -3.236 |    0.2588 |   -0.1597 |
#   |    U|               |    -2.539 |    0.9558 |    0.5373 |
#   |    X|               |    -2.539 |    0.9558 |    0.5373 |
#   |   24|     249.71262 |    -3.229 |    0.2639 |   -0.1785 |
#   |    U|               |    -2.532 |    0.9609 |    0.5186 |
#   |    X|               |    -2.532 |    0.9609 |    0.5186 |
#   |   25|     249.71189 |    -3.228 |    0.2645 |   -0.1756 |
#   |    U|               |    -2.531 |    0.9616 |    0.5215 |
#   |    X|               |    -2.531 |    0.9616 |    0.5215 |
#   |   26|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   27|     249.71189 |    -3.228 |    0.2644 |   -0.1758 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |-----+---------------+-----------+-----------+-----------|
#   → calculating covariance
# ✔ done
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 5952
# → compress parHistData in nlmixr2 object, save 3112

# Uses port and respect parameter boundaries
fit2 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="port"))
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     381993.54 |   -0.7071 |   -0.7071 |   -0.7071 |
#   |    U|               |  -0.01005 |  -0.01005 |  -0.01005 |
#   |    X|               |  -0.01005 |  -0.01005 |  -0.01005 |
#   |    2|     373997.79 |   -0.7143 |   -0.7013 |   -0.6999 |
#   |    U|               |  -0.01722 | -0.004207 | -0.002874 |
#   |    X|               |  -0.01722 | -0.004207 | -0.002874 |
#   |    3|     350937.52 |   -0.7357 |   -0.6835 |   -0.6785 |
#   |    U|               |  -0.03861 |   0.01354 |   0.01853 |
#   |    X|               |  -0.03861 |   0.01354 |   0.01853 |
#   |    4|     271251.14 |   -0.8192 |   -0.6088 |   -0.5949 |
#   |    U|               |   -0.1222 |   0.08823 |    0.1022 |
#   |    X|               |   -0.1222 |   0.08823 |    0.1022 |
#   |    5|     109311.76 |    -1.064 |   -0.2942 |   -0.3500 |
#   |    U|               |   -0.3672 |    0.4029 |    0.3470 |
#   |    X|               |   -0.3672 |    0.4029 |    0.3470 |
#   |    6|     8543.8898 |    -1.877 |    0.4342 |   -0.3054 |
#   |    U|               |    -1.180 |     1.131 |    0.3917 |
#   |    X|               |    -1.180 |     1.131 |    0.3917 |
#   |    7|     868.61152 |    -2.484 |    0.6819 |   -0.1958 |
#   |    U|               |    -1.787 |     1.379 |    0.5012 |
#   |    X|               |    -1.787 |     1.379 |    0.5012 |
#   |    8|     281.34338 |    -3.114 |    0.4170 |   -0.2574 |
#   |    U|               |    -2.417 |     1.114 |    0.4396 |
#   |    X|               |    -2.417 |     1.114 |    0.4396 |
#   |    9|     249.95643 |    -3.235 |    0.2594 |   -0.1593 |
#   |    U|               |    -2.538 |    0.9565 |    0.5378 |
#   |    X|               |    -2.538 |    0.9565 |    0.5378 |
#   |   10|     249.71260 |    -3.229 |    0.2639 |   -0.1785 |
#   |    U|               |    -2.532 |    0.9609 |    0.5186 |
#   |    X|               |    -2.532 |    0.9609 |    0.5186 |
#   |   11|     249.71189 |    -3.228 |    0.2645 |   -0.1756 |
#   |    U|               |    -2.531 |    0.9616 |    0.5215 |
#   |    X|               |    -2.531 |    0.9616 |    0.5215 |
#   |   12|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   13|     249.71189 |    -3.228 |    0.2644 |   -0.1758 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   14|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   15|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   16|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   17|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   18|     249.71189 |    -3.228 |    0.2644 |   -0.1759 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |-----+---------------+-----------+-----------+-----------|
#   → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 5952
# → compress parHistData in nlmixr2 object, save 2920

Run 3. Set all of initial estimates as 1.01

library(nlmixr2est)
one.cmt <- function() {
  ini({
    tka <- log(1.01)
    tcl <- log(1.01)
    tv <- log(1.01)
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka)
    cl <- exp(tcl)
    v <- exp(tv)
    linCmt() ~ add(add.sd)
  })
}

# Uses nlsLM from minpack.lm if available
fit1 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="LM"))
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     373422.45 |    0.7071 |    0.7071 |    0.7071 |
#   |    U|               |  0.009950 |  0.009950 |  0.009950 |
#   |    X|               |  0.009950 |  0.009950 |  0.009950 |
#   |    2|     4325.0248 |     19.84 |     1.548 |     20.76 |
#   |    U|               |     19.14 |    0.8504 |     20.07 |
#   |    X|               |     19.14 |    0.8504 |     20.07 |
#   |    3|     4325.0256 |     19.84 |     1.548 |    -34.60 |
#   |    U|               |     19.14 |    0.8504 |    -35.30 |
#   |    X|               |     19.14 |    0.8504 |    -35.30 |
#   |    4|     4325.0256 |     19.84 |     1.548 |    -6.432 |
#   |    U|               |     19.14 |    0.8504 |    -7.130 |
#   |    X|               |     19.14 |    0.8504 |    -7.130 |
#   |    5|     3944.6522 |     19.84 |     1.548 |     7.648 |
#   |    U|               |     19.14 |    0.8504 |     6.951 |
#   |    X|               |     19.14 |    0.8504 |     6.951 |
#   |    6|     4325.0256 |     19.84 |     19.42 |    -10.19 |
#   |    U|               |     19.14 |     18.72 |    -10.88 |
#   |    X|               |     19.14 |     18.72 |    -10.88 |
#   |    7|     4325.0256 |     19.84 |     1.531 |    -4.740 |
#   |    U|               |     19.14 |    0.8341 |    -5.437 |
#   |    X|               |     19.14 |    0.8341 |    -5.437 |
#   |    8|     127968.49 |     19.84 |     1.508 |     1.934 |
#   |    U|               |     19.14 |    0.8113 |     1.237 |
#   |    X|               |     19.14 |    0.8113 |     1.237 |
#   |    9|     3670.8572 |     19.84 |     1.543 |     7.076 |
#   |    U|               |     19.14 |    0.8458 |     6.379 |
#   |    X|               |     19.14 |    0.8458 |     6.379 |
#   |   10|     2543.4099 |     19.84 |     1.527 |     5.934 |
#   |    U|               |     19.14 |    0.8301 |     5.237 |
#   |    X|               |     19.14 |    0.8301 |     5.237 |
#   |   11|     4581.2196 |     19.84 |     1.539 |     3.646 |
#   |    U|               |     19.14 |    0.8422 |     2.949 |
#   |    X|               |     19.14 |    0.8422 |     2.949 |
#   |   12|     1408.6841 |     19.84 |     1.500 |     5.233 |
#   |    U|               |     19.14 |    0.8033 |     4.536 |
#   |    X|               |     19.14 |    0.8033 |     4.536 |
#   |   13|     879.08869 |     18.81 |     1.659 |     4.222 |
#   |    U|               |     18.11 |    0.9614 |     3.525 |
#   |    X|               |     18.11 |    0.9614 |     3.525 |
#   |   14|     572.40186 |     17.48 |     1.304 |     4.553 |
#   |    U|               |     16.79 |    0.6068 |     3.856 |
#   |    X|               |     16.79 |    0.6068 |     3.856 |
#   |   15|     570.76519 |     14.66 |     1.223 |     4.547 |
#   |    U|               |     13.96 |    0.5260 |     3.849 |
#   |    X|               |     13.96 |    0.5260 |     3.849 |
#   |   16|     570.73010 |     9.006 |     1.241 |     4.543 |
#   |    U|               |     8.309 |    0.5439 |     3.846 |
#   |    X|               |     8.309 |    0.5439 |     3.846 |
#   |   17|     570.72717 |     14.66 |     1.236 |     4.544 |
#   |    U|               |     13.96 |    0.5387 |     3.847 |
#   |    X|               |     13.96 |    0.5387 |     3.847 |
#   |   18|     570.72694 |     20.31 |     1.237 |     4.544 |
#   |    U|               |     19.61 |    0.5402 |     3.847 |
#   |    X|               |     19.61 |    0.5402 |     3.847 |
#   |   19|     570.72692 |     25.96 |     1.237 |     4.544 |
#   |    U|               |     25.26 |    0.5398 |     3.847 |
#   |    X|               |     25.26 |    0.5398 |     3.847 |
#   |   20|     570.72692 |     20.31 |     1.237 |     4.544 |
#   |    U|               |     19.61 |    0.5399 |     3.847 |
#   |    X|               |     19.61 |    0.5399 |     3.847 |
#   |-----+---------------+-----------+-----------+-----------|
#   → calculating covariance
# ✔ done
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 5952
# → compress parHistData in nlmixr2 object, save 2944
fit2 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="port"))
# 
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tka |       tcl |        tv |
# |-----+---------------+-----------+-----------+-----------|
#   |    1|     373422.45 |    0.7071 |    0.7071 |    0.7071 |
#   |    U|               |  0.009950 |  0.009950 |  0.009950 |
#   |    X|               |  0.009950 |  0.009950 |  0.009950 |
#   |    2|     365528.50 |    0.6999 |    0.7130 |    0.7144 |
#   |    U|               |  0.002697 |   0.01586 |   0.01720 |
#   |    X|               |  0.002697 |   0.01586 |   0.01720 |
#   |    3|     342769.92 |    0.6782 |    0.7310 |    0.7360 |
#   |    U|               |  -0.01894 |   0.03382 |   0.03881 |
#   |    X|               |  -0.01894 |   0.03382 |   0.03881 |
#   |    4|     264230.92 |    0.5937 |    0.8066 |    0.8204 |
#   |    U|               |   -0.1035 |    0.1094 |    0.1233 |
#   |    X|               |   -0.1035 |    0.1094 |    0.1233 |
#   |    5|     106586.06 |    0.3489 |     1.121 |     1.065 |
#   |    U|               |   -0.3483 |    0.4241 |    0.3682 |
#   |    X|               |   -0.3483 |    0.4241 |    0.3682 |
#   |    6|     7918.7026 |   -0.5111 |     1.846 |     1.062 |
#   |    U|               |    -1.208 |     1.149 |    0.3645 |
#   |    X|               |    -1.208 |     1.149 |    0.3645 |
#   |    7|     819.38792 |    -1.120 |     2.072 |     1.174 |
#   |    U|               |    -1.818 |     1.374 |    0.4764 |
#   |    X|               |    -1.818 |     1.374 |    0.4764 |
#   |    8|     277.15408 |    -1.738 |     1.796 |     1.133 |
#   |    U|               |    -2.435 |     1.099 |    0.4357 |
#   |    X|               |    -2.435 |     1.099 |    0.4357 |
#   |    9|     249.93642 |    -1.839 |     1.654 |     1.237 |
#   |    U|               |    -2.536 |    0.9570 |    0.5394 |
#   |    X|               |    -2.536 |    0.9570 |    0.5394 |
#   |   10|     249.71266 |    -1.835 |     1.658 |     1.216 |
#   |    U|               |    -2.532 |    0.9609 |    0.5184 |
#   |    X|               |    -2.532 |    0.9609 |    0.5184 |
#   |   11|     249.71189 |    -1.834 |     1.659 |     1.219 |
#   |    U|               |    -2.531 |    0.9616 |    0.5215 |
#   |    X|               |    -2.531 |    0.9616 |    0.5215 |
#   |   12|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   13|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   14|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   15|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   16|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   17|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |   18|     249.71189 |    -1.834 |     1.659 |     1.218 |
#   |    U|               |    -2.531 |    0.9615 |    0.5212 |
#   |    X|               |    -2.531 |    0.9615 |    0.5212 |
#   |-----+---------------+-----------+-----------+-----------|
#   → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 5952
# → compress parHistData in nlmixr2 object, save 2920

Run 4. Pheno case with initials set as 1.

pheno <- function() {
  ini({
    tcl <- log(1) # typical value of clearance
    tv <-  log(1)   # typical value of volume
    add.err <- 0.1    # residual variability
  })
  model({
    cl <- exp(tcl ) # individual value of clearance
    v <- exp(tv)    # individual value of volume
    ke <- cl / v            # elimination rate constant
    d/dt(A1) = - ke * A1    # model differential equation
    cp = A1 / v             # concentration in plasma
    cp ~ add(add.err)       # define error model
  })
}

fit1 <- nlmixr(pheno, nlmixr2data::pheno_sd, est="nls", nlsControl(algorithm="LM"))
# ℹ parameter labels from comments will be replaced by 'label()'
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate sensitivities
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tcl |        tv |
# |-----+---------------+-----------+-----------|
#   |    1|     0.0000000 |      -nan |      -nan |
#   |    U|               |      -nan |      -nan |
#   |    X|               |      -nan |      -nan |
#   Error in minpack.lm::nls.lm(par = c(NaN, NaN), lower = c(NaN, NaN), upper = c(NaN,  : 
#                                                                                   Non-finite (or null) value for a parameter specified!
#                                                                                   Error: Non-finite (or null) value for a parameter specified!
fit2 <- nlmixr(pheno, nlmixr2data::pheno_sd, est="nls", nlsControl(algorithm="port"))
# 
# ℹ parameter labels from comments will be replaced by 'label()'
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate sensitivities
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tcl |        tv |
# |-----+---------------+-----------+-----------|
#   |    1|     0.0000000 |      -nan |      -nan |
#   |    U|               |      -nan |      -nan |
#   |    X|               |      -nan |      -nan |
#   Error in nlsModel(formula, mf, start, wts, upper, scaleOffset = scOff,  : 
#                       singular gradient matrix at initial parameter estimates
#                     Error: singular gradient matrix at initial parameter estimates

Run 5. Pheno case by initials set as 1.01

pheno <- function() {
  ini({
    tcl <- log(1.01) # typical value of clearance
    tv <-  log(1.01)   # typical value of volume
    add.err <- 0.1    # residual variability
  })
  model({
    cl <- exp(tcl ) # individual value of clearance
    v <- exp(tv)    # individual value of volume
    ke <- cl / v            # elimination rate constant
    d/dt(A1) = - ke * A1    # model differential equation
    cp = A1 / v             # concentration in plasma
    cp ~ add(add.err)       # define error model
  })
}

fit1 <- nlmixr(pheno, nlmixr2data::pheno_sd, est="nls", nlsControl(algorithm="LM"))
# ℹ parameter labels from comments will be replaced by 'label()'
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate sensitivities
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tcl |        tv |
# |-----+---------------+-----------+-----------|
#   |    1|     102996.96 |     1.000 |     1.000 |
#   |    U|               |  0.009950 |  0.009950 |
#   |    X|               |  0.009950 |  0.009950 |
#   |    2|     90157.696 |   -0.3161 |     1.491 |
#   |    U|               |    -1.306 |    0.5009 |
#   |    X|               |    -1.306 |    0.5009 |
#   |    3|     45884.071 |    -2.891 |     2.137 |
#   |    U|               |    -3.881 |     1.147 |
#   |    X|               |    -3.881 |     1.147 |
#   |    4|     31359.070 |    -4.192 |     1.501 |
#   |    U|               |    -5.182 |    0.5113 |
#   |    X|               |    -5.182 |    0.5113 |
#   |    5|     27118.289 |    -3.830 |     1.629 |
#   |    U|               |    -4.820 |    0.6389 |
#   |    X|               |    -4.820 |    0.6389 |
#   |    6|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |    7|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |-----+---------------+-----------+-----------|
#   → calculating covariance
# ✔ done
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
# 
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 33192
# → compress parHistData in nlmixr2 object, save 2248
fit2 <- nlmixr(pheno, nlmixr2data::pheno_sd, est="nls", nlsControl(algorithm="port"))
# ℹ parameter labels from comments will be replaced by 'label()'
# → loading into symengine environment...
# → pruning branches (`if`/`else`) of nls model...
# ✔ done
# → calculate jacobian
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate sensitivities
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → calculate ∂(f)/∂(θ)
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in nls gradient...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → finding duplicate expressions in nls pred-only...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# Key: U: Unscaled Parameters; X: Back-transformed parameters; 
# 
# |    #| Function Val. |       tcl |        tv |
# |-----+---------------+-----------+-----------|
#   |    1|     102996.96 |     1.000 |     1.000 |
#   |    U|               |  0.009950 |  0.009950 |
#   |    X|               |  0.009950 |  0.009950 |
#   |    2|     102590.79 |    0.9838 |     1.048 |
#   |    U|               | -0.006285 |   0.05815 |
#   |    X|               | -0.006285 |   0.05815 |
#   |    3|     101538.43 |    0.9348 |     1.192 |
#   |    U|               |  -0.05527 |    0.2020 |
#   |    X|               |  -0.05527 |    0.2020 |
#   |    4|     100825.30 |    0.8914 |     1.317 |
#   |    U|               |  -0.09867 |    0.3272 |
#   |    X|               |  -0.09867 |    0.3272 |
#   |    5|     98201.313 |    0.5884 |     1.621 |
#   |    U|               |   -0.4016 |    0.6313 |
#   |    X|               |   -0.4016 |    0.6313 |
#   |    6|     94707.572 |    0.2671 |     1.263 |
#   |    U|               |   -0.7229 |    0.2733 |
#   |    X|               |   -0.7229 |    0.2733 |
#   |    7|     92105.373 |  -0.02896 |     1.189 |
#   |    U|               |    -1.019 |    0.1990 |
#   |    X|               |    -1.019 |    0.1990 |
#   |    8|     71990.635 |    -2.090 |     2.700 |
#   |    U|               |    -3.080 |     1.710 |
#   |    X|               |    -3.080 |     1.710 |
#   |    9|     267558.70 |    -5.785 |    0.8711 |
#   |    U|               |    -6.775 |   -0.1189 |
#   |    X|               |    -6.775 |   -0.1189 |
#   |   10|     56967.392 |    -2.770 |     2.472 |
#   |    U|               |    -3.760 |     1.482 |
#   |    X|               |    -3.760 |     1.482 |
#   |   11|     28037.937 |    -3.740 |     1.762 |
#   |    U|               |    -4.730 |    0.7721 |
#   |    X|               |    -4.730 |    0.7721 |
#   |   12|     27104.958 |    -3.798 |     1.627 |
#   |    U|               |    -4.788 |    0.6371 |
#   |    X|               |    -4.788 |    0.6371 |
#   |   13|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |   14|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |   15|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |   16|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |   17|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |   18|     27101.897 |    -3.799 |     1.635 |
#   |    U|               |    -4.789 |    0.6453 |
#   |    X|               |    -4.789 |    0.6453 |
#   |-----+---------------+-----------+-----------|
#   → loading into symengine environment...
# → pruning branches (`if`/`else`) of full model...
# ✔ done
# → finding duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → optimizing duplicate expressions in EBE model...
# [====|====|====|====|====|====|====|====|====|====] 0:00:00 
# 
# → compiling EBE model...
# ✔ done
# → Calculating residuals/tables
# ✔ done
# → compress origData in nlmixr2 object, save 33192
# → compress parHistData in nlmixr2 object, save 2616
# > fit2 
# ── nlmixr² nls with port algorithm ──
# 
# OBJF      AIC      BIC Log-likelihood Condition#(Cov) Condition#(Cor)
# Pop 955.4098 1246.281 1255.411      -620.1403         10.3313        4.261143
# 
# ── Time (sec fit2$time): ──
# 
# setup table compress    other
# elapsed 0.001835 0.019     0.01 1.431165
# 
# ── (fit2$parFixed or fit2$parFixedDf): ──
# 
# Parameter   Est.       SE   %RSE       Back-transformed(95%CI) BSV(SD) Shrink(SD)%
# tcl     typical value of clearance -4.789  0.01331  0.278 0.008319 (0.008105, 0.008539)                    
# tv         typical value of volume 0.6453 0.005692 0.8821          1.907 (1.885, 1.928)                    
# add.err       residual variability  12.07                                         12.07                    
# 
# Covariance Type (fit2$covMethod): nls
# Censoring (fit2$censInformation): No censoring
# Minimization message (fit2$message):  
#   both X-convergence and relative convergence (5) 
# 
# ── Fit Data (object fit2 is a modified tibble): ──
# # A tibble: 155 × 13
# ID     TIME    DV IPRED  IRES IWRES    cp    A1      cl     v      ke   tad dosenum
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl>   <dbl> <dbl>   <dbl>
#   1 1        2   17.3 13.0   4.30 0.356 13.0   24.8 0.00832  1.91 0.00436     2       1
# 2 1      112.  31   21.3   9.68 0.802 21.3   40.6 0.00832  1.91 0.00436     4      10
# 3 2        2    9.7  7.80  1.90 0.158  7.80  14.9 0.00832  1.91 0.00436     2       1

}

mattfidler commented 1 month ago

Perhaps you should update nlmixr2?

With the latest dependencies I cannot reproduce your issue:

    library(nlmixr2est)
#> Loading required package: nlmixr2data
#> detected new version of nlmixr2est, cleaning rxode2 cache
    one.cmt <- function() {
      ini({
        tka <- log(1.01)
        tcl <- log(1.01)
        tv <- log(1.01)
        add.sd <- 0.7
      })
      model({
        ka <- exp(tka)
        cl <- exp(tcl)
        v <- exp(tv)
        linCmt() ~ add(add.sd)
      })
    }

    # Uses nlsLM from minpack.lm if available
    fit1 <- nlmixr(one.cmt, nlmixr2data::theo_sd, est="nls", nlsControl(algorithm="LM"))
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> → loading into symengine environment...
#> → pruning branches (`if`/`else`) of nls model...
#> ✔ done
#> → calculate jacobian
#> → calculate ∂(f)/∂(θ)
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → finding duplicate expressions in nls gradient...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → optimizing duplicate expressions in nls gradient...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → finding duplicate expressions in nls pred-only...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> rxode2 3.0.1 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
#> 
#> Attaching package: 'rxode2'
#> The following objects are masked from 'package:nlmixr2est':
#> 
#>     boxCox, yeoJohnson
#> Key: U: Unscaled Parameters; X: Back-transformed parameters; 
#> 
#> |    #| Function Val. |       tka |       tcl |        tv |
#> |-----+---------------+-----------+-----------+-----------|
#> |    1|     373422.45 |    0.7071 |    0.7071 |    0.7071 |
#> |    U|               |  0.009950 |  0.009950 |  0.009950 |
#> |    X|               |  0.009950 |  0.009950 |  0.009950 |
#> |    2|     4325.0248 |     19.84 |     1.548 |     20.76 |
#> |    U|               |     19.14 |    0.8504 |     20.07 |
#> |    X|               |     19.14 |    0.8504 |     20.07 |
#> |    3|     4325.0256 |     19.84 |     1.548 |    -34.60 |
#> |    U|               |     19.14 |    0.8504 |    -35.30 |
#> |    X|               |     19.14 |    0.8504 |    -35.30 |
#> |    4|     4325.0256 |     19.84 |     1.548 |    -6.432 |
#> |    U|               |     19.14 |    0.8504 |    -7.130 |
#> |    X|               |     19.14 |    0.8504 |    -7.130 |
#> |    5|     3944.6522 |     19.84 |     1.548 |     7.648 |
#> |    U|               |     19.14 |    0.8504 |     6.951 |
#> |    X|               |     19.14 |    0.8504 |     6.951 |
#> |    6|     4325.0256 |     19.84 |     19.42 |    -10.19 |
#> |    U|               |     19.14 |     18.72 |    -10.88 |
#> |    X|               |     19.14 |     18.72 |    -10.88 |
#> |    7|     4325.0256 |     19.84 |     1.531 |    -4.740 |
#> |    U|               |     19.14 |    0.8341 |    -5.437 |
#> |    X|               |     19.14 |    0.8341 |    -5.437 |
#> |    8|     127968.49 |     19.84 |     1.508 |     1.934 |
#> |    U|               |     19.14 |    0.8113 |     1.237 |
#> |    X|               |     19.14 |    0.8113 |     1.237 |
#> |    9|     3670.8572 |     19.84 |     1.543 |     7.076 |
#> |    U|               |     19.14 |    0.8458 |     6.379 |
#> |    X|               |     19.14 |    0.8458 |     6.379 |
#> |   10|     2543.4099 |     19.84 |     1.527 |     5.934 |
#> |    U|               |     19.14 |    0.8301 |     5.237 |
#> |    X|               |     19.14 |    0.8301 |     5.237 |
#> |   11|     4581.2196 |     19.84 |     1.539 |     3.646 |
#> |    U|               |     19.14 |    0.8422 |     2.949 |
#> |    X|               |     19.14 |    0.8422 |     2.949 |
#> |   12|     1408.6841 |     19.84 |     1.500 |     5.233 |
#> |    U|               |     19.14 |    0.8033 |     4.536 |
#> |    X|               |     19.14 |    0.8033 |     4.536 |
#> |   13|     879.08869 |     18.81 |     1.659 |     4.222 |
#> |    U|               |     18.11 |    0.9614 |     3.525 |
#> |    X|               |     18.11 |    0.9614 |     3.525 |
#> |   14|     572.40186 |     17.48 |     1.304 |     4.553 |
#> |    U|               |     16.79 |    0.6068 |     3.856 |
#> |    X|               |     16.79 |    0.6068 |     3.856 |
#> |   15|     570.76519 |     14.66 |     1.223 |     4.547 |
#> |    U|               |     13.96 |    0.5260 |     3.849 |
#> |    X|               |     13.96 |    0.5260 |     3.849 |
#> |   16|     570.73010 |     9.006 |     1.241 |     4.543 |
#> |    U|               |     8.309 |    0.5439 |     3.846 |
#> |    X|               |     8.309 |    0.5439 |     3.846 |
#> |   17|     570.72717 |     14.66 |     1.236 |     4.544 |
#> |    U|               |     13.96 |    0.5387 |     3.847 |
#> |    X|               |     13.96 |    0.5387 |     3.847 |
#> |   18|     570.72694 |     20.31 |     1.237 |     4.544 |
#> |    U|               |     19.61 |    0.5402 |     3.847 |
#> |    X|               |     19.61 |    0.5402 |     3.847 |
#> |   19|     570.72692 |     25.96 |     1.237 |     4.544 |
#> |    U|               |     25.26 |    0.5398 |     3.847 |
#> |    X|               |     25.26 |    0.5398 |     3.847 |
#> |   20|     570.72692 |     20.31 |     1.237 |     4.544 |
#> |    U|               |     19.61 |    0.5399 |     3.847 |
#> |    X|               |     19.61 |    0.5399 |     3.847 |
#> |-----+---------------+-----------+-----------+-----------|
#> → calculating covariance
#> ✔ done
#> → loading into symengine environment...
#> → pruning branches (`if`/`else`) of full model...
#> ✔ done
#> → finding duplicate expressions in EBE model...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → optimizing duplicate expressions in EBE model...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → compiling EBE model...
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> ✔ done
#> → Calculating residuals/tables
#> ✔ done
#> → compress origData in nlmixr2 object, save 5952
#> → compress parHistData in nlmixr2 object, save 2944

Created on 2024-10-16 with reprex v2.1.1

mattfidler commented 1 month ago

Well actually you are setting all initials to zero.

However, I can reproduce (I copied the wrong model)

    library(nlmixr2est)
#> Loading required package: nlmixr2data
    pheno <- function() {
      ini({
        tcl <- log(1) # typical value of clearance
        tv <-  log(1)   # typical value of volume
        add.err <- 0.1    # residual variability
      })
      model({
        cl <- exp(tcl ) # individual value of clearance
        v <- exp(tv)    # individual value of volume
        ke <- cl / v            # elimination rate constant
        d/dt(A1) = - ke * A1    # model differential equation
        cp = A1 / v             # concentration in plasma
        cp ~ add(add.err)       # define error model
      })
    }

    fit1 <- nlmixr(pheno, nlmixr2data::pheno_sd, est="nls", nlsControl(algorithm="LM"))
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> → loading into symengine environment...
#> → pruning branches (`if`/`else`) of nls model...
#> ✔ done
#> → calculate jacobian
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → calculate sensitivities
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → calculate ∂(f)/∂(θ)
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → finding duplicate expressions in nls gradient...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → optimizing duplicate expressions in nls gradient...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → finding duplicate expressions in nls pred-only...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’
#> rxode2 3.0.1 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
#> 
#> Attaching package: 'rxode2'
#> The following objects are masked from 'package:nlmixr2est':
#> 
#>     boxCox, yeoJohnson
#> Key: U: Unscaled Parameters; X: Back-transformed parameters; 
#> 
#> |    #| Function Val. |       tcl |        tv |
#> |-----+---------------+-----------+-----------|
#> |    1|     0.0000000 |      -nan |      -nan |
#> |    U|               |      -nan |      -nan |
#> |    X|               |      -nan |      -nan |
#> Error in minpack.lm::nls.lm(par = c(NaN, NaN), lower = c(NaN, NaN), upper = c(NaN,  : 
#>   Non-finite (or null) value for a parameter specified!
#> Error: Non-finite (or null) value for a parameter specified!

Created on 2024-10-16 with reprex v2.1.1

mattfidler commented 1 month ago

In general, estimations without relative sizes between parameters cannot be scaled well (my guess is this is a scaling problem). In NONMEM zero is not allowed as an initial estimate since it can cause issues.

mattfidler commented 1 month ago

Pull request #486 will fall back to unscaled estimation when all the parameter estimates start at zero.

While this is an option, all gradient search methods work better when there is a scaling factor.

In general the nlmixr2 scaling factor does better when it knows the relative size of the initial estimates and the relative locations. It also scales based on the functional form that is used.

mattfidler commented 1 month ago

Let me know if you need any other help with this 😄