##### Exaamples for Chi-square GoF test ##### ##### Slidedeck e4 source("https://rfs.kvasaheim.com/stat200.R") ito1 = "#E1EBEE" ito2 = "#00AAE4" ito3 = "#ffdddd" ito4 = "#990000" ito = c(ito1,ito2,ito3,ito4) ##### ##### ##### ##### ### Framing Example obs = c(180, 215, 205) exp = c(200, 200, 200) TS = sum( (obs-exp)^2/exp ) TS 1-pchisq(TS, df=2) # Or chisq.test(x=c(180,215,205), p=c(1/3, 1/3, 1/3)) # graphic 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,12), ylim=c(0,0.5) ) axis(1, at=0:10*2) title(xlab="Test Statistic", line=2.5) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=2) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[1] ) # xx=seq(TS,15,length=1e4) yy=dchisq(xx,df=2) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[2] ) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=2) lines(xx,yy) arrows( 10,0.2, 6, 1.5*dchisq(6,df=2), length=0.1 ) text(10, 0.2, label="p-value", pos=3) mtext(side=1, at=TS, text="obs") ##### ##### ##### ##### ### Example 1: Car Origin obs = c(19, 23, 2) exp = c(44/3, 44/3, 44/3) TS = sum( (obs-exp)^2/exp ) TS 1-pchisq(TS, df=2) # Or chisq.test(x=c(19,23,2)) # graphic 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,12), ylim=c(0,0.5) ) axis(1, at=0:10*2) title(xlab="Test Statistic", line=2.5) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=2) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[1] ) # xx=seq(TS,15,length=1e4) yy=dchisq(xx,df=2) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[2] ) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=2) lines(xx,yy) arrows( 10,0.2, 12,0.05, length=0.1 ) text(10, 0.2, label="p-value", pos=3) ##### ##### ##### ##### ### Example 2: Quo Vadis obs = c(13, 7, 10, 5) exp = c(14, 7, 7, 7) TS = sum( (obs-exp)^2/exp ) TS 1-pchisq(TS, df=3) chisq.test(x=obs,p=c(14,7,7,7)/35) # graphic 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,12), ylim=c(0,0.25) ) axis(1, at=0:10*2) title(xlab="Test Statistic", line=2.5) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=3) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[1] ) # xx=seq(TS,15,length=1e4) yy=dchisq(xx,df=3) polygon( x=c(xx,rev(xx)), y=c(yy,rep(0,length(xx)) ), col=ito[2] ) # xx=seq(0,15,length=1e4) yy=dchisq(xx,df=3) lines(xx,yy) arrows( 10,0.2, 6, 1.1*dchisq(6,df=3), length=0.1 ) text(10, 0.2, label="p-value", pos=3) mtext(side=1, at=TS, text="obs") ### Example 3: Knox Race and Ethnicity obs = c(109, 73, 214, 635) n=sum(obs) exp = c(0.1606, 0.0709, 0.2312, 0.5389)*n TS = sum( (obs-exp)^2/exp ) TS 1-pchisq(TS, df=3) chisq.test(x=c(109, 73, 214, 635), p=c(0.1606, 0.0709, 0.2312, 0.5389)) chisq.test(x=c(109, 73, 214, 635), p=c(0.1606, 0.0709, 0.2312, 0.5389), rescale.p = TRUE) # graphics par(bg="transparent") par(yaxs="i", xpd=NA, yaxt="n") par(family="serif",las=1) par(mar=c(3.5,2,0.5,2.5)) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0.5,4.5), ylim=c(0,0.80) ) title(xlab="Race/Ethnicity", line=2.5) segments(0.5,0, 4.5,0) segments(0.5,1:4/4, 4.5,1:4/4, lty=3, col="grey") axis(1, at=1:4, labels=c("Black", "Asian", "Hispanic", "White")) mtext(side=2, at=0:4/4, text=c("0.00","0.25","0.50","0.75","1.00"), line=0, las=1) mtext(side=4, at=0:4/4, text=c("0.00","0.25","0.50","0.75","1.00"), line=0, las=1) for( i in 1:4 ) { ee = round( exp[i] ) ccii = binom.test(x=ee, n=n, conf.level=1-(0.05/4))$conf.int mmm=0.25 rect(i-mmm, ccii[1],i+mmm, ccii[2], col=ito1) mmm=0.4 segments (i-mmm, ee/n, i+mmm, ee/n, lwd=3) } points(obs/n, pch=21, bg=ito2, cex=1.4) #binom.plot( x=obs, names=c("Black", "Asian", "Hispanic", "White"), boxcol=ito[2], conf.level=1-0.5^4 ) ### End of File