二次回归计算器
该计算器根据预测变量和响应变量的值生成二次回归方程。
只需在下面的框中输入预测变量和响应变量的值列表,然后单击“计算”按钮:
预测值:
响应值:
二次回归方程:
ŷ = -2.5475 + ( 3.7516 )x + ( -0.1704 ) x2
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 = '';
var xbar = math.mean(x); var ybar = math.mean(y);
let xbar2_hold = 0 for (let i = 0; i < x.length; i++) { xbar2_hold += Math.pow(x[i], 2); } var xbar2 = xbar2_hold / x.length; let sxx = 0 for (let i = 0; i < x.length; i++) { sxx += Math.pow(x[i] - xbar, 2); } let sxy = 0 for (let i = 0; i < x.length; i++) { sxy += (x[i] - xbar)*(y[i]-ybar); } let sxx2 = 0 for (let i = 0; i < x.length; i++) { sxx2 += (x[i] - xbar)*(Math.pow(x[i], 2)-xbar2); } let sx2x2 = 0 for (let i = 0; i < x.length; i++) { sx2x2 += Math.pow((Math.pow(x[i], 2)-xbar2), 2); } let sx2y = 0 for (let i = 0; i < x.length; i++) { sx2y += (Math.pow(x[i], 2)-xbar2)*(y[i]-ybar); } var b = ((sxy*sx2x2)-(sx2y*sxx2)) / ((sxx*sx2x2)-Math.pow(sxx2, 2)); var c = ((sx2y*sxx)-(sxy*sxx2)) / ((sxx*sx2x2)-Math.pow(sxx2, 2)); var a = ybar - (b*xbar) - (c*xbar2); document.getElementById('a').innerHTML = a.toFixed(4); document.getElementById('b').innerHTML = b.toFixed(4); document.getElementById('c').innerHTML = c.toFixed(4); } //output error message if boths lists are not equal else { //document.getElementById('out').innerHTML = ''; document.getElementById('error_msg').innerHTML = 'The two lists must be of equal length.'; } } //end calc function