nlmixr2 / rxode2

rxode2
https://nlmixr2.github.io/rxode2/
GNU General Public License v3.0
32 stars 7 forks source link

Uninitialized subject when data mixes time = 0 and time != 0 #339

Closed billdenney closed 2 years ago

billdenney commented 2 years ago

In the example below, the first subject in simfit has no simulated results because the time 0 result is removed. (I think that this is not an issue when the first time is not zero for both subjects.)

library(tidyverse)
library(nlmixr2)
#> Loading required package: nlmixr2data
oncology_sdm_lobo_2002 <- function() {
  description <- "Signal transduction model for delayed concentration effects on cancer cell growth"
  reference <- "Lobo ED, Balthasar JP. Pharmacodynamic modeling of chemotherapeutic effects: Application of a transit compartment model to characterize methotrexate effects in vitro. AAPS J. 2002;4(4):212-222. doi:10.1208/ps040442"
  # Values for lkng, ltau, lec50, and kmax are for methotrexate from Lobo 2002,
  # Table 2.  propErr and addErr are added as reasonable values though not from
  # Lobo 2002 where no value is apparent in the paper.
  ini({
    lkng <- log(0.02) ; label("Cell net growth rate (growth minus death) (1/hr)")
    ltau <- log(c(1, 34.1, 500)) ; label("Mean transit time of each transit compartment (hr)")
    lec50 <- log(c(1, 50, 2000)) ; label("Drug concentration reducing the cell growth by 50% (ug/mL)")
    kmax <- 0.01 ; label("Maximum drug-related reduction in cell growth (1/hr)")

    propErr <- c(0, 0.3) ; label("Proportional residual error (fraction)")
    addErr <- c(0, 50, 1000) ; label("Additive residual error (tumor volume units)")
  })
  model({
    # cp is the drug concentration
    kng <- exp(lkng)
    tau <- exp(ltau)
    taulast <- tau
    ec50 <- exp(lec50)

    edrug <- kmax*cp/(ec50 + cp)

    tumor(0) <- tumor0
    d/dt(transit1) <- (edrug - transit1)/tau
    d/dt(transit2) <- (transit1 - transit2)/tau
    d/dt(transit3) <- (transit2 - transit3)/tau
    d/dt(transitlast) <- transit3/tau - transitlast/taulast
    d/dt(tumor) <- kng*tumor - transitlast*tumor
    tumor ~ prop(propErr) + add(addErr)
  })
}

d_sim <-
  data.frame(
    tumor0 = 100,
    time = 0:24,
    cmt = "tumor",
    cp=pmxTools::calc_sd_1cmt_linear_bolus(t = 0:24, dose = 1, CL = 1, V = 1)
  ) %>%
  crossing(
    ID = 1:2
  )

simulated <- nlmixr2(oncology_sdm_lobo_2002, data = d_sim, est = "rxSolve")
prepfit <-
  as.data.frame(simulated) %>%
  filter(!(id == 1 & time == 0)) %>%
  rename(
    DV=ipredSim
  )

simfit <- nlmixr2(oncology_sdm_lobo_2002, data= prepfit, est = "focei", control = foceiControl(print = 0))
#> rxode2 2.0.9 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
#> calculating covariance matrix
#> done
#> → Calculating residuals/tables
#> DLSODA-  At start of problem, too much accuracy   
#>       requested for precision of machine..  See TOLSF (=R1) 
#> exit of dop853 at x = 0.0000000000000000e+00, more than nmax = 1000000 are needed
#> ✔ done
#> → compress origData in nlmixr2 object, save 6624
#> → compress parHist in nlmixr2 object, save 5000
as.data.frame(simfit)
#>    ID TIME       DV IPRED     IRES    IWRES      transit1      transit2
#> 1   1    1 102.0202    NA       NA       NA            NA            NA
#> 2   1    2 104.0811    NA       NA       NA            NA            NA
#> 3   1    3 106.1837    NA       NA       NA            NA            NA
#> 4   1    4 108.3287    NA       NA       NA            NA            NA
#> 5   1    5 110.5171    NA       NA       NA            NA            NA
#> 6   1    6 112.7497    NA       NA       NA            NA            NA
#> 7   1    7 115.0274    NA       NA       NA            NA            NA
#> 8   1    8 117.3512    NA       NA       NA            NA            NA
#> 9   1    9 119.7218    NA       NA       NA            NA            NA
#> 10  1   10 122.1405    NA       NA       NA            NA            NA
#> 11  1   11 124.6078    NA       NA       NA            NA            NA
#> 12  1   12 127.1251    NA       NA       NA            NA            NA
#> 13  1   13 129.6932    NA       NA       NA            NA            NA
#> 14  1   14 132.3132    NA       NA       NA            NA            NA
#> 15  1   15 134.9860    NA       NA       NA            NA            NA
#> 16  1   16 137.7129    NA       NA       NA            NA            NA
#> 17  1   17 140.4949    NA       NA       NA            NA            NA
#> 18  1   18 143.3331    NA       NA       NA            NA            NA
#> 19  1   19 146.2286    NA       NA       NA            NA            NA
#> 20  1   20 149.1826    NA       NA       NA            NA            NA
#> 21  1   21 152.1962    NA       NA       NA            NA            NA
#> 22  1   22 155.2708    NA       NA       NA            NA            NA
#> 23  1   23 158.4074    NA       NA       NA            NA            NA
#> 24  1   24 161.6074    NA       NA       NA            NA            NA
#> 25  2    0 100.0000   100   0.0000   0.0000  0.000000e+00  0.000000e+00
#> 26  2    1 102.0202     0 102.0202 388.6971 -1.193863e-02 -1.831705e-04
#> 27  2    2 104.0811     0 104.0811 396.5493 -4.442670e-03 -6.828019e-05
#> 28  2    3 106.1837     0 106.1837 404.5602 -1.642391e-03 -2.524229e-05
#> 29  2    4 108.3287     0 108.3287 412.7328 -6.052952e-04 -9.302933e-06
#> 30  2    5 110.5171     0 110.5171 421.0706 -2.228240e-04 -3.424639e-06
#> 31  2    6 112.7497     0 112.7497 429.5768 -8.199245e-05 -1.260163e-06
#> 32  2    7 115.0274     0 115.0274 438.2548 -3.016606e-05 -4.636300e-07
#> 33  2    8 117.3512     0 117.3512 447.1083 -1.109784e-05 -1.705656e-07
#> 34  2    9 119.7218     0 119.7218 456.1404 -4.082717e-06 -6.274835e-08
#> 35  2   10 122.1405     0 122.1405 465.3556 -1.501954e-06 -2.308393e-08
#> 36  2   11 124.6078     0 124.6078 474.7562 -5.525390e-07 -8.492117e-09
#> 37  2   12 127.1251     0 127.1251 484.3469 -2.032679e-07 -3.124077e-09
#> 38  2   13 129.6932     0 129.6932 494.1314 -7.477809e-08 -1.149284e-09
#> 39  2   14 132.3132     0 132.3132 504.1135 -2.750932e-08 -4.227980e-10
#> 40  2   15 134.9860     0 134.9860 514.2972 -1.012012e-08 -1.555387e-10
#> 41  2   16 137.7129     0 137.7129 524.6867 -3.722982e-09 -5.721949e-11
#> 42  2   17 140.4949     0 140.4949 535.2860 -1.369609e-09 -2.104987e-11
#> 43  2   18 143.3331     0 143.3331 546.0994 -5.038509e-10 -7.743816e-12
#> 44  2   19 146.2286     0 146.2286 557.1313 -1.853564e-10 -2.848791e-12
#> 45  2   20 149.1826     0 149.1826 568.3860 -6.818880e-11 -1.048012e-12
#> 46  2   21 152.1962     0 152.1962 579.8681 -2.508526e-11 -3.855419e-13
#> 47  2   22 155.2708     0 155.2708 591.5821 -9.228351e-12 -1.418329e-13
#> 48  2   23 158.4074     0 158.4074 603.5327 -3.394920e-12 -5.217742e-14
#> 49  2   24 161.6074     0 161.6074 615.7248 -1.248921e-12 -1.919500e-14
#>         transit3   transitlast tumor        kng      tau  taulast     ec50
#> 1             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 2             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 3             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 4             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 5             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 6             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 7             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 8             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 9             NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 10            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 11            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 12            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 13            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 14            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 15            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 16            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 17            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 18            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 19            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 20            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 21            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 22            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 23            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 24            NA            NA    NA 0.01990729 32.42753 32.42753 47.19696
#> 25  0.000000e+00  0.000000e+00   100 0.01990729 32.42753 32.42753 47.19696
#> 26 -1.878022e-06 -1.445623e-08     0 0.01990729 32.42753 32.42753 47.19696
#> 27 -7.000674e-07 -5.388827e-09     0 0.01990729 32.42753 32.42753 47.19696
#> 28 -2.588057e-07 -1.992178e-09     0 0.01990729 32.42753 32.42753 47.19696
#> 29 -9.538170e-08 -7.342084e-10     0 0.01990729 32.42753 32.42753 47.19696
#> 30 -3.511235e-08 -2.702802e-10     0 0.01990729 32.42753 32.42753 47.19696
#> 31 -1.292028e-08 -9.945492e-11     0 0.01990729 32.42753 32.42753 47.19696
#> 32 -4.753535e-09 -3.659072e-11     0 0.01990729 32.42753 32.42753 47.19696
#> 33 -1.748786e-09 -1.346142e-11     0 0.01990729 32.42753 32.42753 47.19696
#> 34 -6.433502e-10 -4.952241e-12     0 0.01990729 32.42753 32.42753 47.19696
#> 35 -2.366764e-10 -1.821836e-12     0 0.01990729 32.42753 32.42753 47.19696
#> 36 -8.706851e-11 -6.702170e-13     0 0.01990729 32.42753 32.42753 47.19696
#> 37 -3.203074e-11 -2.465592e-13     0 0.01990729 32.42753 32.42753 47.19696
#> 38 -1.178345e-11 -9.070409e-14     0 0.01990729 32.42753 32.42753 47.19696
#> 39 -4.334890e-12 -3.336817e-14     0 0.01990729 32.42753 32.42753 47.19696
#> 40 -1.594717e-12 -1.227546e-14     0 0.01990729 32.42753 32.42753 47.19696
#> 41 -5.866636e-13 -4.515891e-15     0 0.01990729 32.42753 32.42753 47.19696
#> 42 -2.158215e-13 -1.661304e-15     0 0.01990729 32.42753 32.42753 47.19696
#> 43 -7.939628e-14 -6.111594e-16     0 0.01990729 32.42753 32.42753 47.19696
#> 44 -2.920826e-14 -2.248330e-16     0 0.01990729 32.42753 32.42753 47.19696
#> 45 -1.074512e-14 -8.271143e-17     0 0.01990729 32.42753 32.42753 47.19696
#> 46 -3.952908e-15 -3.042784e-17     0 0.01990729 32.42753 32.42753 47.19696
#> 47 -1.454194e-15 -1.119378e-17     0 0.01990729 32.42753 32.42753 47.19696
#> 48 -5.349680e-16 -4.117960e-18     0 0.01990729 32.42753 32.42753 47.19696
#> 49 -1.968037e-16 -1.514913e-18     0 0.01990729 32.42753 32.42753 47.19696
#>            edrug
#> 1  -1.465782e-01
#> 2  -5.418803e-02
#> 3  -1.997076e-02
#> 4  -7.351728e-03
#> 5  -2.705213e-03
#> 6  -9.952820e-04
#> 7  -3.661560e-04
#> 8  -1.347029e-04
#> 9  -4.955465e-05
#> 10 -1.823017e-05
#> 11 -6.706507e-06
#> 12 -2.467187e-06
#> 13 -9.076274e-07
#> 14 -3.338975e-07
#> 15 -1.228340e-07
#> 16 -4.518811e-08
#> 17 -1.662378e-08
#> 18 -6.115545e-09
#> 19 -2.249783e-09
#> 20 -8.276491e-10
#> 21 -3.044751e-10
#> 22 -1.120101e-10
#> 23 -4.120622e-11
#> 24 -1.515892e-11
#> 25 -3.932152e-01
#> 26 -1.465782e-01
#> 27 -5.418803e-02
#> 28 -1.997076e-02
#> 29 -7.351728e-03
#> 30 -2.705213e-03
#> 31 -9.952820e-04
#> 32 -3.661560e-04
#> 33 -1.347029e-04
#> 34 -4.955465e-05
#> 35 -1.823017e-05
#> 36 -6.706507e-06
#> 37 -2.467187e-06
#> 38 -9.076274e-07
#> 39 -3.338975e-07
#> 40 -1.228340e-07
#> 41 -4.518811e-08
#> 42 -1.662378e-08
#> 43 -6.115545e-09
#> 44 -2.249783e-09
#> 45 -8.276491e-10
#> 46 -3.044751e-10
#> 47 -1.120101e-10
#> 48 -4.120622e-11
#> 49 -1.515892e-11

Created on 2022-10-17 with reprex v2.0.2

billdenney commented 2 years ago

My preference for the behavior would be that the user would be required to set an option to either initialize at 0 or at the first measurement. And, that setting should be an explicit choice rather than a default.

mattfidler commented 2 years ago

There is already an option, though perhaps documentation needs to be a bit more explicit. It is very explicit when the first observation is negative,

https://github.com/nlmixr2/rxode2/blob/6d440074b4eefe5adce79887739f59bf2d4932e4/src/etTran.cpp#L2045

So use:

rxSetIni0(FALSE)

To change this behavior

mattfidler commented 2 years ago

Evetually, I want to change to something like:

a(10)=3
b(4)=6

But that has been a feature request that I have been thinking about for awhile (but never created an issue for)...

mattfidler commented 2 years ago

Because the option is already present, there is likely a data translation error or there is an error for the initial problem somehow

mattfidler commented 2 years ago

Well it isn't a data translation error. It is doing the right thing (using the underlying classic rxode2 code and the prepfit data.

Note that I still can't run reprex::reprex() on wayland:


> library(rxode2)
> # from (oncology_sdm_lobo_2002())$simulationModel
+ rx <- rxode2({
+     param(lkng, ltau, lec50, kmax, propErr, addErr, cp, tumor0)
+     kng = exp(lkng)
+     tau = exp(ltau)
+     taulast = tau
+     ec50 = exp(lec50)
+     edrug = kmax * cp/(ec50 + cp)
+     tumor(0) = tumor0
+     d/dt(transit1) = (edrug - transit1)/tau
+     d/dt(transit2) = (transit1 - transit2)/tau
+     d/dt(transit3) = (transit2 - transit3)/tau
+     d/dt(transitlast) = transit3/tau - transitlast/taulast
+     d/dt(tumor) = kng * tumor - transitlast * tumor
+     rx_yj_ ~ 2
+     rx_lambda_ ~ 1
+     rx_low_ ~ 0
+     rx_hi_ ~ 1
+     rx_pred_f_ ~ tumor
+     rx_pred_ ~ rx_pred_f_
+     rx_r_ ~ (addErr)^2 + (rx_pred_f_)^2 * (propErr)^2
+     ipredSim = rxTBSi(rx_pred_, rx_lambda_, rx_yj_, rx_low_, 
+         rx_hi_)
+     sim = rxTBSi(rx_pred_ + sqrt(rx_r_) * err.tumor, rx_lambda_, 
+         rx_yj_, rx_low_, rx_hi_)
+     dvid(5)
+ })
+ > # from qs::base91_encode(qs::qserialize(d_sim))
+ prepfit <- qs::qdeserialize(qs::base91_decode("un]\"BAAA@QRtHACAAAAAAAuWeBAABdk1kus^^d8Ah9}?=Z:alBMc4Iv(F\":C?hVBAMZRxwFfBB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Z]*PByzRza9ik5LJ;}@p;JH}+.,1om5gxAkHdn%`#kbKMJuiDOa#*M!h.2dUje501>,MIW$207SwS$M~FCMaG:`&l?j<c+ZMyFt+u:CpBWOZL)$@WywmY|LCGPdVv=>K]e\"!>HG]Tw0Xbm7SOIjt.B%cH5Ewy_YgfI6(#$I]1Q4Jte1:^W%XdqR4C#OV#._I`G$k|#>NfxcZp!Y]YnX~yZ0nx),%jfE\"u7yS,:y&oJV`;4nMg0g[a]Tidjt|y[*~_8H;*X61T/1kv9x$f/3N?~>xL$mO^0Nk6W|U[wD"))
> head(prepfit) # time 0 is missing here
  id time  kng  tau taulast ec50        edrug       DV        sim     transit1
1  1    1 0.02 34.1    34.1   50 7.303850e-05 102.0202  65.673434 5.666220e-06
2  1    2 0.02 34.1    34.1   50 2.699399e-05 104.0811 104.740533 7.603913e-06
3  1    3 0.02 34.1    34.1   50 9.947509e-06 106.1837  37.562447 8.164464e-06
4  1    4 0.02 34.1    34.1   50 3.661786e-06 108.3287   3.156474 8.222402e-06
5  1    5 0.02 34.1    34.1   50 1.347408e-06 110.5171  47.265125 8.080313e-06
6  1    6 0.02 34.1    34.1   50 4.957259e-07 112.7497 117.696059 7.880630e-06
      transit2     transit3  transitlast    tumor          cp tumor0
1 8.225240e-08 1.011216e-09 1.243437e-11 102.0202 0.367879441    100
2 2.727191e-07 6.000752e-09 1.006670e-10 104.0811 0.135335283    100
3 4.928050e-07 1.688075e-08 4.147003e-10 106.1837 0.049787068    100
4 7.159394e-07 3.388743e-08 1.123073e-09 108.3287 0.018315639    100
5 9.308434e-07 5.674403e-08 2.388233e-09 110.5171 0.006737947    100
6 1.134605e-06 8.498855e-08 4.356776e-09 112.7497 0.002478752    100
> head(etTrans(prepfit, rx)) # time 0 is added back here with internal evid=9, the evid to initialize to zero.
  ID TIME EVID AMT II       DV          cp
1  1    0    9  NA  0       NA          NA
2  1    1    0  NA  0 102.0202 0.367879441
3  1    2    0  NA  0 104.0811 0.135335283
4  1    3    0  NA  0 106.1837 0.049787068
5  1    4    0  NA  0 108.3287 0.018315639
6  1    5    0  NA  0 110.5171 0.006737947

Covariates (non time-varying):
  ID tumor0
1  1    100
2  2    100

Compartment translation:
[1] Compartment Number
<0 rows> (or 0-length row.names)
> 
mattfidler commented 2 years ago

Note that turning off the ini0, the model runs fine:

> library(tidyverse)
+ library(nlmixr2)
── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
> Loading required package: nlmixr2data
> . + . + > d_sim <-
+   data.frame(
+     tumor0 = 100,
+     time = 0:24,
+     cmt = "tumor",
+     cp=pmxTools::calc_sd_1cmt_linear_bolus(t = 0:24, dose = 1, CL = 1, V = 1)
+   ) %>%
+   crossing(
+     ID = 1:2
+   )
+ > simulated <- nlmixr2(oncology_sdm_lobo_2002, data = d_sim, est = "rxSolve")
+ prepfit <-
+   as.data.frame(simulated) %>%
+   filter(!(id == 1 & time == 0)) %>%
+   rename(
+     DV=ipredSim
+   )
+ > rxode2::rxSetIni0(FALSE)
[1] FALSE
> simfit <- nlmixr2(oncology_sdm_lobo_2002, data= prepfit, est = "focei", control = foceiControl(print = 0))
rxode2 2.0.9 using 4 threads (see ?getRxThreads)
calculating covariance matrix
[====|====|====|====|====|====|====|====|====|====] 100%; 0:00:00 done
→ Calculating residuals/tables
✔ done
→ compress origData in nlmixr2 object, save 6624
→ compress parHist in nlmixr2 object, save 5296
> simfit
── nlmixr² Population Only (outer: nlminb) ──

        OBJF      AIC      BIC Log-likelihood Condition Number
Pop 62.79619 164.8522 176.2031      -76.42608         80745.15

── Time (sec simfit$time): ──

           setup optimize covariance table compress    other
elapsed 0.016787 0.006418    0.00642 0.021    0.011 0.197375

── (simfit$parFixed or simfit$parFixedDf): ──

                                                         Parameter   Est.
lkng              Cell net growth rate (growth minus death) (1/hr)  -3.88
ltau            Mean transit time of each transit compartment (hr)   3.56
lec50   Drug concentration reducing the cell growth by 50% (ug/mL)   3.92
kmax          Maximum drug-related reduction in cell growth (1/hr)    -19
propErr                     Proportional residual error (fraction) 0.0115
addErr                Additive residual error (tumor volume units) 0.0973
            SE  %RSE Back-transformed(95%CI) BSV(SD) Shrink(SD)%
lkng    0.0137 0.353 0.0206 (0.0201, 0.0212)                    
ltau      0.27  7.58       35.2 (20.8, 59.8)                    
lec50    0.257  6.55       50.2 (30.4, 83.1)                    
kmax      3.87  20.4      -19 (-26.5, -11.4)                    
propErr                               0.0115                    
addErr                                0.0973                    

  Covariance Type (simfit$covMethod): |r|
  Information about run found (simfit$runInfo):
   • IDs without zero-time start at the first observed time: 1 
   • gradient problems with initial estimate and covariance; see $scaleInfo 
   • R matrix non-positive definite but corrected by R = sqrtm(R%*%R) 
  Censoring (simfit$censInformation): No censoring
  Minimization message (simfit$message):  
    false convergence (8) 
  In an ODE system, false convergence may mean "useless" evaluations were performed.
  See https://tinyurl.com/yyrrwkce
  It could also mean the convergence is poor, check results before accepting fit
  You may also try a good derivative free optimization:
    nlmixr2(...,control=list(outerOpt="bobyqa"))

── Fit Data (object simfit is a modified tibble): ──
# A tibble: 49 × 16
  ID     TIME    DV IPRED  IRES IWRES transit1  transit2 transit3 transi…¹ tumor
  <fct> <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl>     <dbl>    <dbl>    <dbl> <dbl>
1 1         1  102.  100   2.02  1.76  0         0        0        0        100 
2 1         2  104.  102.  2.00  1.70 -0.00386  -5.45e-5 -5.27e-7 -3.98e-9  102.
3 1         3  106.  104.  1.98  1.65 -0.00517  -1.80e-4 -3.72e-6 -5.56e-8  104.
# … with 46 more rows, 5 more variables: kng <dbl>, tau <dbl>, taulast <dbl>,
#   ec50 <dbl>, edrug <dbl>, and abbreviated variable name ¹​transitlast
# ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
> 
mattfidler commented 2 years ago

For me when I change the option I get:

> simfit <- nlmixr2(oncology_sdm_lobo_2002, data= prepfit, est = "focei", control = foceiControl(print = 0))
calculating covariance matrix
[====|====|====|====|====|====|====|====|====|====] 100%; 0:00:00 done
→ Calculating residuals/tables
DLSODA-  At start of problem, too much accuracy   
      requested for precision of machine..  See TOLSF (=R1) 
IDID=-3, illegal input detected (see printed message).
exit of dop853 at x = 0.0000000000000000e+00, more than nmax = 1000000 are needed
IDID=-2, larger nmax is needed
✔ done
→ compress origData in nlmixr2 object, save 6624
→ compress parHist in nlmixr2 object, save 5008

Note that

DLSODA-  At start of problem, too much accuracy   
      requested for precision of machine..  See TOLSF (=R1) 
IDID=-3, illegal input detected (see printed message).
exit of dop853 at x = 0.0000000000000000e+00, more than nmax = 1000000 are needed

Says that this is an ODE solving issue in general at the table step, so it is related to nlmixr2/nlmixr2est#256 instead.

mattfidler commented 2 years ago

Or rather that may be the cause.

mattfidler commented 2 years ago

As suggested by the initial issue, the model had the issue when removing the subject's evid=9. My guess is that the estimation no longer treated the first data point (because it no longer is part of the data) and had different estimates, and then had problems solving the resulting ode

billdenney commented 2 years ago

FYI, all of my recent issues are related to this problem. I think that fixing the data handling around time = 0 and time != 0 will resolve my issues for all of these. And for now, I can work around it with an EVID = 2 row at the beginning.

mattfidler commented 2 years ago

Ok. I tried to reproduce it again and I couldn't. I am reinstalling the entire stack to see if I can reproduce again.

mattfidler commented 2 years ago

With evid=9

Browse[2]> etTrans(fit$dataSav, model)
   ID TIME EVID AMT II       DV           cp tumor0
1   1    0    9  NA  0       NA           NA     NA
2   1    1    0  NA  0 102.0202 3.678794e-01    100
3   1    2    0  NA  0 104.0811 1.353353e-01    100
4   1    3    0  NA  0 106.1837 4.978707e-02    100
5   1    4    0  NA  0 108.3287 1.831564e-02    100
6   1    5    0  NA  0 110.5171 6.737947e-03    100

with evid=0

Browse[2]> etTrans(fit$dataSav, model)
   ID TIME EVID AMT II       DV           cp
1   1    0    0  NA  0 100.0000 1.000000e+00
2   1    1    0  NA  0 102.0202 3.678794e-01
3   1    2    0  NA  0 104.0811 1.353353e-01
4   1    3    0  NA  0 106.1837 4.978707e-02
5   1    4    0  NA  0 108.3287 1.831564e-02
6   1    5    0  NA  0 110.5171 6.737947e-03
7   1    6    0  NA  0 112.7497 2.478752e-03
8   1    7    0  NA  0 115.0274 9.118820e-04
9   1    8    0  NA  0 117.3512 3.354626e-04
...

Covariates (non time-varying):
  ID tumor0
1  1    100
2  2    100
mattfidler commented 2 years ago

with evid=2

etTrans(fit$dataSav, model)
   ID TIME EVID AMT II       DV           cp
1   1    0  101   0  0       NA 1.000000e+00
2   1    0    2  NA  0       NA 1.000000e+00
3   1    1    0  NA  0 102.0202 3.678794e-01
4   1    2    0  NA  0 104.0811 1.353353e-01
5   1    3    0  NA  0 106.1837 4.978707e-02
6   1    4    0  NA  0 108.3287 1.831564e-02
7   1    5    0  NA  0 110.5171 6.737947e-03
8   1    6    0  NA  0 112.7497 2.478752e-03
9   1    7 
...
Covariates (non time-varying):
  ID tumor0
1  1    100
2  2    100

Compartment translation:
[1] Compartment Number
<0 rows> (or 0-length row.names)
mattfidler commented 2 years ago

Which means imputed variables need to be considered before observed.

mattfidler commented 2 years ago

They are already imputed. The only issue is time invariant covariates are not considered correctly with NA values