# Simulation

## Simulation of a hierarchical model

In What is a model? A joint probability distribution! Section we proposed to consider a model as a joint probability distribution of random variables. Furthermore, the hierarchical structure of a model allows us to decompose this joint distribution into a product of conditional and marginal distributions.

Thus, if a model includes observations $\by$, individual parameters $\bpsi$, a design made up of observation times $\bt$ and source terms (e.g., doses) $\bu$, individual covariates $\bc$ and population parameters $\theta$, the joint distribution of these random variables can be written:

Simulating this model means successively drawing the variables of the model using their associated submodel:

- The population parameters $\theta$ from the distribution $\qth$.
- The individual covariates $\bc$ from the distribution $\qc$.
- The individual parameters $\bpsi$ from the distribution $\qcpsithc$ using the values of $\theta$ and $\bc$ obtained in steps 1 and 2.
- The dose regimen $\bu$ from the distribution $\qu$.
- The measurement times $\bt$ from the distribution $\qt$.
- Lastly, observations $\by$ from the distribution $\qcypsiut$ using the values of $\bpsi$, $\bu$ and $\bt$ obtained at steps 3, 4 and 5.

Evidently, if certain variables are not considered random and are given, the associated simulation step is not performed.

We have already seen that for other tasks (model exploration, estimation) that $\mlxtran$ is well-adapted to implementing this type of hierarchical model because it can deal with this structure by defining the various submodels in easy-to-read blocks. It is also possible to use $\mlxtran$ like a function from within R and Matlab. This capability means that we can write our own R or Matlab script in which the design is defined, the simulation function is called and the results are used for plotting figures, performing tests, etc. This solution combines the flexibility of the R and Matlab environments with the ability of $\mlxtran$ to easily encode complex models.

## Examples

The joint model ${\cal M}_2$ detailed in the Model selection Section can be implemented with $\mlxtran$. We suppose that there can be either simple or multiple drug administrations either orally adm=1) or by iv (adm=2). The model is "ready" to accept these different dose regimens, which are defined either within the data (for estimation) or the simulation script.

Here is an example of a Matlab script that uses the joint1_model.txt model to simulate data for 10 patients. In this example, a dose of 50mg is administered every 24 hours over a 7 day period. The simulated data is stored in a cell array. We can then use exploredatapx for visualizing it.

If we preferred, we could instead use R to run exactly the same simulations using exactly the same $\mlxtran$ model:

It is also possible to include in the model a statistical model for the population parameters or the covariates (here, the weight) by adding sections [POPULATION] and/or [COVARIATE] to the $\mlxtran$ model. For example, we could add the following code to the previous $\mlxtran$ code: