Dear UseRs I would like to show my students how to use "resampling" to solve the following simple problem: If a family has two children of which one is a boy, what is the probability that the other child is also a boy. The answer is (obviously) 1/3, and can be show easily using the usual methods. But I would like to get the students to think of resampling, by doing the following: Flip two coins repeatedly, denoted 0 and 1 (1 for boy, say). Discard those pairs that both contain 0.>From those left over, count how many pairs are (1,1).Divide this number by the number available to choose from (i.e. all pairs, except (0,0)). This will then give 1/3 (more or less of course). Can somebody help me to code this efficiently, or elegantly (and smartly) in R, without loops etc. It is intended for first year students that are only starting to learn about statistics (or probability theory), and R of course. Thank you for your time. Regards Jacob Jacob L van Wyk Department of Statistics University of Johannesburg, APK P O Box 524 Auckland Park 2006 South Africa Tel: +27 11 489 3080 Fax: +27 11 489 2832 [[alternative HTML version deleted]]
try the following: B <- 10000 index <- rowSums(matrix(rbinom(B*2, 1, 0.5), ncol = 2)) sum(index == 2) / sum(index > 0) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Jacob van Wyk" <jlvw at na.rau.ac.za> To: <r-help at stat.math.ethz.ch> Sent: Friday, September 22, 2006 12:57 PM Subject: [R] A simple resampling problem> Dear UseRs > > I would like to show my students how to use "resampling" to solve > the > following simple problem: > > If a family has two children of which one is a boy, what is the > probability that the other child is also a boy. > The answer is (obviously) 1/3, and can be show easily using the > usual > methods. > But I would like to get the students to think of resampling, by > doing > the following: > Flip two coins repeatedly, denoted 0 and 1 (1 for boy, say). Discard > those pairs that both contain 0. >>From those left over, count how many pairs are (1,1). > Divide this number by the number available to choose from (i.e. all > pairs, except (0,0)). > This will then give 1/3 (more or less of course). > > Can somebody help me to code this efficiently, or elegantly (and > smartly) in R, without loops etc. > It is intended for first year students that are only starting to > learn > about statistics (or probability theory), and R of course. > > Thank you for your time. > Regards > Jacob > > > > > Jacob L van Wyk > Department of Statistics > University of Johannesburg, APK > P O Box 524 > Auckland Park 2006 > South Africa > Tel: +27 11 489 3080 > Fax: +27 11 489 2832 > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Try this: set.seed(1) n <- 100 first <- sample(0:1, n, replace = TRUE) second <- sample(0:1, n, replace = TRUE) sum(first == 1 & second == 1) / sum(first == 1 | second == 1) # The last could be written more compactly like this # as 1 and 0 will be treated as TRUE and FALSE by the # logical operators and then TRUE and FALSE will be # regarded as 1 and 0 by sum sum(first & second) / sum(first | second) On 9/22/06, Jacob van Wyk <jlvw at na.rau.ac.za> wrote:> Dear UseRs > > I would like to show my students how to use "resampling" to solve the > following simple problem: > > If a family has two children of which one is a boy, what is the > probability that the other child is also a boy. > The answer is (obviously) 1/3, and can be show easily using the usual > methods. > But I would like to get the students to think of resampling, by doing > the following: > Flip two coins repeatedly, denoted 0 and 1 (1 for boy, say). Discard > those pairs that both contain 0. > >From those left over, count how many pairs are (1,1). > Divide this number by the number available to choose from (i.e. all > pairs, except (0,0)). > This will then give 1/3 (more or less of course). > > Can somebody help me to code this efficiently, or elegantly (and > smartly) in R, without loops etc. > It is intended for first year students that are only starting to learn > about statistics (or probability theory), and R of course. > > Thank you for your time. > Regards > Jacob > > > > > Jacob L van Wyk > Department of Statistics > University of Johannesburg, APK > P O Box 524 > Auckland Park 2006 > South Africa > Tel: +27 11 489 3080 > Fax: +27 11 489 2832 > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >