@visactor/vmind
Version:
<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu
38 lines (32 loc) • 2.36 kB
JavaScript
import { isArray } from "@visactor/vutils";
import { pearsonCorrelationCoeff, studentTQuantile } from "../statistics";
import { InsightType } from "../../type";
import { ChartType } from "../../../../types";
const pearsonAlgo = (context, options) => {
const {seriesDataMap: seriesDataMap, cell: cell, insights: insights} = context, {threshold: pearsonThreshold = .8, withoutSeries: withoutSeries = !1} = options || {}, {y: celly, x: cellx, color: color} = cell, yField = isArray(celly) ? celly[0] : celly, xField = isArray(cellx) ? cellx[0] : cellx, seriesField = isArray(color) ? color[0] : color, result = [], seriesNames = Object.keys(seriesDataMap);
return withoutSeries && seriesNames.length > 1 ? [] : (seriesNames.forEach((series => {
let seriesDataset = seriesDataMap[series].map((d => d.dataItem));
const outlierData = insights.filter((insight => insight.type === InsightType.Outlier && (!seriesField || String(insight.data[0][seriesField]) === series))).map((i => i.data[0]));
outlierData.length > 0 && (seriesDataset = seriesDataset.filter((dataItem => !outlierData.find((od => od[xField] === dataItem[xField] && od[yField] === dataItem[yField] && (!seriesField || dataItem[seriesField] === od[seriesField]))))));
const xMeasureSet = seriesDataset.map((d => d[yField])), yMeasureSet = seriesDataset.map((d => d[xField]));
if (xMeasureSet.length !== yMeasureSet.length) return;
const pearsonCoefficient = pearsonCorrelationCoeff(xMeasureSet, yMeasureSet), degree = yMeasureSet.length - 2, tValue = pearsonCoefficient * Math.sqrt(degree) / Math.sqrt(1 - pearsonCoefficient ** 2), threshold = studentTQuantile(.975, degree);
Math.abs(pearsonCoefficient) > pearsonThreshold && Math.abs(tValue) >= threshold && result.push({
type: InsightType.Correlation,
fieldId: [ xField, yField ],
significant: Math.abs(pearsonCoefficient),
seriesName: series,
info: {
isLinearCorrelation: !0
}
});
})), result);
};
export const ScatterPlotCorrelation = {
name: "pearson-coefficient",
chartType: [ ChartType.ScatterPlot ],
forceChartType: [ ChartType.ScatterPlot ],
insightType: InsightType.Correlation,
algorithmFunction: pearsonAlgo
};
//# sourceMappingURL=pearson.js.map