nlmixr2 / nlmixr2plot

Plotting support for nlmixr2
https://nlmixr2.github.io/nlmixr2plot/
GNU General Public License v3.0
2 stars 0 forks source link

`plot(fit)` only shows trace plots with no IIV #18

Closed billdenney closed 1 year ago

billdenney commented 1 year ago

I have a model with no IIV, and it only shows trace plots:

library(nlmixr2)
#> Loading required package: nlmixr2data
library(nlmixr2lib)
fit <- nlmixr2(object = nlmixr2lib::readModelDb("PK_1cmt"), data = theo_sd, est = "focei", control = list(print=0))
#> rxode2 2.0.11.9000 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`
#> calculating covariance matrix
#> done
#> → Calculating residuals/tables
#> ✔ done
#> → compress origData in nlmixr2 object, save 5952
#> → compress parHist in nlmixr2 object, save 1472
plot(fit)

Created on 2022-11-14 with reprex v2.0.2

I'll work on a PR to show more of the expected plots ASAP (as I need it for a current project).

billdenney commented 1 year ago

I found the fix to this in an unexpected place. The following line is the issue:

https://github.com/nlmixr2/nlmixr2plot/blob/eed98e31aac55afc1c8a77afc4beb02eb737913f/R/plot.nlmixr2.R#L9

Specifically, the RES column does not exist in data without IIV. Is there a reason that column does not exist? And, would it be okay to switch to IRES which does exist in the fit without IIV?

one.cmt <- function() {
  ini({
    tka <- 0.45
    tcl <- log(c(0, 2.7, 100))
    tv <- 3.45
    eta.ka ~ 0.6
    eta.cl ~ 0.3
    eta.v ~ 0.1
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    linCmt() ~ add(add.sd)
  })
}

censData <- nlmixr2data::theo_md
# Assign CENS = 1 for bloq values, otherwise CENS = 0.
censData$CENS[censData$DV < 3 & censData$AMT == 0] <- 1
censData$CENS[censData$DV >= 3 & censData$AMT == 0] <- 0
# Set DV to LOQ for all censored items
censData$DV[censData$CENS == 1] <-  3

suppressMessages(
  fit <-
    nlmixr2est::nlmixr(
      one.cmt, censData,
      est="focei",
      control = nlmixr2est::foceiControl(print = 0, eval.max = 10),
      table=nlmixr2est::tableControl(npde=TRUE)
    )
)
#> calculating covariance matrix
#> done
oneCmtNoIiv <- function() {
  ini({
    tka <- 0.45
    tcl <- log(c(0, 2.7, 100))
    tv <- 3.45
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka)
    cl <- exp(tcl)
    v <- exp(tv)
    linCmt() ~ add(add.sd)
  })
}

suppressMessages(
  fitNoIiv <-
    nlmixr2est::nlmixr(
      object = oneCmtNoIiv,
      data = nlmixr2data::theo_sd,
      est = "focei",
      control = nlmixr2est::foceiControl(print = 0, eval.max = 10),
      table = nlmixr2est::tableControl(npde=TRUE)
    )
)
#> calculating covariance matrix
#> done

head(as.data.frame(fit))
#>   ID TIME         DV    EPRED        ERES        NPDE         NPD       PDE
#> 1  1 0.00  0.1445204 0.132864  0.01165636 -0.42157764  0.07526986 0.3366667
#> 2  1 0.25  2.7652569 3.634110 -0.86885330  0.27931903 -0.44913630 0.6100000
#> 3  1 0.57  6.5700000 6.054060  0.51593996 -0.03342793  0.18486703 0.4866667
#> 4  1 1.12 10.5000000 7.877470  2.62253026  2.05374891  1.24445179 0.9800000
#> 5  1 2.02  9.6600000 8.305465  1.35453455 -0.24472849  0.71706607 0.4033333
#> 6  1 3.82  8.5800000 7.596986  0.98301420 -1.26278442  0.61281299 0.1033333
#>          PD     PRED        RES       WRES         IPRED       IRES      IWRES
#> 1 0.5300000 0.000000  0.1445204  0.1479446 -2.024677e-15  0.1445204  0.1479446
#> 2 0.3266667 3.159832 -0.3945746 -0.2282705  3.483899e+00 -0.7186425 -0.7356699
#> 3 0.5733333 5.721944  0.8480563  0.3700926  6.338025e+00  0.2319747  0.2374711
#> 4 0.8933333 7.849927  2.6500731  1.1999191  8.773369e+00  1.7266312  1.7675419
#> 5 0.7633333 8.608658  1.0513417  0.5989205  9.785230e+00 -0.1252303 -0.1281975
#> 6 0.7300000 7.726710  0.8532904  0.5608454  9.125388e+00 -0.5453876 -0.5583100
#>           CPRED       CRES      CWRES CENS lowerLim upperLim       eta.ka
#> 1 -1.832686e-15  0.1445204  0.1479446    1     -Inf        3 -0.001845718
#> 2  3.145132e+00 -0.3798747 -0.2050862    1     -Inf        3 -0.001845718
#> 3  5.696001e+00  0.8739990  0.3493027    0       NA       NA -0.001845718
#> 4  7.815893e+00  2.6841074  1.0984605    0       NA       NA -0.001845718
#> 5  8.573258e+00  1.0867417  0.5528155    0       NA       NA -0.001845718
#> 6  7.691778e+00  0.8882219  0.5254449    0       NA       NA -0.001845718
#>       eta.cl      eta.v      ka       cl       v  tad dosenum
#> 1 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 0.00       1
#> 2 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 0.25       1
#> 3 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 0.57       1
#> 4 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 1.12       1
#> 5 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 2.02       1
#> 6 -0.3757471 -0.0961874 1.47558 1.955198 28.0753 3.82       1
head(as.data.frame(fitNoIiv))
#>   ID TIME    DV    IPRED       IRES      IWRES      ka       cl        v  tad
#> 1  1 0.00  0.74 0.000000  0.7400000  0.5620486 1.56281 2.542398 31.84017 0.00
#> 2  1 0.25  2.84 3.216025 -0.3760252 -0.2856006 1.56281 2.542398 31.84017 0.25
#> 3  1 0.57  6.57 5.774039  0.7959609  0.6045523 1.56281 2.542398 31.84017 0.57
#> 4  1 1.12 10.50 7.845212  2.6547875  2.0163778 1.56281 2.542398 31.84017 1.12
#> 5  1 2.02  9.66 8.562704  1.0972962  0.8334240 1.56281 2.542398 31.84017 2.02
#> 6  1 3.82  8.58 7.779702  0.8002979  0.6078463 1.56281 2.542398 31.84017 3.82
#>   dosenum
#> 1       1
#> 2       1
#> 3       1
#> 4       1
#> 5       1
#> 6       1

Created on 2022-11-14 with reprex v2.0.2