-
Notifications
You must be signed in to change notification settings - Fork 12
/
create-run-population.Rmd
150 lines (112 loc) · 6.54 KB
/
create-run-population.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Population simulations"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Population simulations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
draft: true
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.showtext = TRUE
)
```
## Population simulations
Population simulations can be easily performed in R by combining the simulation loaded from a *.pkml file with the population information created in PK-Sim and exported to CSV format (for details, please refer to [OSPS online documentation](https://docs.open-systems-pharmacology.org/working-with-pk-sim/pk-sim-documentation/pk-sim-creating-populations)) or created directly in R (see [Creating populations](#creating-populations)).
### Loading population file
The method `loadPopulation` creates an object of the `Population` class that can be passed to the `runSimulations()` method (see [Running simulations and retrieving the results](run-simulation.html)).
```{r loadPop}
library(ospsuite)
# Load population information from csv
popFilePath <- system.file("extdata", "pop.csv", package = "ospsuite")
myPopulation <- loadPopulation(csvPopulationFile = popFilePath)
print(myPopulation)
```
## Creating populations {#SectionCreatePopulation}
Similar to creating individual parameter sets (see [Creating individuals](create-individual.html)), a population is created from *population characteristics* created by calling the method `createPopulationCharacteristics()`. To see the list of available values for the arguments `species` and `population` (only for human), use the enums `Species` and `HumanPopulation`, respectively. The returned object of type `PopulationCharacteristics` is then passed to the function `createPopulation` to generate a set of parameter values. The algorithm behind is the same used in PK-Sim when creating an population. Molecule ontogenies can be added as described in the vignette [Creating individuals](create-individual.html).
```{r createPop}
library(ospsuite)
# If no unit is specified, the default units are used. For "height" it is "dm", for "weight" it is "kg", for "age" it is "year(s)".
populationCharacteristics <- createPopulationCharacteristics(
species = Species$Human,
population = HumanPopulation$Asian_Tanaka_1996,
numberOfIndividuals = 50,
proportionOfFemales = 50,
weightMin = 30,
weightMax = 98,
weightUnit = "kg",
heightMin = NULL,
heightMax = NULL,
ageMin = 0,
ageMax = 80,
ageUnit = "year(s)"
)
print(populationCharacteristics)
# Create population from population characteristics
result <- createPopulation(populationCharacteristics = populationCharacteristics)
myPopulation <- result$population
print(myPopulation)
```
## Running population simulation
To run a population simulation, the `Population` object created by the `createPopulation` method must be passed to the `runSimulation()` method:
```{r runPop}
library(ospsuite)
# Load simulation
simFilePath <- system.file("extdata", "Aciclovir.pkml", package = "ospsuite")
sim <- loadSimulation(simFilePath)
# Run population simulation
simulationResults <- runSimulations(simulations = sim, population = myPopulation)[[1]]
print(simulationResults)
```
Population simulations are run in parallel on multi-core machines - one core simulates a subset of all individuals defined in the population. By default, the number of cores used equals the maximal number of logical cores available minus one.
The user can change the default behavior by providing custom `SimulationRunOptions()`.
```{r simulationRunOptions}
# Load simulation
simFilePath <- system.file("extdata", "Aciclovir.pkml", package = "ospsuite")
sim <- loadSimulation(simFilePath)
# Create a SimulationRunOptions object
simRunOptions <- SimulationRunOptions$new()
print(simRunOptions)
# Change the maximal number of cores to use and show a progress bar during simulation
simRunOptions$numberOfCores <- 3
simRunOptions$showProgress <- TRUE
# Run population simulation with custom options
populationResults <- runSimulations(simulations = sim, population = myPopulation, simulationRunOptions = simRunOptions)[[1]]
print(populationResults)
```
Simulated time-value pairs for a specific output from the `SimulationResults`-object returned by the `runSimulation` method can be accessed with the method `getOutputValues`. The user can provide either the path(s) of the output (which can be a molecule, a parameter, or an observer),
or the object(s) of the type `Molecule`, `Parameter`, or `Quantity` (for observers) with the argument `quantitiesOrPaths`. If no output is specified, all outputs available in the simulation results are returned.
The paths of all available outputs can be accessed via
```{r getAllOutputSelections}
populationResults$allQuantityPaths
```
`getOutputValues()` returns a list with two entries: `data` and `metadata`:
* `data` is a dataframe with two predefined columns (IndividualId and Time) as well as one column for each requested output
* `IndividualId`
* `Time` a vector with simulated time values (in minutes, equal for all outputs)
* a vector with simulated entries for each output requested.
The values of `IndividualId`, `Time`, and the simulated outputs, are appended for each simulated individual. Note that this results in non-monotonously increasing column `Time`.
```{r getOutputValues}
# Get simulated results by path
resultsPath <- populationResults$allQuantityPaths[[1]]
print(resultsPath)
resultsData <- getOutputValues(populationResults, quantitiesOrPaths = resultsPath)
resultsTime <- resultsData$data$Time
resultsValues <- resultsData$data$`Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)`
plot(resultsTime, resultsValues, type = "l")
```
To get the results for a specific individual or a set of individuals, the argument `individualIds` of the method `getOutputValues()` can be specified:
```{r getOutputValuesForIndividual}
# Get simulated results by path
resultsPath <- populationResults$allQuantityPaths[[1]]
print(resultsPath)
# Get only the results for individuals with IDs 1 and 2
resultsData <- getOutputValues(populationResults, quantitiesOrPaths = resultsPath, individualIds = c(1, 2))
resultsTime <- resultsData$data$Time
resultsValues <- resultsData$data$`Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)`
plot(resultsTime, resultsValues, type = "l")
```
For more information about running simulations, please refer to [Running simulations and retrieving the results](run-simulation.html).