6

R.Net 1.5 を使用して、ARIMA を使用して簡単な予測を実行しようとしています。R 2.14 と R 2.15 で試しました。Visual Studio 2012 ターゲット .NET 4 を使用していますが、.NET 4.5 と Visual Studio 2010 も試しました。

ここに私が書いたコードの一部があります:

string rhome = System.Environment.GetEnvironmentVariable("R_HOME");
        if (string.IsNullOrEmpty(rhome))
            rhome = @"C:\Program Files\R\R-2.14.0";

        System.Environment.SetEnvironmentVariable("R_HOME", rhome);
        System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") + ";" + rhome + @"\bin\x64");
        using (REngine engine = REngine.CreateInstance("RDotNet"))
        {

            engine.Initialize();

            NumericVector testGroup = engine.CreateNumericVector(submissions);
            engine.SetSymbol("testGroup", testGroup);
            engine.Evaluate("testTs <- c(testGroup)");
            NumericVector ts = engine.GetSymbol("testTs").AsNumeric();

            engine.Evaluate("tsValue <- ts(testTs, frequency=1, start=c(2010, 1, 1))");
            engine.Evaluate("library(forecast)");
            engine.Evaluate("arimaFit <- auto.arima(tsValue)");
            engine.Evaluate("fcast <- forecast(tsValue, h=36)");
            engine.Evaluate("plot(fcast)");

            NumericVector nv = engine.GetSymbol("fcast").AsNumeric();

数値ベクトルを取得しようとすると失敗します。TI はここでいくつかのエラーを取得します。1 つ目は「エラー: オブジェクトをタイプ 'double' に強制できません」で、2 つ目は「エラー: アクセス違反が発生しました - 慎重に続行してください」です。

予測を GenericVector として取得すると、RDotNet.SymbolicExpressions のリストが取得されます。それらをループして内容を確認しましたが、ARIMA 関数に関連しているように見えますが、実際の予測出力を見つけることができません。入力やその他の関連する値、およびそれらが何であるかを判断できない一連の数値リストを見つけることができます。

Revolution 内でスクリプトを実行すると、出力がどうあるべきかがわかります。それが、R.Net からの出力が正確かどうかを判断する方法です。R.Net が Revolution とは異なる方法で予測を行っている可能性があり (可能性は低いと思いますが)、genericvector の出力の 1 つが実際に正しい出力である可能性があると思います。

GenericVector の初期化は次のとおりです。デバッグ目的のためだけにブランケット try, catch でラップしました。DynamicVector の内部は、実際に詳細を調べることができる場所です。

GenericVector newVector = engine.GetSymbol("fcast").AsList();

            foreach (var vector in newVector)
            {
                try
                {
                    DynamicVector dv = vector.AsVector();
                }
                catch (Exception)
                {
                }
4

1 に答える 1

7

これは R.Net の問題ではありません。間違ったスクリプトを実行しようとしているだけです。純粋な R でコードを実行してみましょう。

> testTs <- c(1, 2, 3)
> tsValue <- ts(testTs, frequency=1, start=c(2010, 1, 1))
> library("forecast")
> arimaFit <- auto.arima(tsValue)
> fcast <- forecast(tsValue, h=36)
> plot(fcast)

現在class(fcast)は次のようになりforecastます:

> class(fcast)
[1] "forecast"
> as.numeric(fcast)
Error: (list) object cannot be coerced to type 'double'

fcast構造:

> str(fcast)
List of 11
 $ method   : chr "Mean"
 $ level    : num [1:2] 80 95
 $ x        : Time-Series [1:3] from 2010 to 2012: 1 2 3
 $ xname    : chr "object"
 $ mean     : Time-Series [1:36] from 2013 to 2048: 2 2 2 2 2 2 2 2 2 2 ...
 $ lower    : mts [1:36, 1:2] -0.177 -0.177 -0.177 -0.177 -0.177 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "80%" "95%"
  ..- attr(*, "tsp")= num [1:3] 2013 2048 1
  ..- attr(*, "class")= chr [1:2] "mts" "ts"
 $ upper    : mts [1:36, 1:2] 4.18 4.18 4.18 4.18 4.18 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "80%" "95%"
  ..- attr(*, "tsp")= num [1:3] 2013 2048 1
  ..- attr(*, "class")= chr [1:2] "mts" "ts"
 $ model    :List of 4
  ..$ mu   : num 2
  ..$ mu.se: num 0.577
  ..$ sd   : num 1
  ..$ call : language meanf(x = object, h = h, level = level, fan = fan)
 $ lambda   : NULL
 $ fitted   : Time-Series [1:3] from 2010 to 2012: NA 1 1.5
 $ residuals: Time-Series [1:3] from 2010 to 2012: NA 1 1.5
 - attr(*, "class")= chr "forecast"
于 2013-02-14T17:41:27.387 に答える