LinearRegression Type

This LinearRegression type summarized the most common fitting procedures.

Example

 
   // e.g. days since experiment start
   let xData = vector [|1. .. 100.|]
   // e.g. plant size in cm
   let yData = vector [|4.;7.;8.;9.;7.;11.; ...|]
   
   // Estimate the intercept and slope of a line, that fits the data.
   let coefficientsSimpleLinear = 
       LinearRegression.fit(xData,yData,FittingMethod=Fitting.Method.SimpleLinear,Constraint=Fitting.Constraint.RegressionThroughOrigin)
   
   // Predict the size on day 10.5
   LinearRegression.predict(coefficientsSimpleLinear) 10.5

Constructors

Constructor Description

LinearRegression()

Full Usage: LinearRegression()

Returns: LinearRegression
Returns: LinearRegression

Static members

Static member Description

LinearRegression.fit (xData, yData, ?FittingMethod)

Full Usage: LinearRegression.fit (xData, yData, ?FittingMethod)

Parameters:
    xData : matrix - matrix of x vectors
    yData : Vector<float> - vector of y values
    ?FittingMethod : Method - Multivariate regression currently just supports Fitting.SimpleLinear (straight line).

Returns: Coefficients Linear regression coefficients for multivariate regression.

Determines coefficients for multivariate linear regression.

Default is simple linear regression fitting without constraints.

xData : matrix

matrix of x vectors

yData : Vector<float>

vector of y values

?FittingMethod : Method

Multivariate regression currently just supports Fitting.SimpleLinear (straight line).

Returns: Coefficients

Linear regression coefficients for multivariate regression.

Example

 
   // x vectors
   let xData = 
       matrix [
           [1.; 1. ;2.  ]
           [2.; 0.5;6.  ]
           [3.; 0.8;10. ]
           [4.; 2. ;14. ]
           [5.; 4. ;18. ]
           [6.; 3. ;22. ]
       ]
   // measured feature
   let yData = vector [107.5; 110.0; 115.7; 125.0; 137.5; 138.0]
   
   // Estimate linear coefficients for a straight line that fits the data.
   let coefficientsSimpleLinear = 
       LinearRegression.fit(xData, yData)

LinearRegression.fit (xData, yData, ?FittingMethod, ?Constraint, ?Weighting)

Full Usage: LinearRegression.fit (xData, yData, ?FittingMethod, ?Constraint, ?Weighting)

Parameters:
    xData : vector - vector of x values
    yData : Vector<float> - vector of y values
    ?FittingMethod : Method - Either Fitting.SimpleLinear (straight line), Fitting.Polynomial (polynomial), or Fitting.Robust (outlier insensitive straight line).
    ?Constraint : Constraint<float * float> - Either Fitting.Unconstrained, Fitting.RegressionThroughOrigin, or Fitting.RegressionThroughXY (x,y) with x,y beeing coordinates that the line must pass.
    ?Weighting : vector - If a pointwise weight should be attached, a weight vector can be given, that is in the same order as x/y values.

Returns: Coefficients Linear regression coefficients.

Determines coefficients for univariate linear regression.

Default is simple linear regression fitting without constraints.

xData : vector

vector of x values

yData : Vector<float>

vector of y values

?FittingMethod : Method

Either Fitting.SimpleLinear (straight line), Fitting.Polynomial (polynomial), or Fitting.Robust (outlier insensitive straight line).

?Constraint : Constraint<float * float>

Either Fitting.Unconstrained, Fitting.RegressionThroughOrigin, or Fitting.RegressionThroughXY (x,y) with x,y beeing coordinates that the line must pass.

?Weighting : vector

If a pointwise weight should be attached, a weight vector can be given, that is in the same order as x/y values.

Returns: Coefficients

Linear regression coefficients.

Example

 
   // e.g. days since experiment start
   let xData = vector [|1.; 2.; 3.; 4.; 5.; 6. |]
   // e.g. plant size in cm
   let yData = vector [|4.; 7.; 8.; 9.; 7.; 11.|]
   
   // Estimate the intercept and slope of a line, that fits the data.
   let coefficientsSimpleLinear = 
       LinearRegression.fit(xData,yData,FittingMethod=Fitting.Method.SimpleLinear,Constraint=Fitting.Constraint.RegressionThroughXY (1.,2.))
   
   // Estimate the coefficients of a cubic polynomial that fits the data with the given point weighting.
   let coefficientsPolynomial = 
       LinearRegression.fit(xData, yData, FittingMethod=Fitting.Method.Polynomial 3, Weighting = vector [2.; 1.; 0.5.; 1.; 1.; 1.])
   
   // Estimate the intercept and slope of a line, that fits the data and ignore outliers.
   let coefficientsRobust = 
       LinearRegression.fit(xData,yData,FittingMethod=Fitting.Method.Robust Fitting.RobustEstimator.Theil)

LinearRegression.predict coeff xValue

Full Usage: LinearRegression.predict coeff xValue

Parameters:
    coeff : Coefficients - Linear regression coefficients (e.g. from LinearRegression.fit())
    xValue : float

Returns: float Prediction function that takes an x value and predicts its corresponding y value.

Creates prediction function for linear regression.

coeff : Coefficients

Linear regression coefficients (e.g. from LinearRegression.fit())

xValue : float
Returns: float

Prediction function that takes an x value and predicts its corresponding y value.

Example

 
   // e.g. days since experiment start
   let xData = vector [|1.; 2.; 3.; 4.; 5.; 6. |]
   // e.g. plant size in cm
   let yData = vector [|4.; 7.; 8.; 9.; 7.; 11.|]
   
   // Estimate the intercept and slope of a line, that fits the data.
   let coefficientsSimpleLinear = 
       LinearRegression.fit(xData,yData,FittingMethod=Fitting.Method.SimpleLinear,Constraint=Fitting.Constraint.RegressionThroughOrigin)
   
   // Predict the size on day 10.5
   LinearRegression.predict(coefficientsSimpleLinear) 10.5

LinearRegression.predictMultivariate coeff xVector

Full Usage: LinearRegression.predictMultivariate coeff xVector

Parameters:
    coeff : Coefficients - Multivariate linear regression coefficients (e.g. from LinearRegression.fit())
    xVector : vector

Returns: float Prediction function that takes an x vector and predicts its corresponding y value.

Creates prediction function for multivariate linear regression.

coeff : Coefficients

Multivariate linear regression coefficients (e.g. from LinearRegression.fit())

xVector : vector
Returns: float

Prediction function that takes an x vector and predicts its corresponding y value.

Example

 
   let xVectorMulti =
       [
       [1.; 1. ;2.  ]
       [2.; 0.5;6.  ]
       [3.; 0.8;10. ]
       [4.; 2. ;14. ]
       [5.; 4. ;18. ]
       [6.; 3. ;22. ]
       ]
       |> Matrix.ofJaggedSeq
   
   // Here the x values are transformed. In most cases the y values are just provided.
   let yVectorMulti = 
       let transformX (x) =
           x
           |> Matrix.mapiRows (fun _ v -> 100. + (v.[0] * 2.5) + (v.[1] * 4.) + (v.[2] * 0.5))
       xVectorMulti
       |> transformX
       |> vector
   
   // Estimate the intercept and slope of a line, that fits the data.
   let coefficientsSimpleLinear = 
       LinearRegression.fit(xVectorMulti,yVectorMulti,FittingMethod=Fitting.Method.SimpleLinear)
   
   // Predict the size on day 10.5
   LinearRegression.predictMultivariate(coefficientsSimpleLinear) (vector [1.;2.;3.;])