C# Simple Linear Regression Example

[TOC]

using System;
using System.IO;


using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;

namespace CenterSpace.NMath.Stats.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use the linear regression class to perform a simple
  /// linear regression.
  /// </summary>
  /// <remarks>A simple linear regression fits a straight line through a series
  /// of data points. Specifically, the points <c>(x,y)</c> are assumed to 
  /// conform to the equation <c>y = mx + b</c>. A simple linear regression uses
  /// a least squares fit to compute the slope, <c>m</c>, and the y-intercept, <c>b</c>.
  /// </remarks>
  class SimpleLinearRegressionExample
  {

    static void Main(string[] args)
    {
      // Read in data from the file. The data comes from The Data and Story 
      // Library (http://lib.stat.cmu.edu/DASL) and is described below:
      //
      // Results of a study of gas chromatography, a technique which is used 
      // to detect very small amounts of a substance. Five measurements were
      // taken for each of four specimens containing different amounts of the
      // substance. The amounts of the substance in each specimen was determined
      // before the experiment. The responses variable is the output reading 
      // from the gas chromatograph. The purpose of the study is to calibrate
      // the chromatograph by relating the actual amounts of the substance to
      // the chromatograph reading.
      string filename = "..\\..\\SimpleLinearRegressionExample.dat";
      StreamReader dataStream;
      try
      {
        dataStream = new StreamReader(filename);
      }
      catch (FileNotFoundException e)
      {
        string msg = string.Format("Could not find data file {0}.", filename);
        msg += Environment.NewLine;
        msg += e.Message;
        msg += Environment.NewLine;
        msg += "Data file must have the same name as the example source ";
        msg += Environment.NewLine;
        msg += "file and be located two directories up from where the ";
        msg += Environment.NewLine;
        msg += "executable is running.";
        Console.WriteLine(msg);
        return;
      }

      // First read in the independent (or predictor) values. This is a matrix
      // with one column and a row for each amounts measurement.
      DoubleMatrix amounts = new DoubleMatrix(dataStream);

      // Next, read in the resposnes. These are the readings of the gas 
      // chromatograph
      DoubleVector responses = new DoubleVector(dataStream);

      // Print out the amounts and responses values.
      Console.WriteLine();
      Console.WriteLine("amounts = {0}", amounts);
      Console.WriteLine("responses = {0}", responses);

      // Construct a linear regression. If we want our regression to calculate a
      // y-intercept we must send in true for the "addIntercept" parameter (the
      // third paramameter in the constructor).
      LinearRegression regression = new LinearRegression(amounts, responses, true);

      // The y-intercept is the first element of the parameter array returned by 
      // the regression, and the slope is the second.
      Console.WriteLine("y-intercept = {0}", regression.Parameters[0]);
      Console.WriteLine("Slope = {0}", regression.Parameters[1]);

      // What would the model predict for the chromatograph reading for an
      // input amounts of 0.75 and 1.25?
      DoubleVector amount = new DoubleVector(0.75);
      double predictedReading = regression.PredictedObservation(amount);
      Console.WriteLine("Predicted reading for amount {0} is {1}", amount[0], predictedReading);
      amount[0] = 1.25;
      predictedReading = regression.PredictedObservation(amount);
      Console.WriteLine("Predicted reading for amount {0} is {1}", amount[0], predictedReading);

      // Let's look at the coefficient of determination for the model - Rsquared.
      // To do this we will need to construct an Analysis Of VAriance object for 
      // the regression.
      LinearRegressionAnova regressionAnova = new LinearRegressionAnova(regression);
      Console.WriteLine("Rsquare = {0}", regressionAnova.RSquared);

      Console.WriteLine();
      Console.WriteLine("Press Enter Key");
      Console.Read();

    } // Main

  } // class

} // namespace


[TOC]