# In this lab, we will look at three of the distributions we've been talking
# about in class; binomial, poisson, normal. But before that we'll also
# learn how to make *density scale* histogram.
##########################################################################
# 1) Relative frequency histogram:
dat = read.table("http://www.stat.washington.edu/marzban/390/hist_dat.txt",
header=F)
x = dat[,1]
# Recall that a density scale histogram is a relative frequency histogram where
# the y-axis is also divided by the bin size. R does it like this:
par(mfrow=c(1,2))
hist(x)
hist(x,freq=FALSE)
# You can see that the shape is the same, but the advantage of the density
# is that the area under it is 1.
# By the way, since by default R takes the binsizes to be constant, the
# above density histogram is also a relative frequency histogram.
##########################################################################
# 2) Binomial: The mass function itself.
# First let's compute the binomial proportions in lecture 4 (example 1.22).
# In R's convention, putting a "d" before the name of a distribution
# returns the value of the distribution itself. E.g.,
dbinom(0, 100, 0.005)
# The format is dbinom(x, n, pi), where in the lecture's notation,
# x = number of heads out of n tosses of a coin, and pi= prob of head.
# R allows you to run dbinom() for *multiple* values of x, using ":":
dbinom(0:3, 100, 0.005)
sum( dbinom( 0:3, 100, 0.005) )
# sum of the above probs.
# Compare the above with what we got in lecture 4.
# Replacing the "sum" with "plot" will plot the numbers:
plot( dbinom( 0:3, 100, 0.005) )
# Note no need to specify x explicitly.
# So, now we can also plot the mass function for different values
# of n and pi. Note the n and pi values that produce normal-looking
# distributions, and those that produce poisson-looking distributions.
par(mfrow=c(3,4))
plot(dbinom(0:20,5,0.01),type="b")
#n=5, pi=0.01
No need to specify x
plot(dbinom(0:20,5,0.1),type="b")
#n=5, pi=0.1
USE UP-ARROW
plot(dbinom(0:20,5,0.5),type="b")
#n=5, pi=0.5
USE UP-ARROW