nlmixr2 / rxode2parse

1 stars 0 forks source link

Factor value that is part of `keep` is converted to numeric in the source data #78

Closed billdenney closed 9 months ago

billdenney commented 11 months ago

d_sim$TRTG should not be modified, but in the first pass where it is part of keep, it is. (My guess is that this will end up being an rxode2 issue, but I'm not sure exactly where keep has its effects at the moment, and I want to put the reprex into the record.)

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
  })
  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)
  })
}

d_sim <- theo_sd
d_sim$TRTG <- ordered(ifelse(theo_sd$WT < median(theo_sd$WT), "Low", "High"))
d_sim$TRTG
#>   [1] High High High High High High High High High High High High High High High
#>  [16] High High High High High High High High High High High High High High High
#>  [31] High High High High High High High High High High High High High High High
#>  [46] High High High Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#>  [61] High High High High High High High High High High High High Low  Low  Low 
#>  [76] Low  Low  Low  Low  Low  Low  Low  Low  Low  High High High High High High
#>  [91] High High High High High High High High High High High High High High High
#> [106] High High High Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> [121] Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> [136] Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> Levels: High < Low
fit <- nlmixr2(one.compartment, d_sim, est="rxSolve", list(keep = "TRTG"))
d_sim$TRTG
#>   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#>  [38] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2
#>  [75] 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
#> [112] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

d_sim <- theo_sd
d_sim$TRTG <- ordered(ifelse(theo_sd$WT < median(theo_sd$WT), "Low", "High"))
fit <- nlmixr2(one.compartment, d_sim, est="rxSolve")
d_sim$TRTG
#>   [1] High High High High High High High High High High High High High High High
#>  [16] High High High High High High High High High High High High High High High
#>  [31] High High High High High High High High High High High High High High High
#>  [46] High High High Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#>  [61] High High High High High High High High High High High High Low  Low  Low 
#>  [76] Low  Low  Low  Low  Low  Low  Low  Low  Low  High High High High High High
#>  [91] High High High High High High High High High High High High High High High
#> [106] High High High Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> [121] Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> [136] Low  Low  Low  Low  Low  Low  Low  Low  Low 
#> Levels: High < Low

Created on 2023-09-25 with reprex v2.0.2

billdenney commented 11 months ago

With a quick additional test, it seems like this only has an effect when nStud = 1 (the default) and not when nStud = 1000.

mattfidler commented 9 months ago

d_sim$TRTG will be modified since there are dosing records that will be inserted and interpolations that will be performed.

But there should be no effects on the source data.

mattfidler commented 9 months ago
library(rxode2)
#> rxode2 2.0.14.9000 using 8 threads (see ?getRxThreads)
#>   no cache: create with `rxCreateCache()`

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
    })
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)
      })
}

d_sim <- nlmixr2data::theo_sd
d_sim$TRTG <- ordered(ifelse(nlmixr2data::theo_sd$WT < median(nlmixr2data::theo_sd$WT), "Low", "High"))
summary(d_sim$TRTG)
#> High  Low 
#>   84   60

fit <- rxSolve(one.compartment, d_sim, keep="TRTG")

summary(d_sim$TRTG)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   1.000   1.000   1.000   1.417   2.000   2.000

Created on 2023-12-04 with reprex v2.0.2

mattfidler commented 9 months ago

This is because your factor is an ordered factor. I'm unsure if I will allow them to be kept or not. I can't support every different type of data since it actually isn't kept and merged at the end.

mattfidler commented 9 months ago

We already tried an approach where we merged back the data and it didn't successfully run (I remember you tried it).

mattfidler commented 9 months ago

So currently in keep:

Any sort of attributes will be dropped (maybe should have some sort of warning but would need to explicitly check for attributes).

Could additionally support ordered factors