Re: [R] Simulating distribution of max of two die
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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Jaap van Wyk Sent: Monday, August 29, 2011 9:57 PM To: r-help@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 - 2 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@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. __ R-help@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.
Re: [R] Simulating distribution of max of two die
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jaap van Wyk Sent: 30 August 2011 04:57 To: r-help@r-project.org Subject: [R] Simulating distribution of max of two die 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? #Perhaps dice - matrix(sample(1:6, 4,replace=T), ncol=2) dice.max - apply(dice, 1, max) barplot(table(dice.max)) #and just for interest mids-barplot(table(dice.max)/length(dice.max)) #gives proportions all.pairs - expand.grid(1:6, 1:6) dice.max.exact - apply(all.pairs, 1, max) points(mids, table(dice.max.exact)/36, pch=_, cex=2, col=2) *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@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.
[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 - 2 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@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.
Re: [R] Simulating distribution of max of two die
Why not d - ifelse( d1 d2, d1, d2 ) or d - pmax( d1, d2 ) ? Apply operations may seem beautiful, but I think the speed and simplicity of vectorized operations are truly beautiful. --- Jeff Newmiller The . . Go Live... DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Jaap van Wyk j.van...@ru.ac.za wrote: 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 - 2 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@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. [[alternative HTML version deleted]] __ R-help@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.