我创建了一个名为LineOfRegression的方法,我想打印出一个回归的示例线,斜率为0.18784,截距为-7.964。我怎么会在主要的? (我已粘贴全班以供参考; LineOfRegression方法位于底部)
package com.company;
public class ScatterPlot {
public Point[]points = null;
ScatterPlot(Point p){
points = new Point[1];
points[0] = p;
}
public void addPoint(Point p){
Point[]temp = null;
if(points==null || points.length == 0){
points = new Point[1];
points[0] = p;
}
else{
temp = new Point[points.length+1];
//use a for loop to copy points into temp
for(int i = 0; i < points.length; i++){
temp[i] = points[i];
}
//add the new Point p into the last element
temp[points.length] = p;
//copy over temp
points = temp;
}
}
public boolean removeIndex(int index) {
//Points is null
if(points == null){
return false;
}
if(points.length==0){
return false;
}
//Out of bounds index
if(index >= points.length || index <0){
return false;
}
//{p0 p1 p2 p3 p4 p5} index = 3
//P0 - P2
//Copy elements before remove
Point[] newArray = new Point[points.length-1];
for(int i = 0; i<index;i++){
newArray[i] = points[i];
}
//Copy elements after remove
for(int i = index; i<newArray.length; i++){
newArray[i] = points[i+1];
}
points = newArray;
return true;
}
void printReport(){
System.out.println("Number of points " + points.length);
System.out.println("Line of Regression is " + lineOfRegression().toStr());
}
public Line lineOfRegression(){
double n = points.length;
double sigmaX = 0;
double sigmaY = 0;
double sigmaXY = 0;
double sigmaXSquared = 0;
for(int count = 0; count<= n; count++){
sigmaX = sigmaX + points[count].getXCoordinate();
sigmaY = sigmaY + points[count].getYCoordinate();
sigmaXY = sigmaXY + points[count].getXCoordinate() * points[count].getYCoordinate();
}
double slope = (n * sigmaXY) - (sigmaX * sigmaY) / (n + (sigmaXSquared)) - (sigmaXSquared);
double intercept = (sigmaY - (slope * sigmaX)) / n;
Line regressionLine = new Line(slope,intercept);
return regressionLine;
}
}