function [p, Q]= chi2test(x)
% Usage: [p, Q]= chi2test(x)
%
% The chi-squared test.
%
% Given a number of samples this function tests the hypothesis that the samples are
% independent. If Q > chi2(p, nu), the hypothesis is rejected.
%
% Each column represents a variables, each row a sample.
%
% If you find any errors, please let me know: .
%
% ARGUMENTS:
% x Absolut numbers.
% p The prob ability value, calculated from Q.
% Q The resulting Q-value.
%
% EXAMPLE 1
% In region A, 324 of 556 cows were red, whereas in region B 98 of 260 were red.
% [p, Q]= chi2test([324, 556-324; 98, 260-98])
% p=
% 4.2073e-08
% Q=
% 30.0515
% With an error risk of about 4e-08, we can claim that the samples are independent.
%
% EXAMPLE 2
% Throw two different dices to see if they have the same probability of 1 (and 2, 3, 4, 5, 6).
% We don't check if they are symetrical, only if the both behave in the same way.
% [p,Q] = chi2test([15,10; 7,11; 9,7; 20,15; 26,21; 19,16])
% p=
% 0.8200
% Q =
% 2.2059
% The dices don't significantly behave differently. That is, they seem to behave in the same way.
%
% HISTORY: v.1.0, first working version, 2007-08-30.
%
% COPYRIGHT: (c) 2007 Peder Axensten. Use at own risk.
% KEYWORDS: chi-squared test, chi-squared, chi2, test
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check the arguments.
if(nargin ~= 1), error('One and only one argument required!'); end
if(ndims(x) ~= 2), error('The argument (x) must be a 2d matrix!'); end
if(any(size(x) == 1)), error('The argument (x) must be a 2d matrix!'); end
if(any(~isreal(x))), error('All values of the argument (x) must be real values!'); end
% Calculate Q = sum( (a-np*)^2/(np*(1-p*)) )
s= size(x, 1);
r= size(x, 2);
np= sum(x, 2)/sum(sum(x)) * sum(x); % p=sum(x, 2)/sum(sum(x)) and n=sum(x)
Q= sum(sum((x-np).^2./(np)));
% Calculate cdf of chi-squared to Q. Degrees of freedom, v, is (r-1)*(s-1).
p= 1 - gammainc(Q/2, (r-1)*(s-1)/2);
end
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
卡方检验matlab源程序.rar (1个子文件)
chi2test.m 2KB
共 1 条
- 1
资源评论
- m0_749617862023-11-14支持这个资源,内容详细,主要是能解决当下的问题,感谢大佬分享~
- jieg02023-02-21终于找到了超赞的宝藏资源,果断冲冲冲,支持!
- 2201_761111522023-09-07简直是宝藏资源,实用价值很高,支持!
- 2201_753038992023-03-19这个资源总结的也太全面了吧,内容详实,对我帮助很大。
智慧安全方案
- 粉丝: 3615
- 资源: 59万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功