Class LinearRegression


  • public class LinearRegression
    extends Object
    The LinearRegression 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 response y given the value of the predictor variable x.
      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 variable
        y - 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 response y given the value of the predictor variable x.
        Parameters:
        x - the value of the predictor variable
        Returns:
        the expected response y given the value of the predictor variable x
      • toString

        public String toString()
        Returns a string representation of the simple linear regression model.
        Overrides:
        toString in class Object
        Returns:
        a string representation of the simple linear regression model, including the best-fit line and the coefficient of determination R2