线性回归计算器
该计算器根据预测变量和响应变量的值生成线性回归方程。
只需在下面的框中输入预测变量和响应变量的值列表,然后单击“计算”按钮:
预测值:
响应值:
线性回归方程:
ŷ = 0.9694 + ( 7.7673 )*x
配合质量:
R 方: 0.8282
解释:
当预测变量等于 0 时,响应变量的平均值为0.9694 。
预测变量每增加一个单位都与响应变量的平均变化 ( 7.7673 ) 相关。
响应变量中82.82 % 的变异可以通过预测变量来解释。
function calc() {
//get input data var x = document.getElementById('x').value.split(',').map(Number); var y = document.getElementById('y').value.split(',').map(Number);
//check that both lists are equal length if (x.length - y.length == 0) { document.getElementById('error_msg').innerHTML = '';
function linearRegression(y,x){ var lr = {}; var n = y.length; var sum_x = 0; var sum_y = 0; var sum_xy = 0; var sum_xx = 0; var sum_yy = 0;
for (var i = 0; i < y.length; i++) { sum_x += x[i]; sum_y += y[i]; sum_xy += (x[i]*y[i]); sum_xx += (x[i]*x[i]); sum_yy += (y[i]*y[i]); } lr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x); lr['intercept'] = (sum_y - lr.slope * sum_x)/n; lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2); return lr; } var lr = linearRegression(y, x); var a = lr.slope; var b = lr.intercept; var r2 = lr.r2; var r2p = r2*100; document.getElementById('a').innerHTML = a.toFixed(4); document.getElementById('b').innerHTML = b.toFixed(4); document.getElementById('r2').innerHTML = r2.toFixed(4); document.getElementById('interceptOut').innerHTML = b.toFixed(4); document.getElementById('slopeOut').innerHTML = a.toFixed(4); document.getElementById('r2Out').innerHTML = r2p.toFixed(2); } //output error message if boths lists are not equal else { document.getElementById('error_msg').innerHTML = 'The two lists must be of equal length.'; } } //end calc function