Home » Analytics and Data Mining » See it for yourself: analytics in IMSL C# vs. R – Part II

In part I of our series, we walked through two examples – Chi-squared test and contingency table – that were recently discussed in an article in MSDN Magazine that discusses the need for statistical analysis and introduces the R language as an option to perform these statistical duties.

Now, we’d like to look at another example and see how the same numerical results can be achieved in less time and with less complexity using the IMSL C# library.

Example – Graphics

R is a powerful graphical tool, able to create presentation-quality graphics. The IMSL C# library also contains graphical capabilities. Although it’s limited to 2D graphics, there are many useful chart types to quickly visualize your data. Output is supported for both Windows.Forms applications and ASP.NET. The example in the MSDN article plots the following equation:

``z=xe^(-(x^2+y^2 ) ) for -2≤x≤2 , -2≤y≤2 ``

Computing the values for z=f(x,y) should be straightforward for any C# developer. To create a contour chart using the IMSL C# library takes a little bit of code, although it could be shorter if you’re happy with a greyscale chart. The length of the code is largely to configure the `ContourLevel` object and mapping values to a color table. In the following code example, values for the arrays x, y, and z were computed on a 50 by 50 grid prior to this block; `zRange` is hard coded for convenience. Many formatting and display options are available to further customize the chart.

``````Chart c = this.Chart;
AxisXY axis = new AxisXY(c);

int N = 21;
double[] zRange = { -0.5, 0.5 };
double[] cLevels = new double[N];
for (int i = 0; i < cLevels.Length; i++)
{
cLevels[i] = (double)i * (zRange - zRange) / (N - 1.0) + zRange;
}
Contour con = new Contour(axis, x, y, z, cLevels);

Axis.AxisY.AxisTitle.SetTitle("Y");
axis.AxisX.AxisTitle.SetTitle("X");
c.ChartTitle.SetTitle("z = x * e^(-(x^2 + y^2))");

for (int i = 0; i <= N; i++)
{
con.GetContourLevel(i).FillColor =
Colormap_Fields.BLUE_GREEN_RED_YELLOW.GetColor(1.0 - (i / (N * 1.0)));
}
con.ContourLegend.IsVisible = true;
``````

This code exists in a short `Windows.Form` class that extends the IMSL `FrameChart` class, and produces the image shown below. 