MATLAB Answers

How to do Chi square with contingency tables?

66 views (last 30 days)
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!

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'})
[h,p,stats] = fishertest(x)
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by