% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sg-vpc-sim.R
\name{sg_vpc_sim}
\alias{sg_vpc_sim}
\title{Perform simulations for VPC plot}
\usage{
sg_vpc_sim(
  fpath_i,
  gco = NULL,
  model = NULL,
  time_col = "TIME",
  outputs = NULL,
  npop = 100
)
}
\arguments{
\item{fpath_i}{Either a character string specifying the file path to a saved \code{sg_fit} object (R data file), or a list object containing the \code{sg_fit} results directly. The object must contain: \code{SDTAB}, \code{EVTAB}, \code{SUMTAB}, \code{OMEGAMAT}, \code{SIGMAMAT}, and optionally \code{COTAB} and \code{CATAB}}

\item{gco}{Generalized control object. Either an R list or path to Rdata/json file. Either \code{gco} or \code{model} file should be specified for simulations model specification. Defauls is \code{NULL}}

\item{model}{A model object passed to \code{sg_sim()}.}

\item{time_col}{String. The column to use as a time column. Currently, can be only \code{TIME}. Default is \code{TIME}.}

\item{outputs}{Vector of strings. Names of the model variables to output.
If \code{NULL}, all variables returned. Default is \code{NULL}.}

\item{npop}{Integer specifying the number of virtual subjects to simulate per original individual. Higher values provide more robust percentile estimates but increase computation time. Default is \code{100}}
}
\value{
A dataset with simulation results
}
\description{
Perform simulations for VPC plot
}
\examples{
\donttest{
library(rxode2)
library(tibble)
fpath_i <- system.file("extdata", "simurg_object", "Warfarin_PK.RData", package = "SimuRg")
# Simulate VPC from with generalized model object
mod <- rxode2::rxode2({
  ka_pop = 0.1;
  Vd_pop = 10;
  Cl_pop = 0.5;

  omega_ka = 0;
  omega_Vd = 0;
  omega_Cl = 0;

  Cc_b = 0;
  ka_tv = exp(ka_pop);
  Vd_tv = exp(Vd_pop);
  Cl_tv = exp(Cl_pop);

  ka = ka_tv * exp(omega_ka);
  Vd = Vd_tv * exp(omega_Vd);
  Cl = Cl_tv * exp(omega_Cl);

  Cc = Ac / Vd;

  Ad(0) = 0;
  Ac(0) = 0;

  d/dt(Ad) = -ka * Ad;
  d/dt(Ac) = ka * Ad - Cl * Cc;

  Cc_ResErr = Cc * (1 + Cc_b);
})

sg_vpc_sim(fpath_i, model=mod, outputs = "Cc_ResErr")
# Make VPC plot with the generalized control object
model <- system.file("extdata", "models", "rxode", "model_PK_1c.txt", package = "SimuRg")
data  <- system.file("extdata", "datasets", "dspk-warf.csv", package = "SimuRg")
headers <- list(list(name = "ID", use = "identifier", type = NULL),
             list(name = "TIME", use = "time", type = NULL),
                          list(name = "DV", use = "observation", type = "continuous"),
                          list(name = "DVID", use = "observationtype", type = NULL),
                          list(name = "ADM", use = "administration", type = NULL),
                          list(name = "AMT", use = "amount", type = NULL),
                          list(name = "EVID", use = "eventidentifier", type = NULL),
                          list(name = "MDV", use = "missingdependentvariable", type = NULL),
                          list(name = "AGE", use = "covariate", type = "continuous"),
                          list(name = "AGE_centered", use = "covariate", type = "continuous"),
                          list(name = "SEX", use = "covariate", type = "categorical"),
                          list(name = "WEIGHT", use = "covariate", type = "continuous"),
                          list(name = "BMI", use = "covariate", type = "continuous"))

theta <- tribble(~NAME, ~TRANS, ~INIT, ~LB, ~UB, ~EST,
                  "Cl", "logNormal", 0.2, NA, NA, TRUE,
                "V", "logNormal", 20, NA, NA, TRUE,
                "ka", "logNormal", 0.2, NA, NA, TRUE
)

ruv <- list(YNAME = "y1", DVID = 1, TRANS = "normal", PRED = "Cc",
           ERR = "combined1", INIT = c(1, 1), EST = c(TRUE, TRUE), BLQM = NULL)

re <- list(init = tribble(~Cl, ~V, ~ka,
                            1, 0, 0,
                            0, 1, 0,
                            0, 0, 1) \%>\% as.matrix(),
            est = tribble(~Cl, ~V, ~ka,
                         TRUE, NA, NA,
                           NA, TRUE, NA,
                          NA, NA, TRUE) \%>\% as.matrix())
occ <- list(init = tribble(~Cl, ~V, ~ka,
                        0, 0, 0,
                          0, 0, 0,
                          0, 0, 0) \%>\% as.matrix(),
           est = tribble(~Cl, ~V, ~ka,
                         NA, NA, NA,
                         NA, NA, NA,
                         NA, NA, NA) \%>\% as.matrix())
covs <- list(list(PAR = "V", COVNAME = "AGE", FUNC = "linear",
                 TRANS = "median", INIT = 1, EST = TRUE),
            list(PAR = "ka", COVNAME = "SEX", REF = 0, INIT = 1, EST = TRUE))

gco <-list(headers = headers, data = data, model = model, task_opt = "",
          covs = covs, project_name = "test-proj", theta = theta,
          ruv = ruv, re = re, occ = occ, modelText = "")

res <- sg_vpc_sim(fpath_i, gco = gco, output = "Cc")
}
}
