Closed ZhonghuiHuang closed 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
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
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.
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.
Let me know if you need any other help with this 😄
@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
Run 2. Set all of initial estimates as 1.01
Run 3. Set all of initial estimates as 1.01
Run 4. Pheno case with initials set as 1.
Run 5. Pheno case by initials set as 1.01
}