本文整理汇总了Java中org.apache.commons.math3.stat.correlation.PearsonsCorrelation.getCorrelationMatrix方法的典型用法代码示例。如果您正苦于以下问题:Java PearsonsCorrelation.getCorrelationMatrix方法的具体用法?Java PearsonsCorrelation.getCorrelationMatrix怎么用?Java PearsonsCorrelation.getCorrelationMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.stat.correlation.PearsonsCorrelation
的用法示例。
在下文中一共展示了PearsonsCorrelation.getCorrelationMatrix方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateAndOutputCorrelations
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; //导入方法依赖的package包/类
/**
* Calculates pearson correlation between pair of columns in the in the matrix, assuming that there is a strict
* one to one relationship between the matrix columns and the field specifications in the list.
*
* Writes the correlation, pValue and standard error to a file using JSON format.
*
* @param matrix the input matrix where fields are represented by as columns and subjects by rows
* @param fields a list of field specifications for which the correlations are to be calculated
* @param correlationAnalysisOutputPath is the file to which the results are written
* @throws Exception
*/
public static void calculateAndOutputCorrelations(RealMatrix matrix, List<FieldRecipe> fields,
String correlationAnalysisOutputPath) throws Exception {
PearsonsCorrelation correlation = new PearsonsCorrelation(matrix);
RealMatrix correlationMatrix = correlation.getCorrelationMatrix();
RealMatrix pValueMatrix = correlation.getCorrelationPValues();
RealMatrix standardErrorMatrix = correlation.getCorrelationStandardErrors();
// Output the correlation analysis
JSONArray correlationArray = new JSONArray();
for (int i=0; i<correlationMatrix.getRowDimension(); i++){
for (int j=0; j<correlationMatrix.getColumnDimension(); j++){
JSONObject correlationObject = new JSONObject();
correlationObject.put("xFieldLabel", fields.get(i).toField().getLabel());
correlationObject.put("yFieldLabel", fields.get(j).toField().getLabel());
correlationObject.put("correlationCoefficient", correlationMatrix.getEntry(i,j));
correlationObject.put("pValue", pValueMatrix.getEntry(i,j));
correlationObject.put("standardError", standardErrorMatrix.getEntry(i,j));
correlationArray.add(correlationObject);
}
}
Writer writer = new OutputStreamWriter(new FileOutputStream(correlationAnalysisOutputPath), "UTF-8");
writer.write(correlationArray.toJSONString());
writer.flush();
writer.close();
}
示例2: createCorrelationOfSamples
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; //导入方法依赖的package包/类
/**
* creates the correlation of Samples useing Pearson's,Kendall's and Spearman's correlation coefficient
* @param samples
* @param rank
* @param type
*/
public static void createCorrelationOfSamples(List<Sample> samples, String rank, String type) {
//We need the unified map to make sure the counts are properly aligned
LinkedList<TaxonNode> taxonNodeList = getUnifiedTaxonList(samples, rank);
//The matrix data needs to be double, since PearsonsCorrelation only takes double arrays
double[][] taxaCounts = new double[samples.size()][taxonNodeList.size()];
for (int sampleIndex = 0; sampleIndex < samples.size(); sampleIndex++) {
double[] currentSampleCounts = new double[taxonNodeList.size()];
for (int taxonIndex = 0; taxonIndex < currentSampleCounts.length; taxonIndex++) {
currentSampleCounts[taxonIndex] = samples.get(sampleIndex).getTaxonCountRecursive(taxonNodeList.get(taxonIndex));
}
taxaCounts[sampleIndex] = currentSampleCounts;
}
switch (type) {
case "pearson":
pearsonsCorrelation = new PearsonsCorrelation(taxaCounts);
correlationMatrix = pearsonsCorrelation.getCorrelationMatrix();
pValueMatrix = pearsonsCorrelation.getCorrelationPValues();
break;
case "spearman":
spearmansCorrelation = new SpearmansCorrelation(new BlockRealMatrix(taxaCounts));
correlationMatrix = spearmansCorrelation.getCorrelationMatrix();
pValueMatrix = spearmansCorrelation.getRankCorrelation().getCorrelationPValues();
break;
case "kendall":
kendallsCorrelation = new KendallsCorrelation(taxaCounts);
correlationMatrix = kendallsCorrelation.getCorrelationMatrix();
pValueMatrix = new PearsonsCorrelation(taxaCounts).getCorrelationPValues(); //No p-values available for kendall's correlation!
break;
}
//Correct the NaNs to 0.0s
for (int i = 0; i < correlationMatrix.getRowDimension(); i++) {
for (int j = 0; j < correlationMatrix.getColumnDimension(); j++) {
if (Double.isNaN(correlationMatrix.getEntry(i, j)))
correlationMatrix.setEntry(i, j, 0.0);
}
}
for (int i = 0; i < pValueMatrix.getRowDimension(); i++) {
for (int j = 0; j < pValueMatrix.getColumnDimension(); j++) {
if (Double.isNaN(pValueMatrix.getEntry(i, j)))
pValueMatrix.setEntry(i, j, 1.0);
}
}
}
示例3: testPCorr
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; //导入方法依赖的package包/类
@Test
public void testPCorr() {
MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
double[][] x = new double[airdata[0].length][];
double[] y = new double[airdata[0].length];
double[] cp = new double[10];
double[] yxcorr = new double[4];
double[] diag = new double[4];
double sumysq = 0.0;
int off = 0;
for (int i = 0; i < airdata[0].length; i++) {
x[i] = new double[4];
x[i][0] = 1.0;
x[i][1] = FastMath.log(airdata[3][i]);
x[i][2] = FastMath.log(airdata[4][i]);
x[i][3] = airdata[5][i];
y[i] = FastMath.log(airdata[2][i]);
off = 0;
for (int j = 0; j < 4; j++) {
double tmp = x[i][j];
for (int k = 0; k <= j; k++, off++) {
cp[off] += tmp * x[i][k];
}
yxcorr[j] += tmp * y[i];
}
sumysq += y[i] * y[i];
}
PearsonsCorrelation pearson = new PearsonsCorrelation(x);
RealMatrix corr = pearson.getCorrelationMatrix();
off = 0;
for (int i = 0; i < 4; i++, off += (i + 1)) {
diag[i] = FastMath.sqrt(cp[off]);
}
instance.addObservations(x, y);
double[] pc = instance.getPartialCorrelations(0);
int idx = 0;
off = 0;
int off2 = 6;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
if (FastMath.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) {
Assert.fail("Failed cross products... i = " + i + " j = " + j);
}
++idx;
++off;
}
++off;
if (FastMath.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) {
Assert.fail("Assert.failed cross product i = " + i + " y");
}
}
double[] pc2 = instance.getPartialCorrelations(1);
idx = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < i; j++) {
if (FastMath.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) {
Assert.fail("Failed cross products... i = " + i + " j = " + j);
}
++idx;
}
}
double[] pc3 = instance.getPartialCorrelations(2);
if (pc3 == null) {
Assert.fail("Should not be null");
}
return;
}
示例4: testPCorr
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; //导入方法依赖的package包/类
@Test
public void testPCorr() {
MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false);
double[][] x = new double[airdata[0].length][];
double[] y = new double[airdata[0].length];
double[] cp = new double[10];
double[] yxcorr = new double[4];
double[] diag = new double[4];
double sumysq = 0.0;
int off = 0;
for (int i = 0; i < airdata[0].length; i++) {
x[i] = new double[4];
x[i][0] = 1.0;
x[i][1] = Math.log(airdata[3][i]);
x[i][2] = Math.log(airdata[4][i]);
x[i][3] = airdata[5][i];
y[i] = Math.log(airdata[2][i]);
off = 0;
for (int j = 0; j < 4; j++) {
double tmp = x[i][j];
for (int k = 0; k <= j; k++, off++) {
cp[off] += tmp * x[i][k];
}
yxcorr[j] += tmp * y[i];
}
sumysq += y[i] * y[i];
}
PearsonsCorrelation pearson = new PearsonsCorrelation(x);
RealMatrix corr = pearson.getCorrelationMatrix();
off = 0;
for (int i = 0; i < 4; i++, off += (i + 1)) {
diag[i] = FastMath.sqrt(cp[off]);
}
instance.addObservations(x, y);
double[] pc = instance.getPartialCorrelations(0);
int idx = 0;
off = 0;
int off2 = 6;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
if (Math.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) {
Assert.fail("Failed cross products... i = " + i + " j = " + j);
}
++idx;
++off;
}
++off;
if (Math.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) {
Assert.fail("Assert.failed cross product i = " + i + " y");
}
}
double[] pc2 = instance.getPartialCorrelations(1);
idx = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < i; j++) {
if (Math.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) {
Assert.fail("Failed cross products... i = " + i + " j = " + j);
}
++idx;
}
}
double[] pc3 = instance.getPartialCorrelations(2);
if (pc3 == null) {
Assert.fail("Should not be null");
}
return;
}