【发布时间】:2016-01-13 23:07:34
【问题描述】:
我需要对 n x m 矩阵进行 Fisher 精确检验。我一直在寻找几个小时,我只找到了一个示例代码,但它是用 Fortran 编写的。我一直在用 Wolfram 工作,我快要完成了,但我错过了最后一点。
/**
* Performs Fisher's Exact Test on a matrix m x n
* @param matrix Any matrix m x n.
* @return The Fisher's Exact value of the matrix
* @throws IllegalArgumentException If the rows are not of equal length
* @author Ryan Amos
*/
public static double getFisherExact(int[][] matrix){
System.out.println("Working with matrix: ");
printMatrix(matrix);
for (int[] array : matrix) {
if(array.length != matrix[0].length)
throw new IllegalArgumentException();
}
boolean chiSq = matrix.length != 2 || matrix[0].length != 2;
int[] rows = new int[matrix.length];
int[] columns = new int[matrix[0].length];
int n;
//compute R and C values
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
rows[i] += matrix[i][j];
columns[j] += matrix[i][j];
}
System.out.println("rows[" + i + "] = " + rows[i]);
}
for (int i = 0; i < columns.length; i++) {
System.out.println("columns[" + i + "] = " + columns[i]);
}
//compute n
n = 0;
for (int i = 0; i < columns.length; i++) {
n += columns[i];
}
int[][][] perms = findAllPermutations(rows, columns);
double sum = 0;
//int count = 0;
double cutoff = chiSq ? getChiSquaredValue(matrix, rows, columns, n) : getConditionalProbability(matrix, rows, columns, n);
System.out.println("P cutoff = " + cutoff + "\n");
for (int[][] is : perms) {
System.out.println("Matrix: ");
printMatrix(is);
double val = chiSq ? getChiSquaredValue(is, rows, columns, n) : getConditionalProbability(is, rows, columns, n);
System.out.print("Value: " + val);
if(val <= cutoff){
//count++;
System.out.print(" is below " + cutoff);
// sum += (chiSq) ? getConditionalProbability(is, rows, columns, n) : val;
// sum += val;
double p = getConditionalProbability(is, rows, columns, n);
System.out.print("\np = " + p + "\nsum = " + sum + " + p = ");
sum += p;
System.out.print(sum);
} else {
System.out.println(" is above " + cutoff + "\np = " + getConditionalProbability(is, rows, columns, n));
}
System.out.print("\n\n");
}
return sum;
//return count / (double)perms.length;
}
所有其他方法都已经过测试和调试。问题是我不确定从哪里找到所有可能的矩阵(所有矩阵具有相同的行和列总和)。我不确定如何获取这些矩阵并将它们转换为 p 值。我读了一些关于卡方的东西,所以我找到了一个卡方算法。
所以我的问题是: 根据我所拥有的(矩阵的所有排列),我如何计算 p 值? 我所有的尝试要么在最后一个 for 循环中,要么在最后一个 for 循环中被注释掉。
【问题讨论】:
标签: java testing statistics