# A binomial probability: dbinom(4, 11, 0.7) #All binomial probabilities with #parameters 11 and 0.7: x <- 0:11 y <- dbinom(x, 11, 0.7) plot(x,y) #Another example: x <- 0:40 y <- dbinom(x, 40, 0.8) plot(x,y) #Its mean ans variance: 40*0.8 40*0.8*0.2 #Compare with normal distribution with #same mean and variance: y2 <- dnorm(x, 32, sqrt(6.4)) lines(x,y2) #Try third example: n <- 20 p <- 0.9 x <- 0:n y <- dbinom(x, n, p) y2 <- dnorm(x, n*p, n*p*(1-p)) plot(x,y) lines(x,y2) ####### #As for any distribution, a histogram of #a large sample will approach the #theoretical probabilities: sample1 <- rbinom(10000, n, p) hist(sample1, breaks=0:(n+1)-0.5, prob=TRUE) points(x, y) ########################## #Keeping n*p constant, but letting #n increase: x <- 0:50 plot(x, dbinom(x, n, p), type="l") #Another example p <- 0.2 plot(x, dbinom(x, n, p), type="l") #Compare with points(x, dpois(x, n*p)) ############################ #Example of the Normal distribution: plot(x, dnorm(x, 20, 5), type="l") abline(v=20) abline(v=20+2*5) ############################### #Getting a chi-square distribution from #normally distributed values: sample3 <- rnorm(10000)^2 + rnorm(10000)^2+rnorm(10000)^2 hist(sample3, breaks=x-0.5, prob=TRUE) points(x, dchisq(x, 3)) plot(x, dchisq(x, 3), type="l") plot(x, dchisq(x, 25), type="l") lines(x, dnorm(x, 25, sqrt(2*25)), col="red") A <- matrix(rnorm(100000), 10000, 10) sample4 <- apply(A, 1, var)*9 hist(sample4, breaks=x-0.5, prob=TRUE) points(x, dchisq(x, 9)) ################################## #The t distribution: x <- (-100):100/10 plot(x, dt(x, 1), type="l") lines(x, dt(x, 3), col="red") lines(x, dt(x, 10), col="yellow") lines(x, dt(x, 100), col="cyan") lines(x, dt(x, 1000), col="blue") lines(x, dnorm(x)) ################################### #Example showing the connection of the t distr. #with the normal and chi-square: sample5 <- rnorm(10000)/sqrt(rchisq(10000, 5)/5) x <- (-20):20 hist(sample5, breaks=x-0.5, prob=TRUE) points(x, dt(x, 5)) ################################### #Examples of F-distributions: x <- 1:100/20 plot(x, df(x, 1, 1), type="l") lines(x, df(x, 1, 3)) lines(x, df(x, 1, 100)) lines(x, df(x, 3, 1)) lines(x, df(x, 3, 10)) lines(x, df(x, 3, 100)) lines(x, df(x, 10, 1)) lines(x, df(x, 10, 10)) lines(x, df(x, 10, 100))