Multiple Linear Regression Example (C#)
[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
* All rights reserved.
*
* Use of this copyright notice is precautionary only, and does not imply
* publication or disclosure. The content of this work contains confidential
* and proprietary information of Simplexar Software Limited. Any duplication,
* modification, distribution, or disclosure in any form, in whole, or in part,
* is strictly prohibited without express prior written permission.
*/
// Get namespaces.
using System;
using Simplexar.Statsar.LinearAlgebra;
// Start namespace.
namespace Simplexar.Examples.Statsar
{
/// <summary>
/// This example explains how to perform multiple linear regression
/// by fitting a linear equation to multiple variables.
/// </summary>
public static class Example
{
/// <summary>
/// The application entry point.
/// </summary>
public static void Main()
{
try
{
RunExample();
}
catch(Exception exception)
{
Console.WriteLine(exception);
}
Console.WriteLine("Press ENTER to terminate.");
Console.ReadLine();
}
private static void RunExample()
{
// Multiple linear regression is similar to simple linear
// regression with the exception that multiple predictor
// vectors are used to generate a response. Consider the
// following two predictor vectors:
// First predictor.
Vector x1 = new Vector(
-1.039, -1.018, -0.986, -0.001, -0.043,
0.021, 1.012, 0.998, 1.033, 1.977);
Console.WriteLine("X1:");
Console.WriteLine(x1);
Console.WriteLine();
// Second predictor.
Vector x2 = new Vector(
-0.953, -0.017, 0.959, -1.02, -0.044,
0.957, -1.014, 0.035, 0.983, 1.999);
Console.WriteLine("X2:");
Console.WriteLine(x2);
Console.WriteLine();
// We also require a response vector:
Vector y = new Vector(
0.999, -0.46, -1.97, 1.448, 0.048,
-1.342, 1.956, 0.521, -0.892, -2.016);
Console.WriteLine("Y:");
Console.WriteLine(y);
Console.WriteLine();
// Perform linear regression with no intercept (false value)
// between Y, X1 and X2.
LinearRegression regression
= new LinearRegression(false, y, x1, x2);
// Display model.
Console.WriteLine(regression.ModelText);
// Access parameters.
Console.WriteLine(
"Parameter 0: {0}", regression.Parameters[0]);
Console.WriteLine(
"Parameter 1: {0}", regression.Parameters[1]);
// Standard deviation for parameter 0.
Console.WriteLine(
"Standard deviation for parameter 0: {0}",
regression.GetStandardDeviation(0));
// Confidence interval (5%) for parameter 0.
double alpha = 0.05;
Console.WriteLine(
"Confidence interval (5%) for parameter 0: {0}",
regression.GetConfidenceInterval(alpha, 0));
// Model statistics.
Console.WriteLine(
"Model variance: {0}", regression.Variance);
Console.WriteLine(
"Model standard deviation: {0}",
regression.StandardDeviation);
Console.WriteLine(
"Coefficient of determination: {0}",
regression.CoefficientOfDetermination);
Console.WriteLine(
"Adjusted coefficient of determination: {0}",
regression.AdjustedCoefficientOfDetermination);
Console.WriteLine(
"Mean response: {0}",
regression.MeanResponse);
// Prediction.
double predictor = 0.0034;
double response = regression.GetPrediction(predictor);
Console.WriteLine(
"Predictor = {0}, Response = {1}",
predictor, response);
// Prediction interval (5%) for predictor.
Console.WriteLine(
"Prediction interval (5%) for predictor: {0}",
regression.GetPredictionInterval(alpha, predictor));
// Covariance matrix.
Console.WriteLine("Covariance matrix:");
Console.WriteLine(regression.CovarianceMatrix);
}
}
}