--- title: "Getting started with nmpilot" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting started with nmpilot} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5) ``` This vignette walks through a small popPK analysis with **nmpilot**, from the data set and a first control stream to the comparison of two models. NONMEM is not needed to read it: the package ships an example project whose NONMEM runs have already been made. The chunks that call NONMEM are shown but not run. ```{r setup} library(nmpilot) ex <- system.file("extdata", "example", package = "nmpilot") list.files(ex, recursive = TRUE) ``` The data are **simulated** (60 fictitious subjects, single oral dose of 100 mg, 9 samples each, body weight `WT` and a binary genotype `GENO`; see `README.txt` and `sim_example_data.R` in the example folder). Two models were fitted with NONMEM 7.6: * `run1`: one-compartment model with first-order absorption, no covariate (BASE); * `run2`: same model with weight on CL (estimated exponent) and V (exponent fixed to 1), and genotype on CL (FINAL). ## 1. The data set `nm_read_csv()` reads a NONMEM data set, identifies the reserved columns and computes the time after dose. `nm_summary()` gives a short diagnostic. ```{r data} dat <- nm_read_csv(file.path(ex, "example_data.csv")) nm_summary(dat$data) ``` ```{r plot-dv} nm_plot_dv(dat$data, scales = "log", verbose = FALSE) ``` ## 2. Writing a control stream `nm_write_ctl()` writes a first control stream from the columns of the data set: here a one-compartment model with first-order absorption, as in `run1`, with a combined error model. `$INPUT` follows the column order of the file, the `#` of `#ID` becomes the `IGNORE` character, and the covariates are written in a `cotab` table. ```{r write-ctl} ctl <- nm_write_ctl(data = "../example_data.csv", input = file.path(ex, "example_data.csv"), error = "combined", theta_init = c(CL = 4, V = 40), covariates = c("WT", "GENO")) ctl ``` `nm_ctl_check()` looks for inconsistencies that can be seen without NM-TRAN, such as a THETA, ETA or EPS used without a declaration. It also works on a control stream written by hand: ```{r check-ctl} nm_ctl_check(ctl) nm_ctl_check(file.path(ex, "run1.ctl")) ``` Three functions transform a control stream: `nm_ctl_mu()` (MU referencing, for the EM and Bayesian methods), `nm_ctl_iov()` (inter-occasion variability) and `nm_ctl_m3()` (M3 method for data below the limit of quantification). With `reference`, `nm_ctl_check()` shows what changed: ```{r mu-ctl} mu <- nm_ctl_mu(ctl) nm_ctl_check(mu, reference = ctl) ``` Use `output_file = "run3.ctl"` to write the result to disk. ## 3. Running NONMEM `nm_run()` calls the `nmfe` launcher directly (no PsN, no Wings for NONMEM). By default each run gets its own `.reg` subdirectory, as in the example folder; `run_subdir = ""` runs it next to its control stream instead. The functions that write files are run here on a copy of the example folder. ```{r run, eval = FALSE} file.copy(ex, tempdir(), recursive = TRUE) proj <- file.path(tempdir(), "example") log <- nm_run( ctl = c("run1.ctl", "run2.ctl"), nmfe_path = "C:/nm76g64/run/nmfe76.bat", analysis_dir = proj, skip_terminated = FALSE ) ``` ## 4. Results of all the runs `nm_extract_runs()` reads every run of a folder: objective function, minimisation and covariance status, parameters with their RSE and shrinkage. Parameters are named from the model code (`CL`, `V`, `KA`...). ```{r runs} runs <- nm_extract_runs(ex) runs$runs[, c("run", "ofv", "dOFV", "min_ok", "cov_status")] nm_compare_runs(runs, what = "value", format = TRUE) ``` The link between each THETA/ETA and the PK parameter comes from `nm_identify_parameters()`: ```{r identify} nm_identify_parameters(file.path(ex, "run2.ctl"))$parameter_table ``` A parameter table of one run can be exported to Word with `nm_extract_run_table()` (packages flextable and officer): ```{r run-table, eval = FALSE} nm_extract_run_table(file.path(proj, "run2.reg")) ``` ## 5. Goodness-of-fit plots ```{r gof, fig.height = 4} gof <- nm_gof(file.path(ex, "run2.reg", "sdtab2")) names(gof$plots) gof$plots$dv_pred gof$plots$cwres_time ``` ## 6. Comparing two models `nm_compare()` puts the BASE and FINAL runs side by side, subject by subject and observation by observation. ```{r compare} cmp <- nm_compare(file.path(ex, "run1.reg"), file.path(ex, "run2.reg"), labels = c("BASE", "FINAL")) cmp ``` ## 7. Visual predictive check The VPC needs two more NONMEM runs (the model re-run on the data, and the simulations). `nm_vpc_ctl()` writes and launches them, then `nm_vpc()` draws the VPC or the prediction-corrected VPC. ```{r vpc, eval = FALSE} nm_vpc_ctl(file.path(proj, "run2.ctl"), nmfe_path = "C:/nm76g64/run/nmfe76.bat", n_sim = 500) nm_vpc(file.path(proj, "run2.ctl"), col_names = c("NSIM", "ID", "TIME", "AMT", "MDV", "DV", "PRED", "IPRED"), type = 2) ``` ## 8. Uncertainty `nm_profile()` (likelihood profiles), `nm_bootstrap()` (case bootstrap) and `nm_jackknife()` (influence of each subject) also run NONMEM and are not executed here. ```{r uncertainty, eval = FALSE} prof <- nm_profile(file.path(proj, "run2.ctl"), "C:/nm76g64/run/nmfe76.bat", param = "THETA(6)", from = 0.1, to = 1, by = 0.1) boot <- nm_bootstrap(file.path(proj, "run2.ctl"), "C:/nm76g64/run/nmfe76.bat", n = 500) jk <- nm_jackknife(file.path(proj, "run2.reg", "run2.ctl"), nmfe_path = "C:/nm76g64/run/nmfe76.bat") plot(jk, "scatter") ``` The covariate assessment based on the Wasserstein distance is described in `vignette("wasserstein", package = "nmpilot")`.