MATLAB Answers

How to do Chi square with contingency tables?

30 ビュー (過去 30 日間)
Danning Zhang
Danning Zhang 2020 年 7 月 23 日
コメント済み: Danning Zhang 2020 年 7 月 23 日
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 件のコメント



Neuropragmatist 2020 年 7 月 23 日
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.

  3 件のコメント

Danning Zhang
Danning Zhang 2020 年 7 月 23 日
Oh Thank you! And what about when I have a 3x2 table, will that work too?
Neuropragmatist 2020 年 7 月 23 日
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:
Danning Zhang
Danning Zhang 2020 年 7 月 23 日
Oh I see, thank you!!


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by