knitr::opts_chunk$set(echo = TRUE)
getwd()
mybin=function(iter=100,n=10, p=0.5){ # make a matrix to hold the samples #initially filled with NA's sam.mat=matrix(NA,nr=n,nc=iter, byrow=TRUE) #Make a vector to hold the number of successes in each trial succ=c() for( i in 1:iter){ #Fill each column with a new sample sam.mat[,i]=sample(c(1,0),n,replace=TRUE, prob=c(p,1-p)) #Calculate a statistic from the sample (this case it is the sum) succ[i]=sum(sam.mat[,i]) } #Make a table of successes succ.tab=table(factor(succ,levels=0:n)) #Make a barplot of the proportions barplot(succ.tab/(iter), col=rainbow(n+1), main="Binomial simulation", xlab="Number of successes") succ.tab/iter }
mybin(100,10,0.7)
mybin(200,10,0.7)
mybin(500,10,0.7)
mybin(1000,10,0.7)
mybin(10000,10,0.7)
dbinom(0:10,10,0.7)
freq <- c(12,8) options <- c(1,0) bag_o_marbles <- rep(options, freq) sample(bag_o_marbles,5,replace = F)
sample(bag_o_marbles,5,replace = T)
myhyper=function(iter=100,N=20,r=12,n=5){ # make a matrix to hold the samples #initially filled with NA's sam.mat=matrix(NA,nr=n,nc=iter, byrow=TRUE) #Make a vector to hold the number of successes over the trials succ=c() for( i in 1:iter){ #Fill each column with a new sample sam.mat[,i]=sample(rep(c(1,0),c(r,N-r)),n,replace=FALSE) #Calculate a statistic from the sample (this case it is the sum) succ[i]=sum(sam.mat[,i]) } #Make a table of successes succ.tab=table(factor(succ,levels=0:n)) #Make a barplot of the proportions barplot(succ.tab/(iter), col=rainbow(n+1), main="HYPERGEOMETRIC simulation", xlab="Number of successes") succ.tab/iter }
myhyper(iter=100,n=5, N=20,r=12)
myhyper(iter=200,n=5, N=20,r=12)
myhyper(iter=500,n=5, N=20,r=12)
myhyper(iter=1000,n=5, N=20,r=12)
myhyper(iter=10000,n=5, N=20,r=12)
dhyper(x=0:5, m=12, n=8,5)
It is a function that takes 3 arguments:
n which is the amount of choices to make from the vector of numbers 1 through 10 (which is defined locally)
iter which is the number of iterations to make
time which is the amount of time between iterations
It samples a vector of values 1 through 10 and displays a chart of each iteration, with a small pause.
If I want to sample a vector of values 1 through 10 choosing 6 and running 1000 iterations while pausing 1 second per iteration I would call mysample(6, 1000, 1)
I'm given 30 plots which Seem to show that the distribution is approximately even.
mysample=function(n, iter=10,time=0.5){ for( i in 1:iter){ #make a sample s=sample(1:10,n,replace=TRUE) # turn the sample into a factor sf=factor(s,levels=1:10) #make a barplot barplot(table(sf)/n,beside=TRUE,col=rainbow(10), main=paste("Example sample()", " iteration ", i, " n= ", n,sep="") , ylim=c(0,0.2) ) #release the table Sys.sleep(time) } } mysample(1000, 1, 1)
choose(8,4)
dpois(4, lambda = 2)
dnbinom(10-3, size = 3, prob = 0.40)
pbinom(8, size = 15, prob = 0.40)
grac0009MATH4753::mysample(5, 1, 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.