Next: Additional notes: simulation of Up: stat6341 Previous: Example R scripts

# Simulation of random variables

There are several uses for simulation:
1. Demonstration of statistical or probabilistic effects.
2. Understanding the responses of structures during their design to ranges of environmental or operational conditions so that their designs can be optimized. Examples of this application include aircraft and ship design, communication networks. This approach allows designers to develop and test products without building prototypes for testing. It also provides opportunities for designers to observe behaviors under extreme conditions that would be impossible or extremely expensive to create for physical prototypes.
3. Simulation of theories for the behavior of physical systems. Simulation results can be compared to observations for validation of theories. Examples include climate models, models for astronomical phenomena such as solar system formation and galactic evolution, and models for chemical reactions that can accelerate development of new drugs or vaccines.
4. Comparison of statistical methods under a variety of conditions and models. This is a common method for the comparison of new methods to existing methods when analytical comparisons are difficult or impossible to obtain.

One of R's strengths is its extensive library of functions for simulation of random variables. This includes the following distributions.

Discrete r.v.'s:
Distribution Basename
binomial binom
geometric geom
hypergeometric hyper
multinomial multinom
negative binomial negbin
Poisson pois

Continuaous r.v.'s:
Distribution Basename
beta beta
Cauchy (with location and scale) cauchy
chi-squared chisq
exponential exp
gamma gamma
F f
log-normal lnorm
normal norm
t t
uniform unif
Weibull weibull
Other distributions are available in contributed packages.

For each distribution there are functions in R to generate the cdf, density or pmf, quantiles, and random samples. These functions use the following convention: dname returns the density or probability mass function, pname returns the cdf, qname returns quantiles, and rname returns random samples. For example,

```n = 200
mu = 100
sig = 20
X = rnorm(n,mu,sig)
X.hist = hist(X,plot=FALSE)
x0 = seq(mu-4*sig,mu+4*sig,length=250)
d0 = dnorm(x0,mu,sig)
y0 = d0*n*unique(diff(X.hist\$breaks))
y.lim = max(c(y0,X.hist\$counts))
png("NormalDens.png",width=480,height=480)
plot(X.hist,col="cyan",ylim=c(0,y.lim),xlim=mu+c(-4,4)*sig,main="")
title(paste("Histogram of N(",mu,",",sig,")",sep=""))
mtext("with Density Function",side=3,line=.25)
lines(x0,y0,col="red")
graphics.off()
```

Random number generators must be initialized before they are used. Ordinarily this is done internally by R. However, there are situations in which a repeatable sequence of generated values is required, for example, when testing code. In this case, we must initialize the RNG by specifying what is referred to as the seed. In R this is accomplished by the function set.seed(seed) where seed is an integer. Specifying this before using one of the r-functions listed above will produce the same set of values.

Subsections

Next: Additional notes: simulation of Up: stat6341 Previous: Example R scripts
ammann
2017-12-10