Simple linear regression analysis calculator : - the code below shows the modeling of the relationship between a response variable and an explanatory variable (dependent and independent variable respectively) in one of the most widely use of statistical techniques referred to as “Regression Analysis”.
This regression model allows “you” to explore what happens to the response variable for specified changes in explanatory variable.
It aims at aims finding the line of best fit through a 2 - dimensional plane of paired x and y variables, finding the error of estimation, Pearsons’ coefficient of correlation and also, finding the (a) regression line using y = a + bx with it corresponding scatter plot.
This regression model allows “you” to explore what happens to the response variable for specified changes in explanatory variable.
It aims at aims finding the line of best fit through a 2 - dimensional plane of paired x and y variables, finding the error of estimation, Pearsons’ coefficient of correlation and also, finding the (a) regression line using y = a + bx with it corresponding scatter plot.
A regression (linear) has two parameters which are the slope and the intercept. Once the estimates of both parameters are known using the program, current application, the predicted value (y) will be generated.
----------------------------------------------------------------------
Copy, Save on your preferred storage device... Note, the class name is LRegression, so all you have to do is "change to your current directory" then Run...
If you saved on "c:\users\document"--- all you have to do is change ur current directory in your cmd prompt to "c:\users\document" then "java LRegression Values of x each separated by a space FOLLOWED by Vales of y.
Example---- if your x values are 12, 23, 45, 6 and your y values are 23, 32.41,12----- all you have to do after changing to your current directory is ~> java LRegression 12 23 45 6 23 32 41 12.
In case of any error (Exception), fear not, its taken care of.... you would get an\ user friendly error message.
-------------------------------------------------------------------------------------------------
/*
Simple Linear Regression calculator
Program written by Doubra Clement Ebijuoworih
*/
import java.util.Scanner;
import static java.lang.System.out;
import javax.swing.JOptionPane;
class LRegression
{
// n counts for entry values (x and y)
static int n = 0;
// initialization and declaration of the summatioon of x and y values..
static double sumOfx = 0.0;
static double sumOfy = 0.0;
///000 UserInput.. i.e.trying to get the sum of x and sum of y...
public static void main(String [] args)
{
double[] x = new double[args.length];
double[] y = new double[args.length];
// checking for NaN
for (int i = 0; i <args.length; i++)
{
try
{
double change = Double.parseDouble(args[i]);
if (change == Double.NaN)
{}
}
catch (Exception e)
{System.out.println("\n\nVALUES OTHER THAN INTEGER OR DOUBLE HAVE BEEN ENTERED");
System.exit(1);
}};
// making sure more than 1 value is input into the system
if (args.length < 3)
{
System.out.println("\n\n REQUIRED INPUT SHOULD BE MORE THAN 1 FOR BOTH X & Y");
System.exit(1);
}
// checking if x input == y input
if ((args.length % 2) != 0)
{
out.println("\n **ERROR** Corresponding 'y' values not found for every x' values or vice versa.....");
out.println("\n **Enter Your 'X' and 'Y' values again; Using the previous format.........");
}
else
{
n = args.length / 2 ;
for (int count = 0; count <= args.length - 1; count++)
{
if (count >= n)
{
// y input and functions...
y[count] = Double.parseDouble(args[count]);
sumOfy += y[count]; // getting the summation of y
continue;
}
else {;}
x[count] = Double.parseDouble(args[count]);
sumOfx += x[count];
}
//000
// 111
// Getting the summation of xy i.e. (Exy)
double [] x_ = new double[args.length];
double [] y_ = new double[args.length];
double SumOfXY = 0.0;
double SumOfxSquare = 0.0;
double SumOfySquare = 0.0;
double slope = 0.0;
double xMean = 0.0;
double yMean = 0.0;
double intercept = 0.0;
double leastSquare = 0.0 ;
double pearsonsCofC = 0.0;
for (int xycounter =0; xycounter <=args.length -1; xycounter++)
{ //opening 44
if (xycounter == n)
break;
x_ [xycounter] = Double.parseDouble(args[xycounter]);
SumOfxSquare += (x_[xycounter] * x_[xycounter]);
y_ [xycounter] = Double.parseDouble(args[xycounter+n]);
SumOfySquare += (y_[xycounter] * y_[xycounter]);
SumOfXY += (x_[xycounter] * y_[xycounter]);
} // closing 44
// 111
/// 222 Calculating "b" = SLOPE of the line...
slope = ((n * SumOfXY) - (sumOfx * sumOfy)) / ((n * SumOfxSquare) -(sumOfx * sumOfx));
/// Calculating "a" = intercept...
xMean = sumOfx / n ;
yMean = sumOfy / n ;
intercept = yMean - (slope * xMean);
/////// Calculating the least Square...
leastSquare = Math.sqrt(((intercept * SumOfySquare) - (slope * SumOfXY) - (n * (yMean * yMean))) / ( SumOfySquare - (n * (yMean * yMean))));
// if (leastSquare == (double)NaN)
// { leastSquare = 0.00; }
/////// Calculating the pearsons CofC
pearsonsCofC = (SumOfXY - (n * (xMean * yMean))) / Math.sqrt((SumOfySquare - (n * (yMean * yMean))) * (SumOfxSquare - (n * (xMean * xMean))));
///// *****************working with JFrames & console***************
out.println("\n For the given values of x and y,\n");
//** Drawing the table**//
out.println(" ---------------------------");
out.printf(" %s %15s", "Values of x" ," Values of y\n");
out.println(" ---------------------------");
for (int draw = 0; draw < n; draw++)
{
double xdouble = 0.0;
double ydouble = 0.0;
xdouble = Double.parseDouble(args[draw]);
ydouble = Double.parseDouble(args[draw+n]);
out.printf(" %9s %s %7s" , xdouble ," |" , ydouble);
out.println();
}
out.println(" ---------------------------");
out.printf(" %9s%14s" , sumOfx, sumOfy);
out.println();
out.println(" ---------------------------");
out.printf("\n %s%.2f %10s%.2f" , "mean of x = " , xMean ,"; mean of y = ", yMean);
out.println();
//** end of table...**//
// out.printf("\n %s " , " ::
out.printf("\n %s%.2f%s%.2f%s\n" , "The regression line :: y = ", intercept, " + ", slope, "(x)");
out.printf("\n %s %.2f\n" , "The LeastSquare :: r = ", leastSquare);
out.printf("\n %s %.2f \n" , "The Pearsons Coefficient of Correlation :: r = ", pearsonsCofC);
// JOptionPane.showMessageDialog(null, "The regression line :- Y = " + (int)intercept +" + "+ //(int)slope +"(x)"+ "\n\nThe LeastSquare :- r = " +(int)leastSquare + "\n\nThe Pearsons //Coefficient of Correlation :- r = " + (int)pearsonsCofC);
///222
}}}
----------------------------------------------------------------------
Copy, Save on your preferred storage device... Note, the class name is LRegression, so all you have to do is "change to your current directory" then Run...
If you saved on "c:\users\document"--- all you have to do is change ur current directory in your cmd prompt to "c:\users\document" then "java LRegression Values of x each separated by a space FOLLOWED by Vales of y.
Example---- if your x values are 12, 23, 45, 6 and your y values are 23, 32.41,12----- all you have to do after changing to your current directory is ~> java LRegression 12 23 45 6 23 32 41 12.
In case of any error (Exception), fear not, its taken care of.... you would get an\ user friendly error message.
-------------------------------------------------------------------------------------------------
/*
Simple Linear Regression calculator
Program written by Doubra Clement Ebijuoworih
*/
import java.util.Scanner;
import static java.lang.System.out;
import javax.swing.JOptionPane;
class LRegression
{
// n counts for entry values (x and y)
static int n = 0;
// initialization and declaration of the summatioon of x and y values..
static double sumOfx = 0.0;
static double sumOfy = 0.0;
///000 UserInput.. i.e.trying to get the sum of x and sum of y...
public static void main(String [] args)
{
double[] x = new double[args.length];
double[] y = new double[args.length];
// checking for NaN
for (int i = 0; i <args.length; i++)
{
try
{
double change = Double.parseDouble(args[i]);
if (change == Double.NaN)
{}
}
catch (Exception e)
{System.out.println("\n\nVALUES OTHER THAN INTEGER OR DOUBLE HAVE BEEN ENTERED");
System.exit(1);
}};
// making sure more than 1 value is input into the system
if (args.length < 3)
{
System.out.println("\n\n REQUIRED INPUT SHOULD BE MORE THAN 1 FOR BOTH X & Y");
System.exit(1);
}
// checking if x input == y input
if ((args.length % 2) != 0)
{
out.println("\n **ERROR** Corresponding 'y' values not found for every x' values or vice versa.....");
out.println("\n **Enter Your 'X' and 'Y' values again; Using the previous format.........");
}
else
{
n = args.length / 2 ;
for (int count = 0; count <= args.length - 1; count++)
{
if (count >= n)
{
// y input and functions...
y[count] = Double.parseDouble(args[count]);
sumOfy += y[count]; // getting the summation of y
continue;
}
else {;}
x[count] = Double.parseDouble(args[count]);
sumOfx += x[count];
}
//000
// 111
// Getting the summation of xy i.e. (Exy)
double [] x_ = new double[args.length];
double [] y_ = new double[args.length];
double SumOfXY = 0.0;
double SumOfxSquare = 0.0;
double SumOfySquare = 0.0;
double slope = 0.0;
double xMean = 0.0;
double yMean = 0.0;
double intercept = 0.0;
double leastSquare = 0.0 ;
double pearsonsCofC = 0.0;
for (int xycounter =0; xycounter <=args.length -1; xycounter++)
{ //opening 44
if (xycounter == n)
break;
x_ [xycounter] = Double.parseDouble(args[xycounter]);
SumOfxSquare += (x_[xycounter] * x_[xycounter]);
y_ [xycounter] = Double.parseDouble(args[xycounter+n]);
SumOfySquare += (y_[xycounter] * y_[xycounter]);
SumOfXY += (x_[xycounter] * y_[xycounter]);
} // closing 44
// 111
/// 222 Calculating "b" = SLOPE of the line...
slope = ((n * SumOfXY) - (sumOfx * sumOfy)) / ((n * SumOfxSquare) -(sumOfx * sumOfx));
/// Calculating "a" = intercept...
xMean = sumOfx / n ;
yMean = sumOfy / n ;
intercept = yMean - (slope * xMean);
/////// Calculating the least Square...
leastSquare = Math.sqrt(((intercept * SumOfySquare) - (slope * SumOfXY) - (n * (yMean * yMean))) / ( SumOfySquare - (n * (yMean * yMean))));
// if (leastSquare == (double)NaN)
// { leastSquare = 0.00; }
/////// Calculating the pearsons CofC
pearsonsCofC = (SumOfXY - (n * (xMean * yMean))) / Math.sqrt((SumOfySquare - (n * (yMean * yMean))) * (SumOfxSquare - (n * (xMean * xMean))));
///// *****************working with JFrames & console***************
out.println("\n For the given values of x and y,\n");
//** Drawing the table**//
out.println(" ---------------------------");
out.printf(" %s %15s", "Values of x" ," Values of y\n");
out.println(" ---------------------------");
for (int draw = 0; draw < n; draw++)
{
double xdouble = 0.0;
double ydouble = 0.0;
xdouble = Double.parseDouble(args[draw]);
ydouble = Double.parseDouble(args[draw+n]);
out.printf(" %9s %s %7s" , xdouble ," |" , ydouble);
out.println();
}
out.println(" ---------------------------");
out.printf(" %9s%14s" , sumOfx, sumOfy);
out.println();
out.println(" ---------------------------");
out.printf("\n %s%.2f %10s%.2f" , "mean of x = " , xMean ,"; mean of y = ", yMean);
out.println();
//** end of table...**//
// out.printf("\n %s " , " ::
out.printf("\n %s%.2f%s%.2f%s\n" , "The regression line :: y = ", intercept, " + ", slope, "(x)");
out.printf("\n %s %.2f\n" , "The LeastSquare :: r = ", leastSquare);
out.printf("\n %s %.2f \n" , "The Pearsons Coefficient of Correlation :: r = ", pearsonsCofC);
// JOptionPane.showMessageDialog(null, "The regression line :- Y = " + (int)intercept +" + "+ //(int)slope +"(x)"+ "\n\nThe LeastSquare :- r = " +(int)leastSquare + "\n\nThe Pearsons //Coefficient of Correlation :- r = " + (int)pearsonsCofC);
///222
}}}