Re: [NMusers] Simulate with ETAs from .phi and residual variability

2023-10-09 Thread Philip Harder Delff

Thank you Leonid and Andreas!

Can we have Nonmem support the combination of $ETAS and $SIMULATION 
ONLYSIM? That would enable NMsim to completely automate simulations of 
known subjects with residual variability, just like it already automates 
simulation of new subjects with residual variability.


Currently, I am using the "mixed option" mentioned by Leonid. This is 
only needed with NMsim if simulating "known" subjects (ETAs) or if the 
Nonmem model for some reason does not produce a variable with residual 
variability. See the "patch" below for examples.


*NMsim automates simulation with new or known subjects*
The reason why I am searching for a Nonmem-native method is that NMsim 
automates simulation of Nonmem models based on the estimation control 
stream and a simulation dataset. The idea is to avoid any 
reimplementation but let Nonmem run the simulation. It works nicely and 
can automatically grab ETAs from a phi file to simulate 
estimated/"known" subjects. The syntax for this feature is


```
results.sim <- NMsim(file.mod="path/to/input/control.mod",
data=data.sim,
method.sim=NMsim_known)
```

Currently, it searches for a .phi file next to the control.mod (which 
can be called control.ctl or anything) but this will be generalized in 
next version so you can pull in other subjects.


If you omit `method.sim` it will use the default method which is 
generation of new subjects (ETAs) using $SIMULATION ONLYSIM. In that 
case the residual variability is simulated too.



*NMsim's patch called addResVar*
NMsim's addResVar() grabs the parameter estimates from .ext and adds 
residual variability. The following example reproduces the residual 
variability implemented in Nonmem with SIGMA parameters where SIGMA(1,1) 
is the variance of the proportional error component, SIGMA(2,2) the one 
of the additive component (and SIGMA(1,2) the covariance). `simres` is a 
data.frame containing results from a simulation without residual 
variability (but with IPRED):


```
simres.var <- addResVar(data=simres,
path.ext = "path/to/model.ext"
  ,prop = 1
  ,add = 2
  ,par.type = "SIGMA"
  ,log = FALSE)
```

If using par.type="THETA", prop and add (obviously not both if the error 
model is not combined) have to provide the THETA element numbers in use. 
 If par.type="THETA", addResVar is by default interpreting the 
parameters as standard devs, with SIGMAs, they are variances. This can 
be modified using another argument, see help. log=TRUE means the 
variability is on log scale, i.e. the "exponential error model".


`addResVar` is a last-resort patch. Since it reimplements the residual 
error model, it is up to your QC to check that the right parameters are 
selected etc. It would be way better if we can have Nonmem do the sim, 
and we would know it is the exact same model that was used for 
simulation and for estimation.


*Further interested?*
NMsim is on CRAN. If you are interested in discussions on these issues, 
you are very welcome at https://github.com/philipdelff/NMsim/issues.


Thank you,
Philip


On 10/8/23 22:22, Leonid Gibiansky wrote:

The only way I know to simulate with residual variability is to use

$SIM SIMONLY

If ETAs need to be fixed, they have to be in the data file

$INPUT ,PKETA1,

CL=THETA(1)*EXP(PKETA1+0*ETA(1))

--
another option is to use mrgsolve or similar R packages (after reading 
in estimated ETA values).

--

Mixed option is to simulate IPRED and then add noise in the R code after 
reading IPRED.


Thank you
Leonid


On 10/8/2023 3:30 PM, Philip Harder Delff wrote:

Dear all,

I want to simulate subjects with ETAs estimated. I use the .phi file 
using a $ETAS statement like:


$ETAS FILE=file.phi FORMAT=s1pE15.8 TBLN=1

As far as I understand that has to be accompanied by a $ESTIMATION 
step that specifies FNLETA=2 like this:


$ESTIMATION MAXEVAL=0 NOABORT METHOD=1 INTERACTION FNLETA=2

$SIM ONLYSIM does not work with $ESTIMATION so instead I just do:

$SIMULATION  (220412)

This however means that in the following from $ERROR we don't get the 
ERR(1) and ERR(2) simulated and so Y=IPRED.


Y=F+F*ERR(1)+ERR(2)

I need to be able to edit the phi file to only simulate a subset of 
the patients so I can't use msf based solutions. Can I somehow use 
$ETAS without $ESTIMATION? Or how else can I get it to simulate the 
residual variability?


Thank you,
Philip





RE: [NMusers] Simulate with ETAs from .phi and residual variability

2023-10-09 Thread Andreas Lindauer
Dear Philp,

You may also want to have a look at the open-source R package campsis 
(https://calvagone.github.io/).
Below is some R-code that illustrates how you can easily simulate individual 
profiles taking the ETAs from a NONMEM output table. 

Best regards, Andreas.

---
library(tidyverse)
library(campsis)
# Load a standard model from the library
# you can also set-up a custom model using the free online simulator 
e-campsis.com)

model <- model_library$advan2_trans2


# set the parameter values
model <- model %>% replace(Theta("V", value=20))
model <- model %>% replace(Theta("CL", value=5))
model <- model %>% replace(Omega("V", value=0.1,type = 'var'))
model <- model %>% replace(Omega("CL", value=0.1,type = 'var'))
model <- model %>% replace(Sigma("PROP", value=0.02,type = 'var'))

# Have a look at the model
model

# load NONMEM output table containing the ETAS (and covariates if you have)
ETA.tab <- read_table("patab001",skip = 1) %>%
  group_by(ID) %>% slice(1) %>%   # take only the first record of each subject
  rename(ETA_CL=ETA1,ETA_V=ETA2) # you may need to rename variables to match 
with the model code
  
# set-up the simulation schedule;  the number of subject must equal that of 
ETA.tab
dataset <- Dataset(subjects = n_distinct(ETA.tab$ID)) %>%
  add(Bolus(time=0, amount=1000, ii=12, addl=2)) %>%
  add(Observations(times=seq(0, 36, by=0.5)))

# add the ETAs by using the bootstrap functionality
dataset <- dataset %>%
  add(Bootstrap(data= ETA.tab,  id="ID", replacement=FALSE, random=FALSE, 
export_id=TRUE))

# we need to discard the random IIV as the ETAs are taken from the dataset
model@parameters@list <- model@parameters@list %>% purrr::discard(.p=~is(.x, 
"omega"))

# run the simulation
results <- model %>% simulate(dataset=dataset,outvars = c('BS_ID')) # BS_ID is 
the original ID in the ETA.tab

# plot the results
spaghettiPlot(results,'OBS_CP')



-Original Message-
From: owner-nmus...@globomaxnm.com  On Behalf Of 
Philip Harder Delff
Sent: Sunday, October 8, 2023 9:30 PM
To: nmusers@globomaxnm.com
Subject: [NMusers] Simulate with ETAs from .phi and residual variability

Dear all,

I want to simulate subjects with ETAs estimated. I use the .phi file using a 
$ETAS statement like:

$ETAS FILE=file.phi FORMAT=s1pE15.8 TBLN=1

As far as I understand that has to be accompanied by a $ESTIMATION step that 
specifies FNLETA=2 like this:

$ESTIMATION MAXEVAL=0 NOABORT METHOD=1 INTERACTION FNLETA=2

$SIM ONLYSIM does not work with $ESTIMATION so instead I just do:

$SIMULATION  (220412)

This however means that in the following from $ERROR we don't get the
ERR(1) and ERR(2) simulated and so Y=IPRED.

Y=F+F*ERR(1)+ERR(2)

I need to be able to edit the phi file to only simulate a subset of the 
patients so I can't use msf based solutions. Can I somehow use $ETAS without 
$ESTIMATION? Or how else can I get it to simulate the residual variability?

Thank you,
Philip




Re: [NMusers] Simulate with ETAs from .phi and residual variability

2023-10-08 Thread Leonid Gibiansky

The only way I know to simulate with residual variability is to use

$SIM SIMONLY

If ETAs need to be fixed, they have to be in the data file

$INPUT ,PKETA1,

CL=THETA(1)*EXP(PKETA1+0*ETA(1))

--
another option is to use mrgsolve or similar R packages (after reading 
in estimated ETA values).

--

Mixed option is to simulate IPRED and then add noise in the R code after 
reading IPRED.


Thank you
Leonid


On 10/8/2023 3:30 PM, Philip Harder Delff wrote:

Dear all,

I want to simulate subjects with ETAs estimated. I use the .phi file 
using a $ETAS statement like:


$ETAS FILE=file.phi FORMAT=s1pE15.8 TBLN=1

As far as I understand that has to be accompanied by a $ESTIMATION step 
that specifies FNLETA=2 like this:


$ESTIMATION MAXEVAL=0 NOABORT METHOD=1 INTERACTION FNLETA=2

$SIM ONLYSIM does not work with $ESTIMATION so instead I just do:

$SIMULATION  (220412)

This however means that in the following from $ERROR we don't get the 
ERR(1) and ERR(2) simulated and so Y=IPRED.


Y=F+F*ERR(1)+ERR(2)

I need to be able to edit the phi file to only simulate a subset of the 
patients so I can't use msf based solutions. Can I somehow use $ETAS 
without $ESTIMATION? Or how else can I get it to simulate the residual 
variability?


Thank you,
Philip





[NMusers] Simulate with ETAs from .phi and residual variability

2023-10-08 Thread Philip Harder Delff

Dear all,

I want to simulate subjects with ETAs estimated. I use the .phi file 
using a $ETAS statement like:


$ETAS FILE=file.phi FORMAT=s1pE15.8 TBLN=1

As far as I understand that has to be accompanied by a $ESTIMATION step 
that specifies FNLETA=2 like this:


$ESTIMATION MAXEVAL=0 NOABORT METHOD=1 INTERACTION FNLETA=2

$SIM ONLYSIM does not work with $ESTIMATION so instead I just do:

$SIMULATION  (220412)

This however means that in the following from $ERROR we don't get the 
ERR(1) and ERR(2) simulated and so Y=IPRED.


Y=F+F*ERR(1)+ERR(2)

I need to be able to edit the phi file to only simulate a subset of the 
patients so I can't use msf based solutions. Can I somehow use $ETAS 
without $ESTIMATION? Or how else can I get it to simulate the residual 
variability?


Thank you,
Philip