Package edu.princeton.cs.algs4
Class LinearRegression
- Object
-
- edu.princeton.cs.algs4.LinearRegression
-
public class LinearRegression extends Object
TheLinearRegression
class performs a simple linear regression on an set of n data points (yi, xi). That is, it fits a straight line y = α + β x, (where y is the response variable, x is the predictor variable, α is the y-intercept, and β is the slope) that minimizes the sum of squared residuals of the linear regression model. It also computes associated statistics, including the coefficient of determination R2 and the standard deviation of the estimates for the slope and y-intercept.- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description LinearRegression(double[] x, double[] y)
Performs a linear regression on the data points(y[i], x[i])
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description double
intercept()
Returns the y-intercept α of the best of the best-fit line y = α + β x.double
interceptStdErr()
Returns the standard error of the estimate for the intercept.double
predict(double x)
Returns the expected responsey
given the value of the predictor variablex
.double
R2()
Returns the coefficient of determination R2.double
slope()
Returns the slope β of the best of the best-fit line y = α + β x.double
slopeStdErr()
Returns the standard error of the estimate for the slope.String
toString()
Returns a string representation of the simple linear regression model.
-
-
-
Constructor Detail
-
LinearRegression
public LinearRegression(double[] x, double[] y)
Performs a linear regression on the data points(y[i], x[i])
.- Parameters:
x
- the values of the predictor variabley
- the corresponding values of the response variable- Throws:
IllegalArgumentException
- if the lengths of the two arrays are not equal
-
-
Method Detail
-
intercept
public double intercept()
Returns the y-intercept α of the best of the best-fit line y = α + β x.- Returns:
- the y-intercept α of the best-fit line y = α + β x
-
slope
public double slope()
Returns the slope β of the best of the best-fit line y = α + β x.- Returns:
- the slope β of the best-fit line y = α + β x
-
R2
public double R2()
Returns the coefficient of determination R2.- Returns:
- the coefficient of determination R2, which is a real number between 0 and 1
-
interceptStdErr
public double interceptStdErr()
Returns the standard error of the estimate for the intercept.- Returns:
- the standard error of the estimate for the intercept
-
slopeStdErr
public double slopeStdErr()
Returns the standard error of the estimate for the slope.- Returns:
- the standard error of the estimate for the slope
-
predict
public double predict(double x)
Returns the expected responsey
given the value of the predictor variablex
.- Parameters:
x
- the value of the predictor variable- Returns:
- the expected response
y
given the value of the predictor variablex
-
-