Skip to content

Commit

Permalink
adds vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebaron committed Mar 22, 2021
1 parent 15974fc commit cd08513
Show file tree
Hide file tree
Showing 15 changed files with 950 additions and 32 deletions.
16 changes: 9 additions & 7 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ inst/validation
cran-comments.md
Rchecks
mrgsolve\.Rcheck
.github
inst/doc
\.github
Doxyfile
doxydoc
^.*\.Rproj$
^\.Rproj\.user$
vignettes
vignettes/.*[.]toc$
vignettes/.*[.]log$
vignettes/.*[.]o$
vignettes/.*[.]so$
tests/.*[.]o$
tests/.*[.]so$
tests/testthat/.*[.]o$
Expand Down Expand Up @@ -53,3 +47,11 @@ docs
^_pkgdown\.yml$
^\.drone\.yml$
^\.drone\.jsonnet$
^doc$
^Meta$
vignettes/mevent\.Rmd
vignettes/mevent-0\.cpp
vignettes/mevent-1\.cpp
vignettes/build
vignettes/mrgsolve_builds
vignettes/mrgsolve-builds
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Rchecks
!package/*.tar.gz
*00_pkg_src
inst/maintenance/unit/tests.md
doc
Meta
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ RdMacros:
Encoding: UTF-8
Language: en-US
LazyLoad: yes
VignetteBuilder: knitr
NeedsCompilation: yes
RoxygenNote: 7.1.1
SystemRequirements: C++11
Expand Down
57 changes: 32 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,37 @@ export _MRGSOLVE_SKIP_MODLIB_BUILD_=true
LOAD_CANDIDATE=library(mrgsolve, lib.loc="mrgsolve.Rcheck")
TEST_UNIT=testthat::test_dir("inst/maintenance/unit",stop_on_failure=TRUE)

bump-dev:
Rscript -e 'usethis::use_version("dev")'

tag-version:
git tag $(VERSION)
git push origin $(VERSION)

all:
make doc
make build
make install

package:
make house
make doc
make build-vignettes

check:
make house
make doc
make build
R CMD check --ignore-vignettes ${TARBALL} --no-manual
make unit

cran:
make house
make doc
make build-vignettes
export _MRGSOLVE_SKIP_MODLIB_BUILD_=false
R CMD CHECK --as-cran ${TARBALL}

drone:
make house
R CMD build --md5 $(PKGDIR)
Expand Down Expand Up @@ -42,28 +68,23 @@ everything:

pkgdown:
Rscript "inst/maintenance/pkgdown.R"
cp -r DOCS/ ../../mrgsolve/docs/
touch ../../mrgsolve/docs/.nojekyll
cp -r DOCS/ ../mrgsolve/docs/
touch ../mrgsolve/docs/.nojekyll

unit:
Rscript -e 'testthat::test_dir("inst/maintenance/unit")'

cran:
make house
make doc
make build
export _MRGSOLVE_SKIP_MODLIB_BUILD_=false
R CMD CHECK --as-cran ${TARBALL}

readme:
Rscript -e 'rmarkdown::render("README.Rmd")'

.PHONY: doc
doc:
Rscript -e "roxygen2::roxygenize()"

.PHONY: build
build:
R CMD build --no-build-vignettes --md5 $(PKGDIR) --no-manual

build-vignettes:
R CMD build --md5 $(PKGDIR) --no-manual

install:
Expand All @@ -72,13 +93,6 @@ install:
install-build:
R CMD INSTALL --build --install-tests ${TARBALL}

check:
make house
make doc
make build
R CMD check ${TARBALL} --no-manual
make unit

qcheck:
make doc
make build
Expand All @@ -87,7 +101,7 @@ qcheck:
check-cran:
make house
make doc
make build
make build-vignettes
R CMD check --as-cran ${TARBALL}

test:
Expand Down Expand Up @@ -126,13 +140,6 @@ check-winhub:
doxygen:
doxygen doxyfile

bump-dev:
Rscript -e 'usethis::use_version("dev")'

tag-version:
git tag $(VERSION)
git push origin $(VERSION)

testing:
cp ${TARBALL} ${MRGSOLVE_TEST_LOC}
touch ${MRGSOLVE_TEST_LOC}/${TARBALL}
Expand Down
150 changes: 150 additions & 0 deletions vignettes/events.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
title: "Event objects"
author: ""
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 1
number_sections: true
keep_md: true
fig_width: 5
fig_height: 4
vignette: >
%\VignetteIndexEntry{Event objects}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---


```{r setup, echo = FALSE, message = FALSE, warning = FALSE, code = readLines("global.R")}
set.seed(23)
```

```{r setup2}
library(mrgsolve)
library(dplyr)
```

# Introduction

Event objects are simple ways to implement PK dosing events into your model
simulation.

Let's illustrate event objects with a one-compartment, PK model. We read this
model from the `mrgsolve` internal model library.

```{r,message=FALSE}
mod <- modlib("pk1cmt", end = 216, delta = 0.1)
```

# Events

Events are constructed with the `ev()` function

```{r}
e <- ev(amt = 100, ii = 24, addl = 6)
```

This will implement 100 unit doses every 24 hours for a total of 7 doses. `e`
has class `ev`, but really it is just a data frame

```{r}
e
```

```{r}
as.data.frame(e)
```

We can implement this series of doses by passing `e` in as the `events` argument
to `mrgsim`

```{r}
mod %>% mrgsim(events = e) %>% plot(EV1 + CP ~ time)
```


The events can also be implemented with the `ev` constructor along the
simulation pipeline

```{r}
mod %>%
ev(amt = 100, ii = 24, addl = 6) %>%
mrgsim %>%
plot(CP ~ time)
```


## Event defaults and expectations

1. `amt` is required
1. `evid = 0` is forbidden
1. Default `time` is 0
1. Default `evid` is 1
1. Default `cmt` is 1

Also by default, `rate`, `ss` and `ii` are 0.

# Combine events

`mrgsolve` has operators defined that allow you to combine events. Let's first
define some event objects.

```{r}
e1 <- ev(amt = 500)
e2 <- ev(amt = 250, ii = 24, addl = 4)
e3 <- ev(amt = 500, ii = 24, addl = 0)
e4 <- ev(amt = 250, ii = 24, addl = 4, time = 24)
```


We can combine `e1` and `e3` with a collection operator

```{r}
c(e1, e4)
```

`mrgsolve` also defines a `%then$` operator that lets you execute one event and
`%then%` a second event
```{r}
e3 %then% e2
```

Notice that `e3` has both `ii` and `addl` defined. This is required for
`mrgsolve` to know when to start `e2`.


## Combine to create a data set

We can take several event objects and combine them into a single simulation data
frame with the `as_data_set` function.

```{r}
e1 <- ev(amt = 100, ii = 24, addl = 6, ID = 1:5)
e2 <- ev(amt = 50, ii = 12, addl = 13, ID = 1:3)
e3 <- ev(amt = 200, ii = 24, addl = 2, ID = 1:2)
```

When combined into a data set, we get
* N=5 IDs receiving 100 mg Q24h x7
* N=3 IDs receiving 50 mg Q12h x 14
* N=2 IDs receiving 200 mg Q48h x 3

```{r}
data <- as_data_set(e1, e2, e3)
data
```

To simulate from this data set, we use the `data_set` function. First, let's
load a population PK model

```{r,message=FALSE}
mod <- modlib("popex")
```
```{r}
mod %>% data_set(data) %>% mrgsim(end=336) %>% plot("GUT, DV")
```


2 changes: 2 additions & 0 deletions vignettes/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
knitr::opts_chunk$set(message = FALSE, warning = FALSE, comment='.')
options(mrgsolve.soloc = "mrgsolve-builds")
Loading

0 comments on commit cd08513

Please sign in to comment.