How to do Chi square with contingency tables?
37 views (last 30 days)
Show older comments
Danning Zhang
on 23 Jul 2020
Commented: Danning Zhang
on 23 Jul 2020
Hello,
I have 2 questions regarding doing chi square test in matlab.
1) I have a traditional 2x2 contingency table with rows = disease, no disease; columns = experimental condition, control condition.
How do I run a chi square on this? I read the crosstab function but didn't quite understand it.
2) I have nx2 contingency tables where columns are still experimental vs control condition. The rows are now a list of ethnicities. I would like to know if ethnicity distribution differed between the 2 conditions. How would I go about this?
Many thanks!
0 Comments
Accepted Answer
Neuropragmatist
on 23 Jul 2020
Crosstabs expects the raw data as vectors, if you only have counts you will need to expand your data into vectors, for instance X would equal 1 for disease and 2 for no disease and Y would equal 1 for experimental condition and 2 for control condition.
If you just have the counts or don't want the hassle of expanding your data to vectors you could look at using a Fisher's exact test instead:
x = table([1;2],[3;4],'VariableNames',{'Experimental','Control'},'RowNames',{'Disease','NoDisease'})
I tend to use Fischer's test instead because it is more relaible with small sample sizes.
Alternatively you could look at some files on the file exchange like this one:
Or if I just want a one off calculation I will often use online contingency table calculators instead (not Matlab?! the shock, the horror!):
Hope this helps.
3 Comments
Neuropragmatist
on 23 Jul 2020
The Fischer's test only works on 2x2 tables as far as I know, but you can still use crosstabs for larger tables and it can calculate a chi-square statistic and p-value for you.
If you only have the cross-tabulation (i.e. a table or spreasheet with counts instead of actual data vectors) you will need to convert it back into data vectors for crosstabs to work; you can see this post here for further info on that route:
More Answers (0)
See Also
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!