--- title: "Covariate assessment with the Wasserstein distance" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Covariate assessment with the Wasserstein distance} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5) ``` `nm_wasserstein()` implements the method of Simon, Hulot and von Fabeck (Front Pharmacol 2026, doi:10.3389/fphar.2026.1804989). For one or several runs, it compares the distribution of the individual deviations (ETA) between groups of subjects defined by a covariate: * the distance between the two distributions is the 1-Wasserstein distance W (the area between their empirical cumulative distributions); its p-value comes from a permutation test, and a t-test is given for reference; * a categorical covariate compares its levels; a continuous covariate compares its 4th and 1st quartiles (`continuous = "extremes"`, default) or every pair of quartiles (`continuous = "pairwise"`, Bonferroni-adjusted); * a Wasserstein-based R2, `R2W = 1 - W/W_ref`, where W is the distance of the ETA distribution to 0, measures how much of the variability left in the reference run is explained by a model; the ETA shrinkage is reported with it. ## Example The example project of the package contains a BASE model without covariate and a FINAL model with body weight and genotype on CL (simulated data, see `vignette("nmpilot")`). The covariates are read from the data set named in `$DATA`; the ETA on CL is found from the model code. ```{r w} library(nmpilot) ex <- system.file("extdata", "example", package = "nmpilot") w <- nm_wasserstein( runs = c(BASE = file.path(ex, "run1.reg"), FINAL = file.path(ex, "run2.reg")), covariates = c("WT", "GENO"), eta = "CL", n_perm = 2000 ) w ``` In the BASE model the distribution of the CL deviations differs strongly between genotypes; in the FINAL model, where genotype is in the model, the two distributions overlap. ```{r ecdf} plot(w) ``` ```{r r2w, fig.height = 3.5} plot(w, type = "r2w") ``` ## Several pairs of quartiles ```{r pairwise} wp <- nm_wasserstein(c(BASE = file.path(ex, "run1.reg")), covariates = "WT", eta = "CL", n_perm = 2000, continuous = "pairwise") wp$tests[, c("comparison", "W", "p_W", "p_W_adj")] ``` ## Inputs `runs` takes run directories of any name (`run1.reg`, `run1.nm7`, `run1/`) or a run prefix (`"C:/study/run1"` for `run1.ext`, `run1.phi` and `run1.ctl` in the same folder). Each run needs its `.phi` (individual ETAs), `.ext` (OMEGA, for the shrinkage) and control stream. The covariates can also be given directly with `data`, a data.frame or a CSV file with one row per subject.