[R] A simple resampling problem
ggrothendieck at gmail.com
Fri Sep 22 13:23:39 CEST 2006
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
> 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.
> 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
> 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