Skip to content

Commit

Permalink
adding README
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebaron committed Sep 1, 2019
1 parent 13eca12 commit 51c696d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Makefile
^vera_.*\.tar\.gz
vera\.Rcheck
\.Rhistory
README.md
README.Rmd
README.html
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
README.html
vera.Rcheck
test.R
vera*.tar.gz
.Rproj.user
.Rhistory
.RData
.Ruserdata
.DS_Store
7 changes: 4 additions & 3 deletions R/vera.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ dvalue <- function(sim,ref,scale) {
#' @param var output names (compartment or capture) as character vector
#' or comma-separated string
#' @param eps parameter change value for sensitivity analysis
#' @param ... arguments passed to `fun`
#'
#' @export
lsa <- function(mod, fun, par, var, eps=1E-8) {
lsa <- function(mod, fun, par, var, eps=1E-8, ...) {
parameters <- mrgsolve::param(mod)
par_names <- names(parameters)
par_sens <- cvec_cs(par)
Expand All @@ -53,7 +54,7 @@ lsa <- function(mod, fun, par, var, eps=1E-8) {
}
parm <- as.numeric(parameters)[par_sens]
var <- cvec_cs(var)
base <- fun(parm)
base <- fun(parm,...)
if(!all(var %in% names(base))) {
col_bad <- setdiff(var,names(base))
col_bad <- paste0(col_bad,collapse=',')
Expand All @@ -76,7 +77,7 @@ lsa <- function(mod, fun, par, var, eps=1E-8) {
tosim <- lapply(seq_along(new_p), function(i) {
base_par[i] <- new_p[i]
})
out <- lapply(tosim,fun)
out <- lapply(tosim,fun,...)
out <- lapply(out, function(x) x[,cols_keep])
out <- lapply(out, make_long, cols = var)
for(i in seq_along(tosim)) {
Expand Down
62 changes: 62 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: "local sensitivity analysis with mrgsolve"
output: github_document
---

```{r,setup,include=FALSE}
knitr::opts_chunk$set(
comment = '.', message=FALSE, warning = FALSE,
fig.path = "man/images/readme-"
)
```

# Model
Load the vera package and a model from the mrgsolve package. We
increase the tolerance a bit as well as the maximum step size.
```{r,message=FALSE}
library(vera)
mod <- modlib(
"pbpk",
end = 12, delta = 0.1,
atol = 1E-20, rtol = 1E-12, hmax=0.5
)
```

# Scenario
Create a function that uses the model to simulate a certain scenario. For now,
we just simulate a single dose.
```{r}
fun <- function(p,dose) {
mod %>%
param(p) %>%
ev(dose) %>%
mrgsim_df()
}
d <- ev(amt = 100)
```

# Sensitivity analysis

Use `vera::lsa()`. We pick the paramters that we want to fiddle with (`par`)
and the output that we want to look at (`Cp` and `Amu` - the amount in the
muscle compartment). `d` gets passed through to the function as `dose`.
```{r}
out <- lsa(mod, fun, par = "Kpli,Kpmu,BW,Ka", var = "Cp,Amu", dose = d)
```

# Ouput

The output is long and ready to send in to `ggplot2`.
```{r}
head(out)
```

There is a default plotting method as well.

```{r}
plot(out)
```


68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local sensitivity analysis with mrgsolve
================

# Model

Load the vera package and a model from the mrgsolve package. We increase
the tolerance a bit as well as the maximum step size.

``` r
library(vera)

mod <- modlib(
"pbpk",
end = 12, delta = 0.1,
atol = 1E-20, rtol = 1E-12, hmax=0.5
)
```

# Scenario

Create a function that uses the model to simulate a certain scenario.
For now, we just simulate a single dose.

``` r
fun <- function(p,dose) {
mod %>%
param(p) %>%
ev(dose) %>%
mrgsim_df()
}

d <- ev(amt = 100)
```

# Sensitivity analysis

Use `vera::lsa()`. We pick the paramters that we want to fiddle with
(`par`) and the output that we want to look at (`Cp` and `Amu` - the
amount in the muscle compartment). `d` gets passed through to the
function as `dose`.

``` r
out <- lsa(mod, fun, par = "Kpli,Kpmu,BW,Ka", var = "Cp,Amu", dose = d)
```

# Ouput

The output is long and ready to send in to `ggplot2`.

``` r
head(out)
```

. time var value par sens
. 1 0.0 Cp 0.000000 Kpli 0.0000000
. 2 0.0 Cp 0.000000 Kpli 0.0000000
. 3 0.1 Cp 0.913300 Kpli -0.2344372
. 4 0.2 Cp 1.240780 Kpli -0.2207772
. 5 0.3 Cp 1.425315 Kpli -0.2315567
. 6 0.4 Cp 1.546065 Kpli -0.2460310

There is a default plotting method as well.

``` r
plot(out)
```

![](man/images/readme-unnamed-chunk-5-1.png)<!-- -->
Binary file added man/images/readme-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 51c696d

Please sign in to comment.