w-statistic
Version:
A tool for statistical analysis.
379 lines (326 loc) • 12.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>regPoly.mjs - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="w-statistic.html">w-statistic</a><ul class='methods'><li data-type='method'><a href="w-statistic.html#.arrAverage">arrAverage</a></li><li data-type='method'><a href="w-statistic.html#.arrAverageWithLogNormCI">arrAverageWithLogNormCI</a></li><li data-type='method'><a href="w-statistic.html#.arrAverageWithNormCI">arrAverageWithNormCI</a></li><li data-type='method'><a href="w-statistic.html#.arrCount">arrCount</a></li><li data-type='method'><a href="w-statistic.html#.arrGammaFit">arrGammaFit</a></li><li data-type='method'><a href="w-statistic.html#.arrGammaHist">arrGammaHist</a></li><li data-type='method'><a href="w-statistic.html#.arrGammaInv">arrGammaInv</a></li><li data-type='method'><a href="w-statistic.html#.arrGeometricAverage">arrGeometricAverage</a></li><li data-type='method'><a href="w-statistic.html#.arrGeometricStd">arrGeometricStd</a></li><li data-type='method'><a href="w-statistic.html#.arrLogNormHist">arrLogNormHist</a></li><li data-type='method'><a href="w-statistic.html#.arrLogNormInv">arrLogNormInv</a></li><li data-type='method'><a href="w-statistic.html#.arrMovingAverage">arrMovingAverage</a></li><li data-type='method'><a href="w-statistic.html#.arrNormHist">arrNormHist</a></li><li data-type='method'><a href="w-statistic.html#.arrNormInv">arrNormInv</a></li><li data-type='method'><a href="w-statistic.html#.arrQuartile">arrQuartile</a></li><li data-type='method'><a href="w-statistic.html#.arrStd">arrStd</a></li><li data-type='method'><a href="w-statistic.html#.bin">bin</a></li><li data-type='method'><a href="w-statistic.html#.histGen">histGen</a></li><li data-type='method'><a href="w-statistic.html#.regLine">regLine</a></li><li data-type='method'><a href="w-statistic.html#.regMpLine">regMpLine</a></li><li data-type='method'><a href="w-statistic.html#.regPoly">regPoly</a></li><li data-type='method'><a href="w-statistic.html#.regPower">regPower</a></li><li data-type='method'><a href="w-statistic.html#.sampleRandom">sampleRandom</a></li><li data-type='method'><a href="w-statistic.html#.studentTInv">studentTInv</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">regPoly.mjs</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import get from 'lodash-es/get.js'
import each from 'lodash-es/each.js'
import map from 'lodash-es/map.js'
import size from 'lodash-es/size.js'
import isnum from 'wsemi/src/isnum.mjs'
import isbol from 'wsemi/src/isbol.mjs'
import iseobj from 'wsemi/src/iseobj.mjs'
import isearr from 'wsemi/src/isearr.mjs'
import ispint from 'wsemi/src/ispint.mjs'
import cint from 'wsemi/src/cint.mjs'
import cdbl from 'wsemi/src/cdbl.mjs'
import ss from './simpleStatistics.mjs'
import { PolynomialRegression } from 'ml-regression-polynomial'
/**
* 針對x,y數據進行多項式回歸(y=b+m1*x+m2*x^2+...)
*
* Unit Test: {@link https://github.com/yuda-lyu/w-statistic/blob/master/test/regPoly.test.js Github}
* @memberOf w-statistic
* @param {Array} arr 輸入陣列,只提取有效數字(或為字串的數字)進行計算
* @param {Object} [opt={}] 輸入設定物件,預設{}
* @param {Number} [opt.interpX=null] 輸入經由回歸結果內插指定x值數字,預設null
* @param {Boolean} [opt.calcR2=false] 輸入是否計算r2值布林值,預設false
* @param {Boolean} [opt.useRegIntercept=true] 輸入是否回歸使用截距布林值,預設true
* @param {Boolean} [opt.useSync=false] 輸入是否使用同步函數布林值,預設false
* @returns {Object|Promise} 若useSync=true回傳回歸結果物件,若useSync=false則回傳Promise,此時若成功則resolve回歸結果物件,若失敗則reject錯誤訊息
* @example
*
* async function test() {
*
* let arr
* let r
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = await regPoly(arr, 2)
* console.log(r)
* // => {
* // b: 7.960481099654818,
* // m1: -0.15371134020614285,
* // m2: 0.0010756013745701653
* // }
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = await regPoly(arr, 3)
* console.log(r)
* // => {
* // b: 21.051572327842386,
* // m1: -0.7142872117740979,
* // m2: 0.0087657232709057,
* // m3: -0.000034014675054462467
* // }
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = await regPoly(arr, 2, { useRegIntercept: false }) //不使用截距, 也就是截距b=0
* console.log(r)
* // => { b: 0, m1: 0.06524644304242452, m2: -0.00035672260356343373 }
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = await regPoly(arr, 2, { interpX: 80 })
* console.log(r)
* // => {
* // b: 7.960481099654818,
* // m1: -0.15371134020614285,
* // m2: 0.0010756013745701653,
* // interpX: 80,
* // interpY: 2.5474226804124473
* // }
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = await regPoly(arr, 2, { calcR2: true })
* console.log(r)
* // => {
* // b: 7.960481099654818,
* // m1: -0.15371134020614285,
* // m2: 0.0010756013745701653,
* // r2: 0.6732052768464256
* // }
*
* arr = [
* [50, 3.3], [50, 2.8],
* [50, 2.9], [70, 2.3],
* [70, 2.6], [70, 2.1],
* [80, 2.5], [80, 2.9],
* [80, 2.4], [90, 3],
* [90, 3.1], [90, 2.8],
* [100, 3.3], [100, 3.5],
* [100, 3]
* ]
* r = regPoly(arr, 2, { useSync: true }) //使用同步函數(sync)
* console.log(r)
* // => {
* // b: 7.960481099654818,
* // m1: -0.15371134020614285,
* // m2: 0.0010756013745701653
* // }
*
* arr = [
* [1, 2.5],
* [2.5, 1.1],
* [4, 0.5],
* ]
* r = await regPoly(arr, 1)
* console.log(r)
* // => { b: 3.0333333333333314, m1: -0.666666666666666 }
*
* }
* test()
* .catch((err) => {
* console.log(err)
* })
*
*/
function regPoly(arr, degree, opt = {}) {
//interpX
let interpX = get(opt, 'interpX')
if (!isnum(interpX)) {
interpX = null
}
//calcR2
let calcR2 = get(opt, 'calcR2')
if (!isbol(calcR2)) {
calcR2 = false
}
//useRegIntercept, 是否回歸使用截距
let useRegIntercept = get(opt, 'useRegIntercept')
if (!isbol(useRegIntercept)) {
useRegIntercept = true
}
//useSync
let useSync = get(opt, 'useSync')
if (!isbol(useSync)) {
useSync = false
}
//_sync
let _sync = () => {
//check arr
if (!isearr(arr)) {
throw new Error(`arr is not an effective array`)
}
//check degree
if (!ispint(degree)) {
throw new Error(`degree[${degree}] is not an integer`)
}
degree = cint(degree)
if (degree < 1) {
throw new Error(`degree[${degree}] < 1`)
}
//rs
let rs = []
each(arr, (v) => {
let x = get(v, 0)
let y = get(v, 1)
if (isnum(x) && isnum(y)) {
rs.push([cdbl(x), cdbl(y)])
}
})
// console.log('rs', rs)
//check
if (size(rs) === 0) {
throw new Error(`no effective data`)
}
//PolynomialRegression
let x = map(rs, 0)
let y = map(rs, 1)
let optPR = {
interceptAtZero: !useRegIntercept,
}
let regression = new PolynomialRegression(x, y, degree, optPR)
// console.log('regression', regression)
// console.log('regression.coefficients', regression.coefficients)
// from 0 to degree, regression.coefficients[0] -> b, regression.coefficients[1] -> m1, regression.coefficients[2] -> m2, ...
// console.log(regression.predict(80)) // Apply the model to some x value
// console.log(regression.toString(10)) // Prints a human-readable version of the function.
// console.log(regression.toLaTeX())
// console.log(regression.score(x, y))
//r
let r = {}
if (useRegIntercept) {
each(regression.coefficients, (v, k) => {
if (k === 0) {
r['b'] = v
}
else {
r[`m${k}`] = v
}
})
}
else {
r['b'] = 0
each(regression.coefficients, (v, k) => {
r[`m${k + 1}`] = v
})
}
// console.log('r', r)
//interpX
if (isnum(interpX)) {
interpX = cdbl(interpX)
let interpY = regression.predict(interpX)
r.interpX = interpX
r.interpY = interpY
}
//calcR2
if (calcR2) {
// let ys = []
// let yps = []
// each(rs, ([x, y]) => {
// let yp = regression.predict(x)
// ys.push(y)
// yps.push(yp)
// })
// let r2 = ss.rSquared(ys, yps)
let r2 = ss.rSquared(rs, (x) => regression.predict(x))
r.r2 = r2
}
return r
}
//_async
let _async = async () => {
let r = null
try {
r = _sync()
if (iseobj(r)) {
return r
}
else {
return Promise.reject(`no effective data`)
}
}
catch (err) {
console.log(err)
return Promise.reject(err.message)
}
}
if (useSync) {
return _sync()
}
else {
return _async()
}
}
export default regPoly
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Tue Jul 29 2025 14:21:18 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
</body>
</html>