# Simulation metamodeling with GNU R

**R snippets**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I am one of the organizers of ESSA2013 conference that will take place in September 2013 in Warsaw, Poland. The conference scope is social simulation and in particular methods of statistical analysis of simulation output (metamodeling). As we have just issued Call for Papers for the conference so I decided to post a simple example of a metamodel.

Recently I had to calculate variance of a random variable given as:

*q*(

*p*-2+

*q*/

*N*),

where *p* and *N* are model parameters and *q* is a random variable that has binomial distribution with *N* Bernoulli trials and probability of success 2-*p*. After some lengthily calculations I came up with the following formula for the variance:

However the result is so long that I was not sure that it was correct, so I decided to verify it using metamodeling.

First I have generated the data for the simulation with the following code and visualized it. The data covers a grid where *p* is from interval [1;2] and *N* spans from 1 to 10. In each grid point 100000 simulations are performed and variance of *q*(*p*-2+*q*/*N*) is calculated:

**(**plyr

**)**

**(**lattice

**)**

**<-**

**function**

**(**param

**)**

**{**

**<-**rbinom

**(**100000, param

**$**N, 2

**–**param

**$**p

**)**

**(**v

**=**var

**((**param

**$**p

**–**2

**+**q

**/**param

**$**N

**)**

*****q

**))**

**}**

**(**1

**)**

**<-**expand.grid

**(**p

**=**seq

**(**1, 2, len

**=**201

**)**, N

**=**1

**:**10

**)**

**<-**ddply

**(**data.set, .

**(**p,N

**)**, sim, .progress

**=**“text”

**)**

**(**v

**~**p

**+**N, data

**=**data.set,

**=**terrain.colors, xlab

**=**“p”, ylab

**=**“N”

**)**

Here is the resulting plot:

Next I have estimated the linear regression model with parameters reflecting analytical specification given above:

**(**lm

**(**v

**~(**p

**+**I

**(**p

**^**2

**)**

**+**I

**(**p

**^**3

**)**

**+**I

**(**p

**^**4

**))**

*** (**N

**+**I

**(**1

**/**N

**))**, data

**=**data.set

**))**

The output looks as follows:

As we can see the model fit is almost perfect and the estimates are very close to analytical results. This reassured me that my calculation was correct.

In this way you could use simulation to verify analytical results. Of course simulation metamodeling has much broader applications – especially when we **do not** have analytical results.

I hope to organize a special session during ESSA2013 dedicated to simulation metamodeling using GNU R. So if you are interested in such topics and willing to promote GNU R in simulation society you are welcome to come to Warsaw.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.