urn-package {urn} | R Documentation |
Generate repeated samples of the same list of objects without replacement.
Package: | urn |
Type: | Package |
Version: | 2.1 |
Date: | 2010-1-08 |
License: | AGPL 3.0 |
urn
creates an urn. sampleu
allows a single sample to be taken without replacement. Call usaple when
repeated samples without replacement are needed. Use sum() to determine population
left in urn, and refill.urn to restore population to originial levels.
Micah Altman Micah\_Altman\@harvard.edu http://maltman.hmdc.harvard.edu/
There are many references explaining sampling without replacement, this is one example:
Mathematical Statistics and Data Analysis, John A. Rice. Wadsworth, 1988, 1995.
library(urn) # Create urn with 3 items u<-urn(list("red","green","blue")) # custom print and summary methods print(u ) summary(u) # draw 2 samples from the urn sample(u,2) # can't sample more items than in the urn, without refilling: # sampleu(u,2) sum(u) sample(u,1) # refill refill(u) # Create an urn with 100010 items of two types in ~51:49 proportions ub<-urn(c(51006,49004)) summary(ub) # take ten draws each of 10001 items if (is.R()) { reps<-replicate(10, table(sample(ub,10001)), simplify=TRUE) } else { reps<-sapply(integer(10), function(x)table(sample(ub,10001)), simplify=TRUE) } print(reps) # should equal 51006 sum(reps[1,]) ## Not run: if (sum(reps[1,])!=51006) { warning("self check failed") } ## End(Not run)