nlmixr2 / nlmixr2est

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

"posthoc" estimation method does not verify that IDs are the same #434

Closed billdenney closed 6 months ago

billdenney commented 6 months ago

In the example below, the IDs changed, so I think that IPRED should either be NA (my preference) or the same as PRED.

library(nlmixr2est)
#> Loading required package: nlmixr2data

one.compartment <- function() {
  ini({
    tka <- 0.45; label("Ka")
    tcl <- 1; label("Cl")
    tv <- 3.45; label("V")
    eta.ka ~ 0.6
    eta.cl ~ 0.3
    eta.v ~ 0.1
    add.sd <- 0.7
  })
  # and a model block with the error specification and model specification
  model({
    ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    d/dt(depot) = -ka * depot
    d/dt(center) = ka * depot - cl / v * center
    cp = center / v
    cp ~ add(add.sd)
  })
}

theo_sd_letterid <- theo_sd
theo_sd_letterid$ID <- LETTERS[theo_sd_letterid$ID]
fit_letterid <- nlmixr2(one.compartment, theo_sd_letterid,  est="saem", saemControl(print=0))
#> → loading into symengine environment...
#> → pruning branches (`if`/`else`) of saem model...
#> ✔ done
#> → finding duplicate expressions in saem model...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> → optimizing duplicate expressions in saem model...
#> [====|====|====|====|====|====|====|====|====|====] 0:00:00
#> ✔ done
#> rxode2 2.1.2 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
#> Calculating covariance matrix
#> → loading into symengine environment...
#> → pruning branches (`if`/`else`) of saem model...
#> ✔ done
#> → finding duplicate expressions in saem predOnly model 0...
#> → finding duplicate expressions in saem predOnly model 1...
#> → optimizing duplicate expressions in saem predOnly model 1...
#> → finding duplicate expressions in saem predOnly model 2...
#> ✔ done
#> → Calculating residuals/tables
#> ✔ done
#> → compress origData in nlmixr2 object, save 7192
#> → compress phiM in nlmixr2 object, save 62360
#> → compress parHistData in nlmixr2 object, save 13864
#> → compress saem0 in nlmixr2 object, save 31584

prediction_letterid_newid <- nlmixr2(fit_letterid, theo_sd, est="posthoc")
#> ℹ use `control` from prior fit
#> ℹ invalid control for `est="posthoc"`, using default
#> → Calculating residuals/tables
#> ✔ done
#> → compress origData in nlmixr2 object, save 5952
# New IDs were provided; IPRED should either be NA or the same as PRED
head(as.data.frame(prediction_letterid_newid))
#>   ID TIME    DV     PRED        RES       WRES    IPRED       IRES      IWRES
#> 1  1 0.00  0.74 0.000000  0.7400000  1.0681064 0.000000  0.7400000  1.0681064
#> 2  1 0.25  2.84 3.266453 -0.4264533 -0.2249325 3.845996 -1.0059960 -1.4520415
#> 3  1 0.57  6.57 5.847208  0.7227924  0.2862476 6.784842 -0.2148425 -0.3101009
#> 4  1 1.12 10.50 7.905784  2.5942157  1.1995747 9.042419  1.4575810  2.1038535
#> 5  1 2.02  9.66 8.563506  1.0964937  0.7948877 9.783637 -0.1236373 -0.1784565
#> 6  1 3.82  8.58 7.669236  0.9107642  0.7720335 9.095485 -0.5154850 -0.7440443
#>      CPRED       CRES      CWRES     eta.ka     eta.cl       eta.v       depot
#> 1 0.000000  0.7400000  1.0681064 0.09705776 -0.4815768 -0.08115571 319.9920000
#> 2 3.223800 -0.3838003 -0.1783515 0.09705776 -0.4815768 -0.08115571 207.3929577
#> 3 5.797588  0.7724123  0.2778124 0.09705776 -0.4815768 -0.08115571 119.0454030
#> 4 7.876910  2.6230899  1.1515404 0.09705776 -0.4815768 -0.08115571  45.8514784
#> 5 8.562504  1.0974957  0.7320675 0.09705776 -0.4815768 -0.08115571   9.6230067
#> 6 7.687693  0.8923071  0.6885349 0.09705776 -0.4815768 -0.08115571   0.4238633
#>     center       ka      cl        v  tad dosenum
#> 1   0.0000 1.734722 1.70609 29.04768 0.00       1
#> 2 111.7173 1.734722 1.70609 29.04768 0.25       1
#> 3 197.0840 1.734722 1.70609 29.04768 0.57       1
#> 4 262.6613 1.734722 1.70609 29.04768 1.12       1
#> 5 284.1920 1.734722 1.70609 29.04768 2.02       1
#> 6 264.2028 1.734722 1.70609 29.04768 3.82       1

Created on 2024-04-06 with reprex v2.1.0

mattfidler commented 6 months ago

I really don't understand what you mean here. Though the control shouldn't pick up from the last estimation method used. That is a bug.

mattfidler commented 6 months ago

Ah. It should detect that it is a string. My guess is that it is not returning the string values even when it is not changed.

billdenney commented 6 months ago

I didn't realize that posthoc also did estimation. Since it does some degree of estimation, I think that my question is moot. I thought that it only did estimation in which case the ID values should be required to be the same.

mattfidler commented 6 months ago

Ok. You are probably looking for predict()