[R] Simulation help

Richard Pugh rpugh at mango-solutions.com
Sun Feb 22 18:40:31 CET 2004

Try this ...

poisNums <- rpois(100000, 3) # Calculate 100,000 poissons (mean 3)
normNums <- sapply(poisNums, rnorm, mean=3) # Calculate N random normals

	# for each simulated poisson.  Normal distribution is mean 3, sd
sumNums <- sapply(normNums, sum)
mySeq <- seq(0.85, 1, length=50)
plot(seq(0.85, 1, length=50)*100, quantile(sumNums, mySeq), type="l",
main="Quantiles of simulated data", xlab="Quantiles", ylab="")
abline(h=quantile(sumNums, c(0.95, 0.99)), col=2, lwd=2)

R does this for me in about 20 seconds ...


Mango Solutions
Tel : (01628) 418134
Mob : (07967) 808091

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
jonathan_wang at sbcglobal.net
Sent: 22 February 2004 17:18
To: r-help at stat.math.ethz.ch
Subject: [R] Simulation help

I am a new R user.  As a test, I want to write a simple code that does
the following simulation:

1. Randomly generate a number from a distribution, say, Poisson.  Let's
say that number is 3.
2. Randomly generate 3 numbers from another distribution, say, Normal.
3. Compute the sum of the numbers generated in step 2 and read it into a
vector, V.
4. Repeat steps 1 through 3 for 100,000 times.
5. Derive quantiles (e.g., 0.95th, 0.99th) of V.

Any help in getting me going would be greatly appreciated.

	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

Incoming mail is certified Virus Free.

More information about the R-help mailing list