I am using R.Net 1.5 to attempt to do a simple forecast using ARIMA. I've tried with R 2.14 and R 2.15. I'm using Visual Studio 2012 target .NET 4 though I've also tried .NET 4.5 and Visual Studio 2010.
Here is a piece of the code I've written:
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();
It fails when I attempt to retrieve the numeric vector. TI get a few errors here. The first is "Error: object cannot be coerced to type 'double'" and the second is "Error: caught access violation - continue with care"
If I retrieve the forecast as a GenericVector I get a list of RDotNet.SymbolicExpressions. I've looped through those to see what they contain and it does seem to be related to the ARIMA function but I cannot locate the actual forecast output. I can find the inputs and other related values as well as a bunch of lists of numbers that I can't determine what they are.
If I run the script within Revolution I can see what the outputs SHOULD be and that is how I'm determining whether the outputs from R.Net are accurate. I suppose it's possible R.Net is performing the forecast differently than Revolution (though unlikely, I think) and one of the outputs in the genericvector is indeed the correct output.
Here is the GenericVector initialization. I wrapped in a blanket try, catch just for debugging purposes: Inside the DynamicVector is where I can actually inspect the details.
GenericVector newVector = engine.GetSymbol("fcast").AsList();
foreach (var vector in newVector)
{
try
{
DynamicVector dv = vector.AsVector();
}
catch (Exception)
{
}
It is not a R.Net problem, you just try to run a wrong script. Let's run your code on pure 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)
Now class(fcast)
equals to forecast
:
> class(fcast)
[1] "forecast"
> as.numeric(fcast)
Error: (list) object cannot be coerced to type 'double'
fcast strucure:
> 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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With