### Class examples from Week 5: Probability and the Normal Curve ### Ken Benoit Feb 2010 ## Tossing two coins examples rbinom(100,2,.5) # this randomly generates 100 tosses of 2 coins # where Pr(outcome=1) is 0.5 # Table of proportions of 10 cosses of 2 coins with Pr=0.5 table(rbinom(10,2,.5))/10 # Table of proportions of 1000 cosses of 2 coins with Pr=0.5 table(rbinom(1000,2,.5))/1000 # Histogram of the outcomes of tosses, N=50 barplot(table(rbinom(50,2,.5))/50) # Histogram of the outcomes of tosses, N=1000 barplot(table(rbinom(1000,2,.5))/1000) # Means of frequency distributions of 2 coin tosses mean(rbinom(10,2,.5)) mean(rbinom(100,2,.5)) mean(rbinom(1000,2,.5)) mean(rbinom(10000,2,.5)) # compute some areas under the normal curve 1-pnorm(1.4) round((1-pnorm(1.4))*100, 2) .50-pnorm(1.33) 1-pnorm(1.33) pnorm(1.33) - pnorm(-1.33) # plot a normal curve x <- seq(-5, 5, 0.1) # numeric sequence every .1 from 5 to 5 plot(x, dnorm(x), type="l") # plot (standard) normal function for x # plot a normal curve with shaded areas -1.33 and +1.33 x <- seq(-4,4,.1) plot(x, dnorm(x), type="n", xaxs="i", yaxs="i", ylim=c(0,.4), bty="l", xaxt="n") polysection <- function(a,b,dist=dnorm,col="blue",n=11) { dx <- seq(a,b,length.out=n) polygon(c(a,dx,b),c(0,dist(dx),0),border=NA,col=col) } polysection(-5,-1.33, col="gray") polysection(1.33, 5, col="gray") axis(1, c(-5,-1.33, 0, 1.33,5)) lines(x,dnorm(x)) # simple example of how to standardize scores x <- c(1,4, 5, 7, 14, 0, 21) (stdx <- (x - mean(x)) / sd(x)) # the long way scale(x) # short-cut way # check normality with a Q-Q plot load("dail2002.Rdata") attach(dail2002) qqnorm(spend_total) qqline(spend_total) detach(dail)