UNPKG

als-statistics

Version:

Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co

49 lines (47 loc) 2.02 kB
import { htmlTable } from '../../utils/html-table.js'; import Column from '../../column/index.js'; export function extractMetrics(columns, metricNames) { const results = {} Object.entries(columns).forEach(([colName, col]) => { results[colName] = {} metricNames.forEach(metricName => { let keys = metricName.split('.'), value = col; for (let i = 0; i < keys.length; i++) { if (value !== undefined) value = value[keys[i]] if (Array.isArray(value)) value = new Column(value) } if (value instanceof Column) value = value.values results[colName][metricName] = value }); }) const objTable = (k) => { const tables = Object.entries(results).map(([varName,descriptives]) => { const rows = Object.entries(descriptives[k]) const headers = [varName,k] delete descriptives[k] return htmlTable(rows,headers,{header:`${k} for ${varName}`}) }).join('<br>') metricNames.splice(metricNames.indexOf(k),1) return tables } Object.defineProperty(results, 'htmlTable', { get() { const entries = Object.entries(results) let htmlResult = '' const objectMetrics = metricNames.filter(metricName => typeof entries[0][1][metricName] === 'object') htmlResult += objectMetrics.map(metric => objTable(metric)).join('<br>') if(metricNames.length === 0) return htmlResult const headers = ['Metric', ...metricNames] const rows = entries.map(([name, metrics]) => [name, ...metricNames.map(k => metrics[k])]) const options = { firstColHeader: true, header: 'Descriptive statistics' } return /*html*/`<div> <h2>Descriptive statistics for ${entries.map(([name]) =>name).join(' , ')}</h2> <hr> ${htmlTable(rows, headers, options)} ${htmlResult} </div>` }, enumerable: false }) return results }