answersLogoWhite

0

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

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach

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