contincy.m

来自「一个非常实用的统计工具箱」· M 代码 · 共 42 行

M
42
字号
function [p, t] = contincy(observed, method)%CONTINCY  Compute the p-value for contigency table row/col independence.%%	   p = contincy(observed, method)%%	   The table  observed  is a count, the  method  is%%	   'chi2': Pearson chi2-distance %	   'logL': Log likelihood quote distance%%	   with default method 'chi2'. The p-value is computed through %	   approximation with chi-2 distribution under the null hypothesis%	   for both methods.%%          See also CAT2TBL%       GPL Copyright (c) Anders Holtsberg, 1999if nargin < 2   method = 'chi2';endif any(any(observed~=round(observed) | observed<0))   error('CONTINCY expects counts, that is nonnegative integers')endrowsum = sum(observed')';colsum = sum(observed);n = sum(rowsum);expected = rowsum * colsum ./ n;if strcmp(method, 'chi2')   t = sum(sum((expected-observed).^2 ./ expected));elseif strcmp(method, 'logL')   I = find(observed>0);   t = 2 * sum(observed(I) .* (log(observed(I)./expected(I))));else   error('unknown method')endp = 1 - pchisq(t, (length(rowsum)-1) * (length(colsum)-1));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?