package edu.indiana.soic.dsc.spidal.lr;

import java.util.*;
import java.io.*;



public class LinearRegression {
    public static void main(String[] args) throws Exception {

        int NMAX = 1000;
        int n=0;
        double []x = new double[NMAX];
        double []y = new double[NMAX];

        String filename = "regcopy.txt";
        FileReader fr = new FileReader(filename);
        BufferedReader br = new BufferedReader(fr);

        String line;
        String[] split;
        int i = 0;

        while((line=br.readLine()) != null){
            split = line.split("\\s+");
            x[i] = Double.parseDouble((split[0]));
            y[i] = Double.parseDouble(split[1]);
            i++;
            //System.out.print(x[i] + "  ");
            //System.out.print(y[i] + "  ");
        }


        double sumx = 0.0; double sumy = 0.0; double sumz=0.0;
        for (n=0; n<NMAX; n++) {
            sumx  = sumx +  x[n];
            sumz = sumz + (x[n] * x[n]);
            sumy  = sumy + y[n];

        }

        double xAvg = sumx / n;
        double yAvg = sumy / n;
        double xX = 0.0; double yY = 0.0; double xY = 0.0;
        for ( i = 0; i < n; i++) {
            xX += (x[i] - xAvg) * (x[i] - xAvg);
            yY += (y[i] - yAvg) * (y[i] - yAvg);
            xY += (x[i] - xAvg) * (y[i] - yAvg);

        }

        double beta1 = xY / xX;
        double beta0 = yAvg - beta1 * xAvg;

        System.out.println("y   = " + beta1 + " * x + " + beta0);


        int df = n - 2;
        double rss = 0.0;
        double ssr = 0.0;

        for ( i = 0; i < n; i++) {

            double fit = beta1*x[i] + beta0;
            rss += (fit - y[i]) * (fit - y[i]);
            ssr += (fit - yAvg) * (fit - yAvg);

        }

        double R2    = ssr / yY;
        double svar  = rss / df;
        double svar1 = svar / xX;
        double svar0 = svar/n + xAvg*xAvg*svar1;
        System.out.println("R^2                 = " + R2);
        System.out.println("STD error of beta_1 = " + Math.sqrt(svar1));
        svar0 = svar * sumz / (n * xX);
        System.out.println("STD error of beta_0 = " + Math.sqrt(svar0));
        System.out.println("SSTO = " + yY);
        System.out.println("SSE  = " + rss);
        System.out.println("SSR  = " + ssr);
    }


}


@@ -166,24 +166,25 @@
 xception %7B%0A%0A
+%0A
         int 
@@ -187,21 +187,17 @@
 int 
-NMAX 
+n
 = 1000;%0A
     
@@ -196,25 +196,8 @@
 00;%0A
-        int n=0;%0A
     
@@ -224,20 +224,17 @@
  double%5B
-NMAX
+n
 %5D;%0A     
@@ -260,20 +260,17 @@
  double%5B
-NMAX
+n
 %5D;%0A%0A    
@@ -292,16 +292,37 @@
 name = %22
+/Users/madhu/Desktop/
 regcopy.
@@ -879,22 +879,19 @@
 or (
-n
+i
 =0; 
-n%3CNMAX; n
+i%3Cn; i
 ++) 
@@ -922,17 +922,17 @@
 mx +  x%5B
-n
+i
 %5D;%0A     
@@ -959,16 +959,16 @@
  (x%5B
-n
+i
 %5D * x%5B
-n
+i
 %5D);%0A
@@ -996,17 +996,17 @@
 umy + y%5B
-n
+i
 %5D;%0A%0A    

Update LinearRegression.java