##### Slidedeck e2: Handling Proportions ##### source("http://rfs.kvasaheim.com/stat200.R") # 261 ito1 = "#E1EBEE" ito2 = "#00AAE4" ito3 = "#ffdddd" ito4 = "#990000" ito = c(ito1,ito2,ito3,ito4) ht=250 wt=900 ##### Ex 1: Coins n = 10 p = 0.500 xx = 0:n yy = dbinom(xx, size=n, prob=p) # graphic 1a par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,10.5), ylim=c(0,0.3)) axis(1, at=0:10) title(xlab="Number of Heads", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg=ito2) # graphic 1b par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,10.5), ylim=c(0,0.3)) axis(1, at=0:10) title(xlab="Number of Heads", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg=ito2) segments(3,0, 3,yy[4], lwd=3) points(3,yy[4], pch=21, bg=ito4) # graphic 1c par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,10.5), ylim=c(0,0.3)) axis(1, at=0:10) title(xlab="Number of Heads", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg=ito2) segments(3,0, 3,yy[4], lwd=3) points(3,yy[4], pch=21, bg=ito4) segments(7,0, 7,yy[8], lwd=3) points(7,yy[8], pch=21, bg=ito4) # graphic 1d par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,10.5), ylim=c(0,0.3)) axis(1, at=0:10) title(xlab="Number of Heads", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg=ito2) for(i in 0:3) { segments(i,0, i,yy[i+1], lwd=3) points(i,yy[i+1], pch=21, bg=ito4) } for(i in 7:10) { segments(i,0, i,yy[i+1], lwd=3) points(i,yy[i+1], pch=21, bg=ito4) } segments(3,0, 3,yy[4], lwd=3) points(3,yy[4], pch=21, bg=ito4) segments(7,0, 7,yy[8], lwd=3) points(7,yy[8], pch=21, bg=ito4) ##### Ex 2: Juniors n = 100 p = 0.250 xx = 0:n yy = dbinom(xx, size=n, prob=p) par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,n+0.5), ylim=c(0,1.1*max(yy))) axis(1, at=seq(0,n,10)) title(xlab="Number of Juniors", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg=ito2) ##### Ex 3: SCU n = 661 p = 0.280 xx = 0:n yy = dbinom(xx, size=n, prob=p) par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(-0.5,n+0.5), ylim=c(0,1.1*max(yy))) axis(1, at=seq(0,n,50)) title(xlab="Number of First-Years", line=2.25) segments(xx,0, xx,yy) points(xx,yy, pch=21, bg="black") points(xx,yy, pch=20, col=ito2) ##### Ex 4: Hat Trick obs = 1.604278 par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,0,0,0.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,3.5), ylim=c(0,2)) axis(1, at=seq(0,5,0.5)) title(xlab="Hat Differential", line=2.25) xx = seq(0,5, length=1e4) yy = dchisq(xx, df=1) polygon( x=c(xx, rev(xx)), y=c(yy, rep(0,length(yy))), col=ito1 ) xx = seq(obs,5, length=1e4) yy = dchisq(xx, df=1) polygon( x=c(xx, rev(xx)), y=c(yy, rep(0,length(yy))), col=ito2 ) arrows(2.5,0.6, 2.2,0.19, length=0.1) text(2.5,0.6, pos=4, label="p-value") mtext(side=1, at=obs,text="obs") prop.test(x=c(10,16), n=c(100,100), correct=FALSE) prop.test(x=c(10,16), n=c(100,100)) wald.test(x=c(10,16), n=c(100,100)) ### End of file