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
ReneRene
Change my mind. I dare you.
Chat with Rene
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan

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

Trending Questions
What is the term for the average number of years a person is likely to live? What is the probability of rolling an even number and a head when rolling a die and then tossing a coin? What does the slope mean on a line graph? How many different two-letter initials with none of the letters repeated can people have? What is the probability of choosing a king on the second draw if the first was a king without replacement? What does 72 mean in sex? What is the probability of rolling a number greater than or equal to 8 with two dice given that at least one of the dice must show a 6? What is the interquartile range? Clayton has decided to use only qualitative data in his study. Which is an example of the type of information he may collect? What is the highest performing mutual fund averaged more than 20 percent for 2011? A variable whose value is a numerical outcome of a random phenomenon is called? How do you find the excluded values of variables? 6 balls in a bag The probability of taking a red ball out of the bag is 0.5 A red ball is taken out of the bag and put to one side What is the probability of taking another red ball out of the bag? What is sampling without replacement? How many ores are in a pack? Can Non Destructive Testing penetrate Chrome Plating? Why is a voluntary response sample not suitable for a statistical study? What percentage is 280000 of 4.5 million? What is the z score of 70.00? If beta coefficient is 1.4 what is the required rate of return?