Probability Histogram
Here we write a simple function to produce a graph for "probability mass function" on the top of histogram. This special function prob.mass() is written as follows:
prob.mass = function(x,prob,...){ for(i in 1:length(x)){ polygon(c(x[i]-0.5,x[i]-0.5,x[i]+0.5,x[i]+0.5), c(0,prob[i],prob[i],0),...) } }
Programming Note.
If the argument “...” is supplied,
partial arguments intended for “...”
will be swallowed and matched as “...”
The function polygon(x,y,...)
draws the polygon surrounded by
.
Random sample. When we obtain the outcomes of random variable from the repeated experiment, the collection of outcomes is called a "random sample." To generate a random sample of size n from a probability mass function prob, we use
data = sample(x,size=n,replace=T,prob=prob)The first argument x is either a positive integer or an array of values
prob = c(1/10,2/10,3/10,4/10) x = length(prob) n = 500 data = sample(x,size=n,replace=T,prob=prob) hist(data, freq=F, breaks=seq(min(x)-0.5,max(x)+0.5,by=1.0), ylim=c(0,max(prob)+0.1), col='green') prob.mass(x,prob,lwd=2,border='red') print(data)
Here we generates the sample x of size n representing the number of outcomes from 1 to 4 according probability prob.
Sample R code. You can download histdemo.R, and run it.
Programming Note. We can introduce a structure of the numeric vector by
vec = c(10.4, 5.6, 3.1, 6.4, 21.7)It can take an arbitrary number of values, and set the length for the variable vec.
© TTU Mathematics