新手上路,求大神指教我输入数据之后怎么出不来结果啊??(一元线性回归)
#include <stdio.h>#include <math.h>
#define ARR_size 100
int main(void)
{
double data[ARR_size][2]; //数组data存放x,y
double x_ave=0, y_ave=0, X_sum=0, y_sum=0;
double Lxx=0, Lxy=0, Lyy=0;
double x, y, a, b ,yi;
double SquarePoor[ARR_size];
float R;
int i;
int count=0;
printf("Please enter at least 2 groups of x, y:\n ");
printf("输入 -999,-999 表示输入结束!\n");
while(1)
{
scanf("%d,%d",&x,&y);
if(fabs(x+999) == 0&&fabs(y+999)== 0) break;
data[count][0]=x;
data[count][1]=y;
count++;
}
for(i=0;i<(count+1);i++)
{
X_sum += data[i][0];
y_sum += data[i][1];
}
x_ave=X_sum/(count+1); // X平均值
y_ave=y_sum/(count+1); // Y平均值
for(i=0;i<(count+1);i++)
{
Lxx += (data[i][0]-x_ave)*(data[i][0]-x_ave); // Lxx = Sum((X - x_ave)平方)
Lxy += (data[i][0]-x_ave)*(data[i][1]-y_ave); // Lxy = Sum((X - x_ave)(Y - y_ave))
Lyy += (data[i][1]-y_ave)*(data[i][1]-y_ave); // Lyy = Sum((y - y_ave)平方)
}
a = Lxy / Lxx;
b = y_ave - a * x_ave;
SquarePoor[0] = SquarePoor[1] = 0;
for ( i=0;i<(count+1);i++)
{
yi = a * data[i][0] + b;
SquarePoor[0] += ((yi - y_ave) * (yi -y_ave )); // U(回归平方和)
SquarePoor[1] += ((data[i][1] - yi) * (data[i][1] - yi)); // Q(剩余平方和)
}
SquarePoor[2] = SquarePoor[0]; // 回归方差
SquarePoor[3] = SquarePoor[1] / (count - 1); // 剩余方差
printf("回归方程式:\n " );
if(fabs(b) == 0)
printf("y=%.5f * x\n",a);
else if(b > 0)
printf("y=%.5f * x + %.5f\n", a, b);
else if(b < 0)
printf("y = %.5f * x - %.5f\n", a, b);
printf("回归显著性检验:\n ");
printf("回归平方和:%12.4lf 回归方差:%12.4lf\n ", SquarePoor[0], SquarePoor[2]);
printf("剩余平方和:%12.4lf 剩余方差:%12.4lf\n ", SquarePoor[1], SquarePoor[3]);
printf("离差平方和:%12.4lf 标准误差:%12.4lf\n ", SquarePoor[0] + SquarePoor[1], sqrt(SquarePoor[3]));
printf("F 检 验:%12.4lf 相关系数:%12.4lf\n ", SquarePoor[2] / SquarePoor[3], sqrt(SquarePoor[0] / (SquarePoor[0] + SquarePoor[1])));
printf("剩余分析:\n ");
printf(" 观察值 估计值 剩余值 剩余平方 \n");
for (i=0;i<(count+1);i++)
{
printf("%12.2lf%12.2lf%12.2lf%12.2lf\n",data[i][1],yi,data[i][1]-yi,(data[i][1]-yi)*(data[i][1]-yi));
}
}