Replies: 9 comments 4 replies
-
HI @tmscs - You should be able to pass both. But another option is to put the individual parameters / covariates on the data set. See two options below: library(mrgsolve)
#>
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#>
#> filter
library(tidyverse)
n <- 30
data <- as_data_set(
ev(ID = seq(n), amt = 100, ii = 24, addl = 9),
ev(ID = seq(n), amt = 300, ii = 24, addl = 9)
) %>% mutate(dose = amt)
idata <- tibble(CL = runif(nrow(data), 0.5, 1.5), ID = seq(length(CL)))
mod <- house() Option 1:Pass both mod %>%
data_set(data) %>%
idata_set(idata) %>%
carry_out(dose) %>%
mrgsim() %>%
plot(CP~time|factor(dose), scales = "same") Option 2:Join new_data <- left_join(data, idata, by = "ID") Now, individual parameters are combined with dosing data head(new_data)
#> ID time cmt evid amt ii addl dose CL
#> 1 1 0 1 1 100 24 9 100 0.6355913
#> 2 2 0 1 1 100 24 9 100 0.6641948
#> 3 3 0 1 1 100 24 9 100 1.4896229
#> 4 4 0 1 1 100 24 9 100 1.3380746
#> 5 5 0 1 1 100 24 9 100 1.4147902
#> 6 6 0 1 1 100 24 9 100 1.0306401
mod %>%
data_set(new_data) %>%
carry_out(dose) %>%
mrgsim() %>%
plot(CP~time|factor(dose), scales = "same") Created on 2020-10-19 by the reprex package (v0.3.0) |
Beta Was this translation helpful? Give feedback.
-
Thanks Kyle, this is great help. May I please also ask if there is a way to include uncertainty of the parameters in the simulations, e.g. RSEs from the estimation? On another note, I got an error message for non-dosing records without CMT values. Those are records to for observation or to change the covariate values (EIVD=0 or2) so there are no associated CMT numbers. Do I have to put in a CMT number in this case, and does it matter what values I put in? And the same issue with the RATE item, I got an error for missing RATE values when the event is not a dose record. Do I have to put in something for RATE - which I think will get an error in NONMEM when RATE is there without AMT? |
Beta Was this translation helpful? Give feedback.
-
Here's an example of simulating with uncertainty. I have to simulate the parameters here and presumably you'll already have that. But you'll see what are the other mechanics that you need to do: library(tidyverse)
library(mrgsolve)
#>
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#>
#> filter This is a nonmem run inside mrgsolvemod <- modlib("1005")
#> Building 1005 ... done. The run stuff is located here project <- system.file("nonmem", "1005", package = "mrgsolve")
ext_file <- file.path(project, "1005.ext")
est <- read_table(ext_file,skip=1) %>% filter(ITERATION ==-1e9)
#> Parsed with column specification:
#> cols(
#> ITERATION = col_double(),
#> THETA1 = col_double(),
#> THETA2 = col_double(),
#> THETA3 = col_double(),
#> THETA4 = col_double(),
#> THETA5 = col_double(),
#> THETA6 = col_double(),
#> THETA7 = col_double(),
#> `SIGMA(1,1)` = col_double(),
#> `SIGMA(2,1)` = col_double(),
#> `SIGMA(2,2)` = col_double(),
#> `OMEGA(1,1)` = col_double(),
#> `OMEGA(2,1)` = col_double(),
#> `OMEGA(2,2)` = col_double(),
#> `OMEGA(3,1)` = col_double(),
#> `OMEGA(3,2)` = col_double(),
#> `OMEGA(3,3)` = col_double(),
#> OBJ = col_double()
#> )
est <- select(est, matches("THETA|OMEGA|SIGMA"))
mu <- as.numeric(log(est))
#> Warning in FUN(X[[i]], ...): NaNs produced
#> Warning in FUN(X[[i]], ...): NaNs produced Simulate some parametersYou will already have this from bootstrap, BAYES run etc pars <- MASS::mvrnorm(1000, mu, dmat(rep(0.01, length(mu))))
pars <- as.data.frame(exp(pars))
names(pars) <- names(est)
head(pars)
#> THETA1 THETA2 THETA3 THETA4 THETA5 THETA6 THETA7
#> 1 7.424726 22.48143 0.07278706 3.348785 138.38830 0.9693311 1.031972
#> 2 9.957642 19.22409 0.07513595 3.421555 97.47279 0.9794705 1.310917
#> 3 9.779223 21.82776 0.05744524 3.059223 106.25812 0.9090591 1.306647
#> 4 9.900401 20.31143 0.07801912 3.468303 125.49913 0.9647835 1.015118
#> 5 11.001511 25.19532 0.06254278 3.113375 122.54329 1.0089037 1.290575
#> 6 10.888949 20.20446 0.06728664 3.423947 133.32373 0.9415983 1.236596
#> SIGMA(1,1) SIGMA(2,1) SIGMA(2,2) OMEGA(1,1) OMEGA(2,1) OMEGA(2,2) OMEGA(3,1)
#> 1 0.04679533 0 0.2192475 0.1982942 0.1079991 0.09792380 NaN
#> 2 0.04671150 0 0.2252488 0.2125749 0.1080456 0.09323076 NaN
#> 3 0.04142997 0 0.1769342 0.2383890 0.1271883 0.10542438 NaN
#> 4 0.04085041 0 0.1950293 0.2139424 0.1176727 0.09761303 NaN
#> 5 0.03970289 0 0.2047149 0.1937804 0.1095106 0.10154214 NaN
#> 6 0.07508084 0 0.2404043 0.2206086 0.1262407 0.09860323 NaN
#> OMEGA(3,2) OMEGA(3,3)
#> 1 NaN 0.05345265
#> 2 NaN 0.05765921
#> 3 NaN 0.03891364
#> 4 NaN 0.04631358
#> 5 NaN 0.04845226
#> 6 NaN 0.04698587 Simplify pars <- mutate(
pars,
`OMEGA(2,1)` = 0,
`OMEGA(3,1)` = 0,
`OMEGA(3,2)` = 0
) Pull out OMEGA and SIGMAWe make a list of matrices for each omegas <- as_bmat(pars, "OMEGA") For example omegas[[1]]
#> [,1] [,2] [,3]
#> [1,] 0.1982942 0.0000000 0.00000000
#> [2,] 0.0000000 0.0979238 0.00000000
#> [3,] 0.0000000 0.0000000 0.05345265
sigmas <- as_bmat(pars, "SIGMA") Dummy datadata <- expand.ev(ID = 1:100, amt = c(100, 300, 1000)) Simulate
do_sim <- function(i) {
mod <- update(
mod,
param = slice(pars,i),
omega = omegas[[i]],
sigma = sigmas[[i]]
)
mrgsim_df(mod, data = data) %>% mutate(rep = i)
}
out <- map_dfr(seq(100), do_sim)
head(out)
#> ID time GUT CENT PERIPH CL Q V2 V3
#> 1 1 0 0.00000 0.000000 0.0000000 9.00391 3.348785 29.85156 138.3883
#> 2 1 0 100.00000 0.000000 0.0000000 9.00391 3.348785 29.85156 138.3883
#> 3 1 1 93.51326 5.302249 0.3191565 9.00391 3.348785 29.85156 138.3883
#> 4 1 2 87.44731 8.475473 1.0914230 9.00391 3.348785 29.85156 138.3883
#> 5 1 3 81.77483 10.270128 2.1146738 9.00391 3.348785 29.85156 138.3883
#> 6 1 4 76.47031 11.177728 3.2593804 9.00391 3.348785 29.85156 138.3883
#> KA ETA_1 ETA_2 ETA_3 IPRED rep
#> 1 0.0670669 0.1928431 0.2835473 -0.08184758 0.0000000 1
#> 2 0.0670669 0.1928431 0.2835473 -0.08184758 0.0000000 1
#> 3 0.0670669 0.1928431 0.2835473 -0.08184758 0.1776205 1
#> 4 0.0670669 0.1928431 0.2835473 -0.08184758 0.2839206 1
#> 5 0.0670669 0.1928431 0.2835473 -0.08184758 0.3440399 1
#> 6 0.0670669 0.1928431 0.2835473 -0.08184758 0.3744437 1 Created on 2020-10-20 by the reprex package (v0.3.0) |
Beta Was this translation helpful? Give feedback.
-
Right; you'll have to make When Thanks, |
Beta Was this translation helpful? Give feedback.
-
Thanks Kyle. I do most simulations in mrgsolve, but can't completely avoid using nonmem for benchmarking or file sharing etc. so try to have the same dataset in both if possible or with known discrepancies for reproducibility and reduce the risk of error. I suppose these non-dosing Thanks you so much for the example. If I understand it correctly, I'd have to first generate the parameters for each replicate using, e.g. the If I may clarify a few things, is it possible to use the uncertainty values from the nonmem output directly when using |
Beta Was this translation helpful? Give feedback.
-
Hi @tmscs - Right ... don't use the code I had there for simulating from the posterior; that was just to get something for me to work with to show you the mechanics of simulation assuming you already have the posterior. If you don't have that, you can bootstrap the NONMEM run or I'd highly recommend running BAYES if you really want an easy way to generate that uncertainty distribution. I know you can't just switch to BAYES any time, but if it's a possibility, I'd consider it. If you want to simulate from the covariance matrix of the estimate, you can check this vignette out: See Section 9 ... this uses the function install.packages("metrumrg", repos = "https://mpn.metworx.com/snapshots/stable/2020-09-20") There is also some code related to that vignette here: On the question of log-transformation: if you do simulate from the covariance matrix, I think you do need to estimate the parameters on log scale; you will simulate negative values for THETAs that are supposed to be positive etc and just in general the distribution of THETAs that you simulate won't be correct. As far as the data set goes, I know it's hard to balance all of the competing interests. I can help you with code to make the transformation if you can't do it in the source data. Kyle |
Beta Was this translation helpful? Give feedback.
-
Thanks for the clarification Kyle. May I ask when you said running BAYES, did you mean running an additional step with $EST BAYES using the final model to get a distribution of parameters or else? |
Beta Was this translation helpful? Give feedback.
-
Hi @tmscs - I was suggesting to estimate the model using If the model has to be estimates with FOCEI, I'd bootstrap or simulate from the covariance matrix (examples in the previous comment). Kyle |
Beta Was this translation helpful? Give feedback.
-
Thanks very much for your clarification. It's really great help. |
Beta Was this translation helpful? Give feedback.
-
Hi Kyle,
I have been trying to do some simulations in mrgsolve for a population with relatively complex dosing regimens (loading dose followed by multiple doses over weeks with individualised doses) and individual covariates. I've managed to specify the individual regimens in "data_set", and the individual parameters and covariates in "idata_set". However, I noticed that mrgsolve accepts only either the "data_set" or the "idata_set" but not both, and idata allows only one subject per row so I can't specify the dosing regimens in idata. May I ask how can this be done in mrgsolve - specify individual regimens and covariates?
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions