urn-package {urn}R Documentation

A package for simulating draws from an urn – sampling without replacement.

Description

Generate repeated samples of the same list of objects without replacement.

Details

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.

Author(s)

Micah Altman Micah\_Altman\@harvard.edu http://maltman.hmdc.harvard.edu/

References

There are many references explaining sampling without replacement, this is one example:

Mathematical Statistics and Data Analysis, John A. Rice. Wadsworth, 1988, 1995.

See Also

sample, urn

Examples


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)


[Package urn version 2.2.1 Index]