Data Analysis Mathematics Linear Algebra Statistics
New Version 6.0!

QuickStart Samples

# Multiple Linear Regression QuickStart Sample (F#)

Illustrates how to use the LinearRegressionModel class to perform a multiple linear regression in F#.

```// Illustrates building multiple linear regression models using
// the LinearRegressionModel class in the
// Extreme.Statistics namespace of the Extreme
// Optimization Numerical Libraries for .NET.

#light

open System

open System.Data
open System.IO

open Extreme.DataAnalysis
open Extreme.Mathematics
open Extreme.Statistics

// Multiple linear regression can be performed using
// the LinearRegressionModel class.
//
// This QuickStart sample uses data test scores of 200 high school
// students, including science, math, and reading.

// First, read the data from a file into a data frame.

// Now create the regression model. Parameters are the data frame,
// the name of the dependent variable, and a string array containing
// the names of the independent variables.
let model = LinearRegressionModel(data,
"science", [| "math"; "female"; "socst"; "read" |])

// Alternatively, we can use a formula to describe the variables
// in the model. The dependent variable goes on the left, the
// independent variables on the right of the ~:
let model2 = LinearRegressionModel(data,
"science ~ math + female + socst + read")

// We can set model options now, such as whether to exclude
// the constant term:
// model.NoIntercept <- false

// The Compute method performs the actual regression analysis.
model.Compute()

// The Parameters collection contains information about the regression
// parameters.
printfn "Variable  Value    Std.Error  t-stat  p-Value"
for parameter in model.Parameters do
// Parameter objects have the following properties:
printfn "%-20s%10.6f%10.6f%8.2f %7.5f"
// Name, usually the name of the variable:
parameter.Name
// Estimated value of the parameter:
parameter.Value
// Standard error:
parameter.StandardError
// The value of the t statistic for the hypothesis that the parameter
// is zero.
parameter.Statistic
// Probability corresponding to the t statistic.
parameter.PValue
printfn ""

// In addition to these properties, Parameter objects have a GetConfidenceInterval
// method that returns a confidence interval at a specified confidence level.
// Notice that individual parameters can be accessed using their numeric index.
// Parameter 0 is the intercept, if it was included.
let confidenceInterval = model.Parameters.[0].GetConfidenceInterval(0.95)
printfn "95%% confidence interval for intercept: %.4f - %.4f"
confidenceInterval.LowerBound confidenceInterval.UpperBound

// Parameters can also be accessed by name:
let confidenceInterval2 = model.Parameters.Get("math").GetConfidenceInterval(0.95)
printfn "95%% confidence interval for 'math': %.4f - %.4f"
confidenceInterval2.LowerBound confidenceInterval2.UpperBound
printfn ""

// There is also a wealth of information about the analysis available
// through various properties of the LinearRegressionModel object:
printfn "Residual standard error: %.3f" model.StandardError
printfn "R-Squared:   %.4f" model.RSquared