[R] Testing Two Categorical Variable

ramin.1981 at gmail.com ramin.1981 at gmail.com
Thu Dec 6 23:05:48 CET 2007


The chi-square does not need your two categorical variables to have equal levels, nor limitation for the number of levels.

The Chi-square procedure is as follow:
χ^2=∑_(All Cells)▒〖(Observed-Expected)〗^2/Expected

Expected Cell= E_ij=n((i^th RowTotal)/n)((j^th RowTotal)/n)

Degree of Freedom=df= (row-1)(Col-1)

This way should not give you any errors if your calculations are all correct. I usually use SAS for calculations like this. Below is a sample code I wrote to test whether US_State and Blood type are independent. You can modify it for your data and should give you no error.

data bloodtype;
input bloodtype$ state$ count@@;
datalines;
A FL 122 B FL 117
AB FL 19 O FL 244
A IA 1781 B IA 351
AB IA 289 O IA 3301
A MO 353 B MO 269
AB MO 60 O MO 713
;
proc freq data=bloodtype;
tables bloodtype*state
/ cellchi2 chisq expected norow nocol nopercent;
weight count;
quit;


Best
Ramin



More information about the R-help mailing list