Determining Linear Approximations
I'm trying to code a program that will read a set of data poitns ( x & y) from a text file and then perform a Linear Least Squares Analysis to determine the a0 and a1 of the linear regression line, as well as the Square Error value.
this is what I have so far without any of the equations but some of it doesnt seem to make sense. Can anyone let me know how it looks so far?
Code Java:
import java.io.*;
import java.util.Scanner;
public class FileScannerTest
{
// Since the could be an error in obtain a path to the input file,
// the "throws FileNotFoundException" clause must be added.
public static void main(String[ ] args) throws FileNotFoundException
{
Scanner scan = new Scanner (System.in);
System.out.println ("Enter x value (0 to terminate): ");
int input = scan.nextInt(); // Variable for user input.
//method to check if numeric or not.
if ("-2324.00".matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
}else {
System.out.println("Value is NOT numeric!");
// arrays to hold the values for x and y
String[] x = new String[25];
double sum = Double.parseDouble(x[1]);
for (int i = 0; i < x.length; i++) {
sum += x[i];
String[] y = new String[25];
// Declare an Employee "counter" and initialize it to zero.
int count = 0;
// Declares the file path to Assignment5_data
String currentDir = System.getProperty("user.dir");
String fileName = currentDir + "\\Assignment5_data.txt";
// Set up the input file, the scanner file stream, and the data string delimiter.
// In this case, the delimiter is set at one space, which is what separates the two data points
File inFile = new File(fileName);
Scanner fileScanner = new Scanner(inFile);
String delimiter = " ";
// Declare a variable to hold each line of file input.
String line;
//Use fileScanner to read each line.
while ( fileScanner.hasNextLine() )
{
// Read each line of text and split it up based upon the delimiter.
// The x string is placed in temp[0],
// the y string in temp[1],
line = fileScanner.nextLine();
String [] temp = line.split(delimiter);
// and increment the value of count.
x [count] = temp[0];
y [count] = temp[1];
count++;
}
// Close the file stream.
fileScanner.close();
/*
for (int index = 0; index < count; index++)
{
System.out.println(x[index] );
System.out.println(y[index] );
System.out.println("");
sum += Integer.parseInt(y [index]);
}
*/
}
}
}
}
Re: Determining Linear Approximations
You probably could have gotten an answer in the Programming Assignment 5 Questions Forum on moodle. ;D