Day9 You have to trust that the dots will somehow connect in your future

  • Plotting regression line

继昨天画出XY关系图後,我们就会进一去想知道XY的关系式,并最好在图上画上趋势线。所以我们需要做回归的计算,然後再画到图上。

  • Regression

回归的功能可以使用这个,这是目前找到最简洁完整的开源,除了线性外,还提供exponential、logarithmic、power、和polynomial,输入的资料格式为序列对的序列,另外可以设定精准度(.precision)和阶数(.order)以及周期(.period):

const data = [[0,1],[32, 67] .... [12, 79]];
const resultLinear = regression.linear(data, {precision: 5});
const resultPoly = regression.polynomial(data, {order: 3});

输出为物件,包含回归资料(.points)、转移函数的function(.predict,回传XY的序列对)、系数(.equation,系数的序列)、判定系数(.r2)和转移函数的文字(.string)。其中要注意转移函数、系数和判定系数都会四舍五入根据设定精准度(预设是小数点下2位),以下是linear的return为例:

    const predict = x => ([
      round(x, options.precision),
      round((gradient * x) + intercept, options.precision)]
    );
    return {
      points: data.map(point => predict(point[0])),
      predict: ,
      equation: [gradient, intercept],
      r2: round(determinationCoefficient(data, points), options.precision),
      string: intercept === 0 ? `y = ${gradient}x` : `y = ${gradient}x + ${intercept}`,
    };
  • Plotting

算完回归的公式後,接下是将结果画在图上,我们建立一个给chart的回归资料组,其中在scatter要连线需将showLine设为true,borderDash为设定成虚线,其中序列中的数值对应有和无的长度,而borderWidth和radius分别是线宽和标记点的半径,我这边是分别把它设小一点以及设0让点不见:

  const newDataset = {
    label: "Regression Line", //data-label
		borderColor: "rgb(0, 0, 0)",
		backgroundColor: "rgb(0, 0, 0)",
		data: [{x: xMin, y: regressParameter[0] * xMin + regressParameter[1]}, {x: xMax, y: regressParameter[0] * xMax + regressParameter[1]}], //data
    showLine: true,
    borderWidth: 1,
    borderDash: [10, 5],
    radius: 0
  }

成果如下:
https://ithelp.ithome.com.tw/upload/images/20210910/20141158fNqW3z3Lrr.jpg


<<:  [Day2] MacOS - 操作上手

>>:  DAY10 资料室--Vuex模组化

30天零负担轻松学会制作APP介面及设计【DAY 29】

大家好,我是YIYI,今天我要来回覆身边的朋友们看完我的文章後问我的问题。 QA Q:如果重来一次还...

[Day12] 第十二章-完成注册API 修改route,controller,model(使用passport认证)

前言 昨天把passport套件安装完 今天试者把model,route,controller设定完...

[DAY9]制作容器(八)

9/25: 隔一天才发现因为前一天字数不够,草稿发文不成功所以断赛了QQ 还是把昨天测试的结果放上来...

Leetcode (Algorithm I): 5. Search Insert Position

思路 也是binary search的应用题,承前两篇文章,有lb跟ub和index三个数值可选,我...

Day-30 不知不觉面试题完赛!感谢大家!

不敢相信今天是第30天了! 我完赛了!好感动啊~ 真的很感谢帮过我的老师/助教/同学/亲友…很多啦...