UNPKG

@nexys/math-ts

Version:

[![npm version](https://badge.fury.io/js/%40nexys%2Fmath-ts.svg)](https://www.npmjs.com/package/@nexys/math-ts) [![TavisCI](https://travis-ci.com/Nexysweb/tableau-wdc-react.svg?branch=master)](https://travis-ci.com/github/Nexysweb/math-ts) [![Deployment](

68 lines (67 loc) 2.11 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const Vector = __importStar(require("../vector")); const I = __importStar(require("./index")); const sumSq = (x, d = 0) => { return x.map((fx) => { return Math.pow((fx - d), 2); }) .reduce((a, b) => a + b); }; const diffSq = (x, y) => x.map((xi, i) => { return Math.pow((xi - y[i]), 2); }) .reduce((a, b) => a + b); class Anova { constructor(y, f, p = 1) { this.n = y.length; this.dfr = p; this.p = p; if (this.n === 0) { throw Error('vectorslenght must be greater than zero'); } if (this.n !== f.length) { throw Error('vectors are not of the same size, object cannot be used'); } this.y = y; this.f = f; this.dft = this.n - 1; this.dfe = this.dft - this.dfr; this.yAvg = Vector.mean(y); this.fAvg = Vector.mean(f); this.yCtr = y.map(yi => yi - this.yAvg); this.fCtr = f.map(fi => fi - this.fAvg); this.rss = sumSq(this.f, this.yAvg); this.ess = diffSq(this.y, this.f); this.tss = sumSq(this.y, this.yAvg); this.rsq = 1 - this.ess / this.tss; this.r = Math.sqrt(this.rsq); this.rms = this.rss / this.dfr; this.ems = this.ess / this.dfe; this.tms = this.tss / this.dft; this.se = Math.sqrt(this.ems); this.radj = 1.0 - this.ems / this.tms; } correlation() { return I.correlation(this.y, this.f); } fTest() { const stat = this.rms / this.ems; return stat; } sigma(m) { return m.map(row => { return row.map(cell => { return Math.sqrt(cell * this.ems); }); }); } } exports.default = Anova;