How to do Chi square with contingency tables?

55 次查看(过去 30 天)
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!

采纳的回答

Neuropragmatist
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 个评论
Neuropragmatist
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:

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by