C# Missing Values Example

[TOC]

using System;
using System.Globalization;
using System.Threading;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Core.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to perform statistical operations on a matrix containing
  /// missing data.
  /// </summary>
  class MissingValuesExample
  {

    static void Main(string[] args)
    {
      Console.WriteLine();

      CultureInfo original = Thread.CurrentThread.CurrentCulture;

      // This example uses strings representing numbers in the US locale
      // so change the current culture info.  For example, "0.446"
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

      // Matrix A contains experimental drug results. Rows represent data for seven patients.
      // Columns represent cholesterol levels before and after a drug regimen. No data is
      // available for two of the patients.
      DoubleMatrix A = new DoubleMatrix("7x2 [280 260 NaN NaN 335 338 310 220 NaN NaN 378 335 288 279]");

      // Back to your original culture.
      Thread.CurrentThread.CurrentCulture = original;

      // Average the columns and display results
      DisplayResults(NMathFunctions.NaNMean(A));

      // Throw out patient #3
      A.Set(2, Slice.All, Double.NaN);

      // Average the columns and display results
      DisplayResults(NMathFunctions.NaNMean(A));

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

    }

    static void DisplayResults(DoubleVector results)
    {
      Console.WriteLine();
      Console.WriteLine("Mean cholesterol level before trial: " + results[0]);
      Console.WriteLine("Mean cholesterol level after trial: " + results[1]);
      Console.WriteLine("Mean drop in cholesterol level: " + (results[1] - results[0]));
    }

  }// class

}// namespace

[TOC]