[C#/COMMON/R.NET] 평균, 중간, 표준 편차, 상관 계수, ANOVA 계산하기
■ 평균, 중간, 표준 편차, 상관 계수, ANOVA를 계산하는 방법을 보여준다. ▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
using System; using System.IO; using System.Collections.Generic; using System.Linq; using RDotNet; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 소스 값 배열 구하기 - GetSourceValueArray(count) /// <summary> /// 소스 값 배열 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>소스 값 배열</returns> private static 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 실행 가능 경로 추가하기 - AddRExecutablePath() /// <summary> /// R 실행 가능 경로 추가하기 /// </summary> private static void AddRExecutablePath() { string pathValue = Environment.GetEnvironmentVariable("Path"); string rExecutablePath = @"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 + rExecutablePath); } #endregion #region 숫자 벡터 설정하기 - SetNumericVector(rEngine, variableName, sourceArray) /// <summary> /// 숫자 벡터 설정하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName">변수명</param> /// <param name="sourceArray">소스 배열</param> private static void SetNumericVector(REngine rEngine, string variableName, double[] sourceArray) { NumericVector numericVector = new NumericVector(rEngine, sourceArray); rEngine.SetSymbol(variableName, numericVector); } #endregion #region 숫자 벡터 설정하기 - SetNumericVector(rEngine, variableName, sourceEnumerable) /// <summary> /// 숫자 벡터 설정하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName">변수명</param> /// <param name="sourceEnumerable">소스 열거형</param> private static void SetNumericVector(REngine rEngine, string variableName, IEnumerable<double> sourceEnumerable) { NumericVector numericVector = new NumericVector(rEngine, sourceEnumerable); rEngine.SetSymbol(variableName, numericVector); } #endregion #region 평균 값 구하기 - GetAverageValue(rEngine, variableName) /// <summary> /// 평균 값 구하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName">변수명</param> /// <returns>평균 값</returns> private static double GetAverageValue(REngine rEngine, string variableName) { double averageValue = rEngine.Evaluate(string.Format("mean({0})", variableName)).AsNumeric().First(); return averageValue; } #endregion #region 중간 값 구하기 - GetMedianValue(rEngine, variableName) /// <summary> /// 중간 값 구하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName">변수명</param> /// <returns>중간 값</returns> private static double GetMedianValue(REngine rEngine, string variableName) { double medianValue = rEngine.Evaluate(string.Format("median({0})", variableName)).AsNumeric().First(); return medianValue; } #endregion #region 표준 편차 값 구하기 - GetStandardDeviationValue(rEngine, variableName) /// <summary> /// 표준 편차 값 구하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName">변수명</param> /// <returns>표준 편차 값</returns> private static double GetStandardDeviationValue(REngine rEngine, string variableName) { double medianValue = rEngine.Evaluate(string.Format("sd({0})", variableName)).AsNumeric().First(); return medianValue; } #endregion #region 상관 계수 값 구하기 - GetCorrelationValue(rEngine, variableName1, variableName2) /// <summary> /// 상관 계수 값 구하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName1">변수명 1</param> /// <param name="variableName2">변수명 2</param> /// <returns>상관 계수 값</returns> private static double GetCorrelationValue(REngine rEngine, string variableName1, string variableName2) { double correlationValue = rEngine.Evaluate(string.Format("cor({0}, {1})", variableName1, variableName2)).AsNumeric().First(); return correlationValue; } #endregion #region ANOVA 값 구하기 - GetANOVAValue(rEngine, variableName1, variableName2) /// <summary> /// ANOVA 값 구하기 /// </summary> /// <param name="rEngine">R 엔진</param> /// <param name="variableName1">변수명 1</param> /// <param name="variableName2">변수명 2</param> /// <returns>상관 계수 값</returns> private static double GetANOVAValue(REngine rEngine, string variableName1, string variableName2) { GenericVector genericVector = rEngine.Evaluate(string.Format("t.test({0}, {1})", variableName1, variableName2)).AsList(); double pValue = genericVector["p.value"].AsNumeric().First(); return pValue; } #endregion #region 프로그램 실행하기 - Main() /// <summary> /// 프로그램 실행하기 /// </summary> private static void Main() { AddRExecutablePath(); // 소스 값 배열을 설정한다. double[] sourceValueArray1 = GetSourceValueArray(1000000); double[] sourceValueArray2 = GetSourceValueArray(1000000); using(REngine rEngine = REngine.CreateInstance("RDotNet")) { rEngine.Initialize(); SetNumericVector(rEngine, "value1", sourceValueArray1); SetNumericVector(rEngine, "value2", sourceValueArray2); // 평균 값을 계산한다. double averageValue = GetAverageValue(rEngine, "value1"); Console.WriteLine("평균 값 : " + averageValue.ToString()); // 중간 값을 계산한다. double medianValue = GetMedianValue(rEngine, "value1"); Console.WriteLine("중간 값 : " + medianValue.ToString()); // 표준 편차 값을 계산한다. double standardDeviationValue = GetStandardDeviationValue(rEngine, "value1"); Console.WriteLine("표준 편차 : " + standardDeviationValue.ToString()); // 상관 계수 값을 계산한다. double correlationValue = GetCorrelationValue(rEngine, "value1", "value2"); Console.WriteLine("상관 계수 : " + correlationValue.ToString()); // ANOVA 값을 계산한다. double pValue = GetANOVAValue(rEngine, "value1", "value2"); Console.WriteLine("ANOVA : " + pValue.ToString()); } } #endregion } } |
※ 상기 소스는 실행에 앞서 R 3.4.1 버전이