Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Polynomial Regression Example (VB.NET)

[VB.NET]
/* 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 polynomial regression
    /// by fitting a polynomial curve to observed data.
    /// </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\Polynomial.csv");
            Console.WriteLine(sheet);

            // Polynomial regression attempts to fit a polynomial curve
            // to observed data. Note that this is still a special case
            // of multiple linear regression since we try to fit a linear
            // equation to additional high-order variables.
            //
            // The data sheet above stores data for two variables, X and Y.
            // Suppose we conjecture that there is a quadratic relationship
            // between X and Y. We can use linear regression to verify this
            // conjecture. The first step is to create new higher-order
            // variables. In this case, we need to create a new column to
            // hold the square of X, since we are assuming a quadratic
            // relationship.
            //
            // 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.
            // The expression we are looking for is: "X * X".

            sheet.Columns.Create("X^2", "X * X");
            Console.WriteLine(sheet);

            // Perform linear regression with an intercept (true value)
            // between the Y, X and X^2 columns.
            LinearRegression regression
                = new LinearRegression(sheet, true, "Y", "X", "X^2");

            // Display model.
            Console.WriteLine(regression.ModelText);

            // Access parameters.
            Console.WriteLine(
                "Parameter 0: {0}", regression.Parameters[0]);
            Console.WriteLine(
                "Parameter 1: {0}", regression.Parameters[1]);
            Console.WriteLine(
                "Parameter 2: {0}", regression.Parameters[2]);

            // 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);

            // Covariance matrix.
            Console.WriteLine("Covariance matrix:");
            Console.WriteLine(regression.CovarianceMatrix);
        }
    }
}