Simple Regression in C# QuickStart Sample

Illustrates how to perform a simple linear regression using the SimpleRegressionModel class in C#.

View this sample in: Visual Basic F# IronPython

using System;

using Extreme.DataAnalysis;
using Extreme.Mathematics;
using Extreme.Statistics;

namespace Extreme.Numerics.QuickStart.CSharp
{
    /// <summary>
    /// Illustrates the use of the SimpleRegressionModel class 
    /// to perform multiple linear regression.
    /// </summary>
    class SimpleRegression
    {
        static void Main(string[] args)
        {
            // The license is verified at runtime. We're using
            // a demo license here. For more information, see
            // https://numerics.net/trial-key
            Extreme.License.Verify("Demo license");
            // Simple linear regression can be performed using 
            // the SimpleRegressionModel class. There are some special constructors
            // for simple linear regression, with only one independent variable.
            //
            // This QuickStart sample uses data from the National Institute
            // for Standards and Technology's Statistical Reference Datasets
            // library at http://www.itl.nist.gov/div898/strd/.

            // Note that, due to round-off error, the results here will not be exactly
            // the same as the NIST results, which were calculated using 500 digits
            // of precision!

            // Model 1 uses the 'NoInt1' dataset. The model has no intercept.

            // First, we construct Double arrays containing the data for
            // the dependent and independent variables.
            double[] yData1 = {130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140};
            double[] xData1 = {60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70};

            // Next, we create the regression model. We can pass the data arrays directly.
            SimpleRegressionModel model1 = new SimpleRegressionModel(yData1, xData1);
            model1.NoIntercept = true;
            model1.Fit();

            foreach(var parameter in model1.Parameters)
                Console.WriteLine(parameter.ToString());
            Console.WriteLine("Residual standard error: {0:F2}", model1.StandardError);
            Console.WriteLine("R-Squared: {0:F3}", model1.RSquared);
            Console.WriteLine("Adjusted R-Squared: {0:F3}", model1.AdjustedRSquared);
            Console.WriteLine("F-statistic: {0:F3}", model1.FStatistic);

            Console.WriteLine(model1.AnovaTable.ToString());

            // Model 2 uses the 'Norris' dataset.

            Console.WriteLine("\n\nModel 2");
            var dependent2 = Vector.Create(new double[] {
                0.1, 338.8, 118.1, 888.0, 9.2, 228.1, 668.5, 998.5,
                449.1, 778.9, 559.2, 0.3, 0.1, 778.1, 668.8, 339.3, 
                448.9, 10.8, 557.7, 228.3, 998.0, 888.8, 119.6, 0.3, 
                0.6, 557.6, 339.3, 888.0, 998.5, 778.9,  10.2 , 117.6, 
                228.9, 668.4, 449.2,   0.2});
            var independent2 = Vector.Create(new double[] { 
                0.2, 337.4, 118.2, 884.6, 10.1, 226.5, 666.3, 996.3, 
                448.6, 777.0, 558.2, 0.4, 0.6, 775.5, 666.9, 338.0, 
                447.5, 11.6, 556.0, 228.1, 995.8, 887.6, 120.2, 0.3, 
                0.3, 556.8, 339.1, 887.2, 999.0, 779.0, 11.1, 118.3, 
                229.2, 669.1, 448.9, 0.5});

            // Next, we create the regression model, using the NumericalVariable objects
            // we just created:
            SimpleRegressionModel model2 = new SimpleRegressionModel(dependent2, independent2);
            model2.Fit();

            foreach(var parameter in model2.Parameters)
                Console.WriteLine(parameter.ToString());
            Console.WriteLine("Residual standard error: {0:F8}", model2.StandardError);
            Console.WriteLine("R-Squared: {0:F8}", model2.RSquared);
            Console.WriteLine("Adjusted R-Squared: {0:F8}", model2.AdjustedRSquared);
            Console.WriteLine("F-statistic: {0:F3}", model2.FStatistic);

            Console.WriteLine(model2.AnovaTable.ToString());

            // The data can also be supplied as two vectors.
            // This is not illustrated here.

            Console.Write("Press any key to exit.");
            Console.ReadLine();
        }
    }
}