Class LinearRegression

java.lang.Object
comp3111.covid.LinearRegression

public class LinearRegression
extends java.lang.Object
The comp3111.covid.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.
  • Constructor Summary

    Constructors 
    Constructor Description
    LinearRegression​(double[] x, double[] y)
    Performs a linear regression on the data points (y[i], x[i]).
  • Method Summary

    Modifier and Type Method Description
    (package private) static LinearRegression fromSeries​(javafx.scene.chart.XYChart.Series<? extends java.lang.Number,​? extends java.lang.Number> data)
    Factory constructor for comp3111.covid.LinearRegression.
    javafx.scene.chart.XYChart.Series<java.lang.Float,​java.lang.Float> generateMockData()
    Creates a set of mock data that fit the regression model.
    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.
    java.lang.String toString()
    Returns a string representation of the simple linear regression model.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • 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:
      java.lang.IllegalArgumentException - if the lengths of the two arrays are not equal
  • Method Details

    • fromSeries

      static LinearRegression fromSeries​(javafx.scene.chart.XYChart.Series<? extends java.lang.Number,​? extends java.lang.Number> data)
      Factory constructor for comp3111.covid.LinearRegression.

      It converts Series data into two indepdent array x and y, and pass them into comp3111.covid.LinearRegression().

      Parameters:
      data - series data
      Returns:
      an instance of comp3111.covid.LinearRegression.
    • generateMockData

      public javafx.scene.chart.XYChart.Series<java.lang.Float,​java.lang.Float> generateMockData()
      Creates a set of mock data that fit the regression model.
      Returns:
      mock data
    • 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 java.lang.String toString()
      Returns a string representation of the simple linear regression model.
      Overrides:
      toString in class java.lang.Object
      Returns:
      a string representation of the simple linear regression model, including the best-fit line and the coefficient of determination R2