预测区间计算器
该计算器为回归分析中的给定值创建预测区间。
只需输入预测变量、响应变量、要为其创建预测区间和置信水平的单个值的值列表,然后单击“计算”按钮:
预测值:
响应值:
预测的 X 值:
信心程度:
90 % 预测区间:( 74.643 , 86.903 )
function calc() {
//get input data var x = document.getElementById('x').value.split(',').map(Number); var y = document.getElementById('y').value.split(',').map(Number); var xpred = +document.getElementById('xpred').value; var CI = +document.getElementById('CI').value;
//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); lr['sum_y'] = sum_y; lr['sum_xx'] = sum_xx; return lr; } //create regression variables var lr = linearRegression(y, x); var a = lr.slope; var b = lr.intercept; var r2 = lr.r2; var r2p = r2*100; var sxx = lr.sum_xx; //create sse variable var my = lr.sum_y / y.length; let sst = 0; for (let i = 0; i < y.length; i++) { sst += Math.pow((y[i] - my), 2); } var CI_out = CI*100 var sse = sst - r2*sst; var n = y.length; var var2 = sse/(n-2); var xbar = math.mean(x); var ypred = b - (-1*a*xpred); var df = n-2; var tcrit = -1*jStat.studentt.inv((1-CI)/2, df); //calculate lower and upper bounds of prediction interval var lowCI = ypred-tcrit*Math.sqrt(var2*(1-(-1*(1/n))-(-1*Math.pow(xpred-xbar,2)/sxx))); var highCI = ypred-(-1*(tcrit*Math.sqrt(var2*(1-(-1*(1/n))-(-1*Math.pow(xpred-xbar,2)/sxx))))); //output results document.getElementById('lowCI').innerHTML = lowCI.toFixed(3); document.getElementById('highCI').innerHTML = highCI.toFixed(3); document.getElementById('CI_out').innerHTML = CI_out.toFixed(0); } //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