using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using RDotNet;
using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 값 배열 1
/// </summary>
private double[] sourceValueArray1;
/// <summary>
/// 소스 값 배열 2
/// </summary>
private double[] sourceValueArray2;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
string pathValue = Environment.GetEnvironmentVariable("Path");
string rBinarayPath = @"C:\Program Files\R\R-3.4.1\bin\x64;C:\Program Files\R\R-3.4.1\bin\i386";
Environment.SetEnvironmentVariable("Path", pathValue + Path.PathSeparator + rBinarayPath);
this.runButton.Click += runButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)
/// <summary>
/// 실행 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void runButton_Click(object sender, EventArgs e)
{
this.sourceValueArray1 = GetSourceValueArray(1000000);
this.sourceValueArray2 = GetSourceValueArray(1000000);
CacluateWithR();
CalculateWithNMATH();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 소스 값 배열 구하기 - GetSourceValueArray(count)
/// <summary>
/// 소스 값 배열 구하기
/// </summary>
/// <param name="count">카운트</param>
/// <returns>소스 값 배열</returns>
private double[] GetSourceValueArray(int count)
{
double[] valueArray = new double[count];
Random random = new Random(DateTime.Now.Millisecond);
for(int i = 0; i < count; i++)
{
valueArray[i] = random.NextDouble() * 100;
}
return valueArray;
}
#endregion
#region 계산하기 (R) - CacluateWithR()
/// <summary>
/// 계산하기 (R)
/// </summary>
private void CacluateWithR()
{
Stopwatch stopWatch = new Stopwatch();
using(REngine rEngine = REngine.CreateInstance("RDotNet"))
{
rEngine.Initialize();
#region 데이터를 변환한다.
stopWatch.Start();
//NumericVector numericVector1 = rEngine.CreateNumericVector(this.sourceValueArray1);
//NumericVector numericVector2 = rEngine.CreateNumericVector(this.sourceValueArray2);
NumericVector numericVector1 = new NumericVector(rEngine, this.sourceValueArray1);
NumericVector numericVector2 = new NumericVector(rEngine, this.sourceValueArray2);
stopWatch.Stop();
long dataConversionTime = stopWatch.ElapsedMilliseconds;
#endregion
rEngine.SetSymbol("group1", numericVector1);
rEngine.SetSymbol("group2", numericVector2);
#region 평균 값을 계산한다.
stopWatch.Restart();
double averageValue = rEngine.Evaluate("mean(group1)").AsNumeric().First();
stopWatch.Stop();
long averageValueTime = stopWatch.ElapsedMilliseconds;
#endregion
#region 중간 값을 계산한다.
stopWatch.Restart();
double medianValue = rEngine.Evaluate("median(group1)").AsNumeric().First();
stopWatch.Stop();
long medianValueTime = stopWatch.ElapsedMilliseconds;
#endregion
#region 표준 편차 값을 계산한다.
stopWatch.Restart();
double standardDeviationValue = rEngine.Evaluate("sd(group1)").AsNumeric().First();
stopWatch.Stop();
long standardDeviationValueTime = stopWatch.ElapsedMilliseconds;
#endregion
#region 상관 계수 값을 계산한다.
stopWatch.Restart();
double correlationValue = rEngine.Evaluate("cor(group1, group2)").AsNumeric().First();
stopWatch.Stop();
long correlationValueTime = stopWatch.ElapsedMilliseconds;
#endregion
#region ANOVA 값을 계산한다.
stopWatch.Restart();
GenericVector genericVector = rEngine.Evaluate("t.test(group1, group2)").AsList();
double pValue = genericVector["p.value"].AsNumeric().First();
stopWatch.Stop();
long pValueTime = stopWatch.ElapsedMilliseconds;
#endregion
this.dataConversionTimeTextBox1.Text = dataConversionTime.ToString();
this.avergageValueTextBox1.Text = averageValue.ToString();
this.averageValueTimeTextBox1.Text = averageValueTime.ToString();
this.medianValueTextBox1.Text = medianValue.ToString();
this.medianValueTimeTextBox1.Text = medianValueTime.ToString();
this.standardDeviationValueTextBox1.Text = standardDeviationValue.ToString();
this.standardDeviationValueTimeTextBox1.Text = standardDeviationValueTime.ToString();
this.correlationValueTextBox1.Text = correlationValue.ToString();
this.correlationValueTimeTextBox1.Text = correlationValueTime.ToString();
this.anovaValueTextBox1.Text = pValue.ToString();
this.anovaValueTimeTextBox1.Text = pValueTime.ToString();
}
}
#endregion
#region 계산하기 (NMATH) - CalculateWithNMATH()
/// <summary>
/// 계산하기 (NMATH)
/// </summary>
private void CalculateWithNMATH()
{
Stopwatch stopwatch = new Stopwatch();
#region 데이터를 변환한다.
stopwatch.Start();
DoubleVector doubleVector1 = new DoubleVector(this.sourceValueArray1);
DoubleVector doubleVector2 = new DoubleVector(this.sourceValueArray2);
stopwatch.Stop();
long dataConversionTime = stopwatch.ElapsedMilliseconds;
#endregion
#region 평균 값을 계산한다.
stopwatch.Restart();
double averageValue = NMathFunctions.Mean(doubleVector1);
stopwatch.Stop();
long averageValueTime = stopwatch.ElapsedMilliseconds;
#endregion
#region 중간 값을 계산한다.
stopwatch.Restart();
double medianValue = NMathFunctions.Median(doubleVector1);
stopwatch.Stop();
long medianValueTime = stopwatch.ElapsedMilliseconds;
#endregion
#region 표준 편차 값을 계산한다.
stopwatch.Restart();
double standardDeviationValue = StatsFunctions.StandardDeviation(doubleVector1);
stopwatch.Stop();
long standardDeviationValueTime = stopwatch.ElapsedMilliseconds;
#endregion
#region 상관 계수 값을 계산한다.
stopwatch.Restart();
double correlationValue = StatsFunctions.Correlation(doubleVector1, doubleVector2);
stopwatch.Stop();
long correlationValueTime = stopwatch.ElapsedMilliseconds;
#endregion
#region ANOVA 값을 계산한다.
stopwatch.Restart();
OneWayAnova oneWayAnova = new OneWayAnova();
oneWayAnova.SetData(new DoubleVector[] { doubleVector1, doubleVector2 });
double pValue = oneWayAnova.AnovaTable.FStatisticPValue;
stopwatch.Stop();
long pValueTime = stopwatch.ElapsedMilliseconds;
#endregion
this.dataConversionTimeTextBox2.Text = dataConversionTime.ToString();
this.averageValueTextBox2.Text = averageValue.ToString();
this.averageValueTimeTextBox2.Text = averageValueTime.ToString();
this.medianValueTextBox2.Text = medianValue.ToString();
this.medianValueTimeTextBox2.Text = medianValueTime.ToString();
this.standardDeviationValueTextBox2.Text = standardDeviationValue.ToString();
this.standardDeviationValueTimeTextBox2.Text = standardDeviationValueTime.ToString();
this.correlationValueTextBox2.Text = correlationValue.ToString();
this.correlationValueTimeTextBox2.Text = correlationValueTime.ToString();
this.anovaValueTextBox.Text = pValue.ToString();
this.anovaValueTimeTextBox.Text = pValueTime.ToString();
}
#endregion
}
}