[R] family question

ben@zoo.ufl.edu ben at zoo.ufl.edu
Wed Aug 30 21:38:35 CEST 2000


  Seems like a very odd setup but it does seem as though the code would do
what you wanted.  Also seems as though you could do some combinatorics to
get the answer much more efficiently if you wanted to ... (i.e., write
down the probabilities of having families that were (1 boy, 0 girl = 50%)
(2 boy, 1 girl = dbinom(2,3,0.5) = 3/8), (3 boys, 2 girls), (4 boys, 3
girls), etc., and then divide by the sum.  Since you add children one at a
time and stop when sum(n1)>sum(n2), you will always have one more boy than
girl in the family ...) [That says, by the way, that you won't get equal
numbers of boys and girls.  You will get between 100% and 50% boys, closer
to 100%].

  However, after some playing (see slightly augmented code below), it also
seems clear that there's something Troels and I are both missing, or a bug
in the rbinom code (??) [or a failure of low-order bits to be
uncorrelated?]; the distribution of family sizes has some odd
characteristics (all family sizes are odd, as expected, but there are more
large families -- and some that reach the maximum, which is unexpected).

  So are we both being boneheaded?

  

NF <- 1000
ND <- NP <- NULL #ND - number boys, P: girls
maxfam <- 1000
famdist <- numeric(maxfam)
for (j in 1:NF) # number of families
{
  cat(j,"\n")
#  n1 <- n2 <- NULL
  n1 <- n2 <- numeric(maxfam)
  for (i in 1:maxfam)  {
    if (i %% 100 == 0) cat("* ",i,"\n")
    n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys
    n2[i] <- 1- n1[i] # and girls
    if(sum(n1)>sum(n2)) break
  }
  famsize <- sum(n1)+sum(n2)
  famdist[famsize] <-   famdist[famsize]+1
  ND[j] <- sum(n1)
  NP[j] <- sum(n2)
}
sum(ND)/sum(NP)
j


f.odd <- famdist[(1:maxfam) %% 2 == 1]
f.even <- famdist[(1:maxfam) %% 2 == 0]

plot(f.odd[1:50])

On Wed, 30 Aug 2000, Troels Ring wrote:

> Dear friends. Please see the program below and answer if it does simulate a 
> population of 1.000.000 families, each with at max 20000 children (typical 
> in Denmark, you know), constructed such that each family stops having 
> children when more boys than girls are present ? Equal numbers of boys and 
> girls are got in the population, according to the simulation, is that obvious ?
> 
> ND <- NP <- NULL #ND - number boys, P: girls
> for (j in 1:1000000) # number of families
> {
> n1 <- n2 <- NULL
> for (i in 1:20000)  {
> n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys
> n2[i] <- 1- n1[i] # and girls
> if(sum(n1)>sum(n2)) break
> }
> ND[j] <- sum(n1)
> NP[j] <- sum(n2)
> }
> sum(ND)/sum(NP)
> j
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 
> 

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list