#X2 & G Tests for Association in RxC Contingency Tables #Using the Sex of the Dog (R) by Coat Color (C) example data table #Enter the RxC Contingency as a matrix: X=matrix(c(158,200,46,46,170,282,51,47),nrow=2,byrow=T) X chisq.test(X) #Calculate G Test Statistic, assigning O as observed and E as expected frequencies O=matrix(chisq.test(X)$observed) O E=matrix(chisq.test(X)$expected) E G=2*sum(O*log(O/E)) G #Determine the Critical Value using the qchisq() function #Set the Type 1 Error to alpha = 0.05 #Input the R = # Rows and C= # Columns to calculate the df alpha=0.05 R=2 C=4 df=(R-1)*(C-1) CV=qchisq(1-alpha,df) CV #Calculate the Probability for the G and X Test Statistic: PG=(1-pchisq(G,df)) PG Xsq=sum(((O-E)^2)/E) PX=(1-pchisq(Xsq,df)) PX #CREATE A RxC CONTINGENCY TABLE IN R FORMAT #Upload and save the dataset MBML.RxC into your working directory RxC=read.table("c:/R/MBML.RxC.txt") RxC attach(RxC) #Use the table function and input the (Row, Column) categorical variables #Check the data with the chisq.test() function T=table(Gender,Eye.Color) T chisq.test(T)