Simple 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;
using Simplexar.Statsar.LinearAlgebra;
// Start namespace.
namespace Simplexar.Examples.Statsar
{
/// <summary>
/// This example explains how to perform simple linear regression
/// by fitting a linear equation to two 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()
{
// Create a calculator.
StatsCalculator calculator = new StatsCalculator();
// Use the calculator to load the sheet.
DataSheet sheet = calculator.Load(
@"..\..\..\..\..\Data\Temperatures.csv");
Console.WriteLine(sheet);
// The above data sheet holds data for the pressure of a chemical
// substance at different temperatures. Suppose we conjecture that
// there is a linear relationship between the reciprocal of the
// temperature in Kelvin and the logarithm of the pressure. We can
// use linear regression to verify this conjecture.
//
// The first step is to create the new variables (i.e. the
// reciprocal of the temperature in Kelvin and the logarithm of the
// pressure) by transforming the above data sheet. To do this, we
// can use the Create() method on the DataSheet columns collection.
// This method takes two parameters, the new column name and an
// expression to perform the transformation.
//
// We can create the new variables by creating columns for Log(P),
// T then 1/T. To do this, the three expressions we are looking for
// are: "Log(P, 10)", "[T (Celsius)] + 273.15" and "1/T".
//
// The first expression is the logarithm of the P column to base 10.
// The second expression uses square brackets to escape a column
// name since the column name contains spaces. The third expression
// is the reciprocal of the new T column, i.e. the reciprocal of the
// temparture in Kelvin.
sheet.Columns.Create("Log(P)", "Log(P, 10)");
sheet.Columns.Create("T", "[T (Celsius)] + 273.15");
sheet.Columns.Create("1/T", "1/T");
Console.WriteLine(sheet);
// Perform linear regression with an intercept (true value)
// between the Log(P) and 1/T columns.
LinearRegression regression
= new LinearRegression(sheet, true, "Log(P)", "1/T");
// 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);
}
}
}