MATLAB Answers

How can I perform a chi-square test to determine how statistically different two proportions are in Statistics Toolbox 7.2 (R2009b)?

607 views (last 30 days)
As an example, if I have the two proportions 51 out of 8193 versus 74 out of 8201, how can I determine how statistically different they are?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Feb 2013
There are functions in MATLAB that can perform the necessary chi-square test to determine two proportions' statistical difference, but the input data must be altered first.
The functions that can be used are CROSSTAB and CHI2GOF. Both of these functions expect raw data, while the data that we have mentioned (proportions 51 out of 8193 versus 74 out of 8201) are already summarized. Given "summarized" data, there is no function in MATLAB that will perform this chi-square test directly on these four data elements. However, if you would like to perform this test manually, there are three ways to do this.
The first way would be to artificially generate "raw" data from this "summarized" data, and then use CROSSTAB on the "raw" data as follows:
% Observed data
n1 = 51; N1 = 8193;
n2 = 74; N2 = 8201;
x1 = [repmat('a',N1,1); repmat('b',N2,1)];
x2 = [repmat(1,n1,1); repmat(2,N1-n1,1); repmat(1,n2,1); repmat(2,N2-n2,1)];
[tbl,chi2stat,pval] = crosstab(x1,x2)
tbl =
51 8142
74 8127
chi2stat =
4.2419
pval =
0.039437
A second method would be to directly perform the chi-square test as follows:
% Observed data
n1 = 51; N1 = 8193;
n2 = 74; N2 = 8201;
% Pooled estimate of proportion
p0 = (n1+n2) / (N1+N2)
% Expected counts under H0 (null hypothesis)
n10 = N1 * p0;
n20 = N2 * p0;
% Chi-square test, by hand
observed = [n1 N1-n1 n2 N2-n2];
expected = [n10 N1-n10 n20 N2-n20];
chi2stat = sum((observed-expected).^2 ./ expected)
p = 1 - chi2cdf(chi2stat,1)
p0 =
0.0076
chi2stat =
4.2419
p =
0.0394
A third method would be to use CHI2GOF in a similar manner. Even though it expects raw data, you can provide frequencies instead. However, you have to find the expected counts manually.
% Observed data
n1 = 51; N1 = 8193;
n2 = 74; N2 = 8201;
% Pooled estimate of proportion
p0 = (n1+n2) / (N1+N2)
% Expected counts under H0 (null hypothesis)
n10 = N1 * p0;
n20 = N2 * p0;
% Chi-square test, by hand
observed = [n1 N1-n1 n2 N2-n2];
expected = [n10 N1-n10 n20 N2-n20];
[h,p,stats] = chi2gof([1 2 3 4],'freq',observed,'expected',expected,'ctrs',[1 2 3 4],'nparams',2)
h =
1
p =
0.039437
stats =
chi2stat: 4.2419
df: 1
With all three approaches, we can see that the chi-square statistic is 4.2419, and the corresponding p-value is 0.0394. These figures let us know how statistically different (or similar) two proportions are.
  3 Comments
Tzila Penesh
Tzila Penesh on 25 Mar 2020
I couldn't understand the comment (of Eric T).
When I want to check more than 2 proportions what input (chi2stat) do I put into the function(chi2cdf)?

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!