prob = 0.2
size = 10
y = rposbinom(n=1000, size, prob)
table(y)
mean(y) # Sample mean
prob / (1-(1-prob)^size) # Population mean
(i = dposbinom(0:size, size, prob))
cumsum(i) - pposbinom(0:size, size, prob) # Should be 0s
table(rposbinom(100, size, prob))
table(qposbinom(runif(1000), size, prob))
round(dposbinom(1:10, size, prob) * 1000) # Should be similar
x = 0:size
plot(x, dposbinom(x, size, prob), type="h", ylab="Probability",
main=paste("Positive-binomial(", size, ",", prob, ") (blue) vs",
" Binomial(", size, ",", prob, ") (red and shifted slightly)", sep=""),
lwd=2, col="blue", las=1)
lines(x+0.05, dbinom(x, size, prob), type="h", lwd=2, col="red")
Run the code above in your browser using DataLab