1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
# SampleWallenius.R
# This demo makes random samples from Wallenius' noncentral hypergeometric
# distribution and compares measured and expected frequencies
require(BiasedUrn)
require(stats)
MakeSamples <- function(m1, m2, n, odds) {
nsamp <- 100000 # Desired number of samples from distribution
xmin <- minHypergeo(m1, m2, n) # Lower limit for x
xmax <- maxHypergeo(m1, m2, n) # Upper limit for x
# Make nsamp samples from Wallenius' distribution
X <- rWNCHypergeo(nsamp, m1, m2, n, odds)
# Get table of frequencies
XTab <- as.data.frame(table(X))
# Relative frequencies
XTab$Freq <- XTab$Freq / nsamp
# Get expected frequencies
XTab$Expected <- dWNCHypergeo(as.integer(levels(XTab$X)), m1, m2, n, odds)
print("X frequencies in Wallenius' noncentral hypergeometric distribution")
# List measured vs. expected frequencies
# (How do I get rid of the row names?)
print(XTab, digits=5)
# Draw histogram
# (Why does my histogram show densities bigger than 1?)
hist(X, freq=FALSE)
}
MakeSamples(6, 8, 5, 1.5)
|