R script: inline.R

$$\newcommand{\esp}[1]{\mathbb{E}\left(#1\right)} \newcommand{\var}[1]{\mbox{Var}\left(#1\right)} \newcommand{\deriv}[1]{\dot{#1}(t)} \newcommand{\prob}[1]{ \mathbb{P}\!(#1)} \newcommand{\eqdef}{\mathop{=}\limits^{\mathrm{def}}} \newcommand{\by}{\boldsymbol{y}} \newcommand{\bc}{\boldsymbol{c}} \newcommand{\bpsi}{\boldsymbol{\psi}} \def\pmacro{\texttt{p}} \def\like{{\cal L}} \def\llike{{\cal LL}} \def\logit{{\rm logit}} \def\probit{{\rm probit}} \def\one{{\rm 1\!I}} \def\iid{\mathop{\sim}_{\rm i.i.d.}} \def\simh0{\mathop{\sim}_{H_0}} \def\df{\texttt{df}} \def\res{e} \def\xomega{x} \newcommand{\argmin}[1]{{\rm arg}\min_{#1}} \newcommand{\argmax}[1]{{\rm arg}\max_{#1}} \newcommand{\Rset}{\mbox{\mathbb{R}}} \def\param{\theta} \def\setparam{\Theta} \def\xnew{x_{\rm new}} \def\fnew{f_{\rm new}} \def\ynew{y_{\rm new}} \def\nnew{n_{\rm new}} \def\enew{e_{\rm new}} \def\Xnew{X_{\rm new}} \def\hfnew{\widehat{\fnew}} \def\degree{m} \def\nbeta{d} \newcommand{\limite}[1]{\mathop{\longrightarrow}\limits_{#1}} \def\ka{k{\scriptstyle a}} \def\ska{k{\scriptscriptstyle a}} \def\kel{k{\scriptstyle e}} \def\skel{k{\scriptscriptstyle e}} \def\cl{C{\small l}} \def\Tlag{T\hspace{-0.1em}{\scriptstyle lag}} \def\sTlag{T\hspace{-0.07em}{\scriptscriptstyle lag}} \def\Tk{T\hspace{-0.1em}{\scriptstyle k0}} \def\sTk{T\hspace{-0.07em}{\scriptscriptstyle k0}} \def\thalf{t{\scriptstyle 1/2}} \newcommand{\Dphi}[1]{\partial_\pphi #1} \def\asigma{a} \def\pphi{\psi} \newcommand{\stheta}{{\theta^\star}} \newcommand{\htheta}{{\widehat{\theta}}}$$

# 1 Introduction

Instead of using an external text file for the model, it is possible to define the model inline, i.e. in the R script with the command

myModel <- inlineModel("
[LONGITUDINAL]
...
")

This model can then be used as any model Mlxtran:

res <- simulx( model=myModel, ... )

The syntax for implementing the model is exactly the same: you can copy and paste any Mlxtran model in the R script, in the comment area defined by (" and "). The only difference is that all the information required for performing a given simulation is in one single R file.

Function inlineModel creates a temporary model file filename. Default name is “tempModel.txt”. filename=“random” generates a random name.

# 2 Example

myModel1 <- inlineModel("
[LONGITUDINAL]
input = {ka, V, k, a}

EQUATION:
D = 100
f = D*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))

DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
")

print(myModel1)
## $filename ## [1] "tempModel.txt" ## ##$str
## [1] "\n[LONGITUDINAL]\ninput = {ka, V, k, a}\n\nEQUATION:\nD = 100\nf = D*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))\n\nDEFINITION:\ny = {distribution=normal, prediction=f, sd=a}\n"

We can now use myModel1.txt with simulx:

f <- list(name='f', time=seq(0, 30, by=0.1))
y <- list(name='y', time=seq(0, 30, by=2))

res <- simulx(model     = myModel1,
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output    = list(f, y))

plot(ggplot(data=res$f, aes(x=time, y=f)) + geom_line(size=0.5) + geom_point(data=res$y, aes(x=time, y=y), colour="red")) 

filename=“random” creates a tempory model file with a random name. This may be useful when several people use siultaneously the same Shiny app for instance.

myModel2 <- inlineModel("
[LONGITUDINAL]
input = {ka, V, k, a}

EQUATION:
f = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))

DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
", filename="random")

print(myModel2)
## $filename ## [1] "tempModel_0be81a5d-1d0b-42c5-b89c-d9076b7f1f7f.txt" ## ##$str
## [1] "\n[LONGITUDINAL]\ninput = {ka, V, k, a}\n\nEQUATION:\nf = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))\n\nDEFINITION:\ny = {distribution=normal, prediction=f, sd=a}\n"
res <- simulx(model     = myModel2,
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output    = list(f, y))

Obviously, the model can be written directly in any text file using basic R command:

model.str <- "
[LONGITUDINAL]
input = {ka, V, k, a}

EQUATION:
f = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))

DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
"

write(model.str,"pk_model.txt")

res <- simulx(model     = "pk_model.txt",
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output    = list(f, y))