answersLogoWhite

0


Best Answer

import java.io.*;
import java.util.*;
public class LinearRegression {

public static void main(String[] args) {
int MAXN = 1000;
int n = 0;
double[] x = new double[MAXN];
double[] y = new double[MAXN];

// first pass: read in data, compute xbar and ybar
double sumx = 0.0, sumy = 0.0, sumx2 = 0.0;
while(!StdIn.isEmpty()) {
x[n] = StdIn.readDouble();
y[n] = StdIn.readDouble();
sumx += x[n];
sumx2 += x[n] * x[n];
sumy += y[n];
n++;
}
double xbar = sumx / n;
double ybar = sumy / n;

// second pass: compute summary statistics
double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
for (int i = 0; i < n; i++) {
xxbar += (x[i] - xbar) * (x[i] - xbar);
yybar += (y[i] - ybar) * (y[i] - ybar);
xybar += (x[i] - xbar) * (y[i] - ybar);
}
double beta1 = xybar / xxbar;
double beta0 = ybar - beta1 * xbar;

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

// analyze results
int df = n - 2;
double rss = 0.0; // residual sum of squares
double ssr = 0.0; // regression sum of squares
for (int i = 0; i < n; i++) {
double fit = beta1*x[i] + beta0;
rss += (fit - y[i]) * (fit - y[i]);
ssr += (fit - ybar) * (fit - ybar);
}
double R2 = ssr / yybar;
double svar = rss / df;
double svar1 = svar / xxbar;
double svar0 = svar/n + xbar*xbar*svar1;
System.out.println("R^2 = " + R2);
System.out.println("std error of beta_1 = " + Math.sqrt(svar1));
System.out.println("std error of beta_0 = " + Math.sqrt(svar0));
svar0 = svar * sumx2 / (n * xxbar);
System.out.println("std error of beta_0 = " + Math.sqrt(svar0));

System.out.println("SSTO = " + yybar);
System.out.println("SSE = " + rss);
System.out.println("SSR = " + ssr);
}
}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is any idea for java code writing linear regression?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Statistics

Do you need more than 10 observations in linear regression?

This is a difficult question to answer. The pure answer is no. In reality, it depends on the level of randomness in the data. If you plot the data, it will give you an idea of the randomness. Even with 10 data points, 1 or 2 outliers can significantly change the regression equation. I am not aware of a rule of thumb on the minimum number of data points. Obviously, the more the better. Also, calculate the correlation coefficient. Be sure to follow the rules of regression. See the following website: http:/www.duke.edu/~rnau/testing.htm


Why is it a good idea to make a graph of the data in the data table?

Viewing the data is an easy way to see some of their characteristics such as trends, seasonality, outliers, relationship between variables (linear, quadratic, power etc).


What is the meaning of preconception?

a preconceived idea or prejudice.


What is the answer to each ordered pair in the table represents a point use the table to write each set of ordered pairs?

Do you not think that, when instructed to "use the table to ... " it would have helped to have at least some idea about the table.Do you not think that, when instructed to "use the table to... " it would have helped to have at least some idea about the table.Do you not think that, when instructed to "use the table to... " it would have helped to have at least some idea about the table.Do you not think that, when instructed to "use the table to... " it would have helped to have at least some idea about the table.


Is areas within a continuous probability distribution represent probabilities?

i dont have an idea

Related questions

For what is Thomas Kuhn most famous?

Thomas Kuhn was most famous for writing "The Structure of Scientific Revolutions" in 1962, which introduced the idea of "paradigm shifts" as an alternative idea to linear progression.


Do you need more than 10 observations in linear regression?

This is a difficult question to answer. The pure answer is no. In reality, it depends on the level of randomness in the data. If you plot the data, it will give you an idea of the randomness. Even with 10 data points, 1 or 2 outliers can significantly change the regression equation. I am not aware of a rule of thumb on the minimum number of data points. Obviously, the more the better. Also, calculate the correlation coefficient. Be sure to follow the rules of regression. See the following website: http:/www.duke.edu/~rnau/testing.htm


What is the central idea of a writing?

The central idea of a writing is the theme.


Is Christianity a linear or cyclic religion?

Linear. Buddhism is an example of cyclic because of the idea of rebirth.


Why was Hammurabi's code a major step forward fo humankind?

Hammurabi was a leader so he thought of the idea for writing laws down.


Difference between writing software and developing software?

Writing software is a part of developing software. The actual process of writing code can be called "writing software." "Developing software" includes includes the entire process of turning an idea into a program: everything from requirements and specifications to writing software.


Difference between linear consumption function and non linear consumption function?

I have no idea. However, in theory there is a difference.


What is a non-example of linear?

It is possible to give an example of non-linear, but I have no idea what a non-example is. Is a non-example of linear a curve. That would be my first thought but not sure


The central idea of writing?

The central idea of a writing is the theme!


How do you describe writing?

Creative Writing is a genre that can fit any book, fiction nonfiction or poem. The idea of Creative Writing is creative, and has mainly an original idea for stories.


How do you do creative writing?

Creative Writing is a genre that can fit any book, fiction nonfiction or poem. The idea of Creative Writing is creative, and has mainly an original idea for stories.


How do you fill in missing numbers of linear sequence hexagon lines?

mo idea