[R] Simulating distribution of max of two die
Greg Snow
Greg.Snow at imail.org
Wed Aug 31 21:03:51 CEST 2011
For this example you can work out every possibility, you can use this in place (or in addition to) the simulations. Here is a quick example with a couple other ways to look at what is happening:
all.rolls <- expand.grid(1:6,1:6)
max.roll <- apply(all.rolls, 1, max)
round(prop.table(table(max.roll)),3)
library(MASS)
fractions( prop.table(table(max.roll)))
print( paste(table(max.roll),'/36',sep=''), quote=FALSE )
library(TeachingDemos)
plot.dice( expand.grid(1:6,1:6), layout=c(6,6) )
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jaap van Wyk
> Sent: Monday, August 29, 2011 9:57 PM
> To: r-help at r-project.org
> Subject: [R] Simulating distribution of max of two die
>
> Hallo
>
> I am teaching a very elementary intro course about R, and want to show
> the students how to find the distribution of the maximum of rolling
> two balanced die. Is there perhaps a more elegant way to do this,
> other than the way I am using below? (I would like to show them two
> ways - the one shown here, and perhaps and improved/elegant approach
> (showing off the "beauty" of R.
> My code is as follows:
>
> R> n <- 20000
> R> d1 <- sample(1:6, n, replace=TRUE)
> R> d2 <- sample(1:6, n, replace=TRUE)
> R> d <- apply(matrix(c(d1,d2), nrow=n, byrow=TRUE), 1, max)
> R> round(table(d)/n, 3)
> d
> 1 2 3 4 5 6
> 0.030 0.084 0.137 0.195 0.246 0.308
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list