Question:
Does matlab have a function for chi-square test for two samples, like kstest2?
There is a function called "CHI2GOF", but I haven't figured out how to use it to the 2 sample case.
"Arthur Zheng" <hzh...@gatech.edu> wrote in message <i5m7ns$cuo$1...@fred.mathworks.com>...
"Joel" <espo...@usna.edu> wrote in message <i5n262$70u$1...@fred.mathworks.com>...
You can use CHI2GOF, but it's really intended more for testing goodness
of fit of a single sample against a distribution family. You're doing
something more like contingency table analysis, which is what CROSSTAB
is for. Not sure what form your data are in, but any of these should work:
% cook up some sample data
k = 5;
p = rand(1,k); p = p./sum(p);
M = 200; N = 250;
x = randsample(1:k,M,true,p); m = histc(x,1:k);
y = randsample(1:k,N,true,p); n = histc(y,1:k);
% Do the test by hand
phat = (m+n) ./ (M+N);
em = phat*M; en = phat*N;
chi2 = sum(([m n] - [em en]).^2 ./ [em en]);
df = k-1;
pval = 1 - chi2cdf(chi2,df);
% Trick CHI2GOF into doing a two sample test. Note the
% nparams value must be such that 2*k - nparams - 1 = k-1
[~,pval,stats] = chi2gof(1:10,'ctrs',1:10,'freq',[m n], ...
'expected',[em en],'nparams',k, 'emin',0)
% Use CROSSTAB
[tbl,chi2,pval] = crosstab([x y],[ones(size(x)) 2*ones(size(y))])
Hope this helps.