Forecast

Oct 7, 2014 at 10:52 AM
Hi Team,

How do we do forecasting in R-3.0.2, I am getting error while executing the code.
Should we include any of the library, if so, then how to we include it.
Oct 13, 2014 at 7:35 AM
This is my code

public static DataFrame Prediction(REngine engine, DataFrame PredrocessedData, string days, string interval)
    {
        NumericVector PreprocessedValue = null;
        DataFrame PredictedData = null;
        try
        {
            PreprocessedValue = engine.CreateNumericVector(PreProcessedList);
            // Assign the Utilization value to R variable UtilValue
            engine.SetSymbol("PreprocessedValue", PreprocessedValue);

            engine.Evaluate("StartDate <- as.Date(" + StartDate + ") " + days);

            engine.Evaluate("size = length(seq(from=as.Date('" + StartDate + "'), by='" + "day" + "', to=as.Date(StartDate)))");

            engine.Evaluate("startDate <- as.POSIXct('" + StartDate + "')");

            engine.Evaluate("endDate <- StartDate + as.difftime(size, units='" + "days" + "')");

            engine.Evaluate("PredictDate = seq(from=startDate, by=" + interval + "*60, to=endDate)");

            engine.Evaluate("freq <- ts(PreprocessedValue, frequency = 20)");

           engine.Evaluate("forecastnavie <-snaive(freq, Datapoints)");

            engine.Evaluate("PredictValue = (forecastnavie$mean)");

            engine.Evaluate("PredictedData = cbind(PredictValue, data.frame(PredictDate))");

            PredictedData = engine.Evaluate("PredictedData").AsDataFrame();

            return PredictedData;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


I am getting the error near snaive function..
Developer
Oct 13, 2014 at 8:31 AM
The code you provided is not self contained; some errors occur at compile or runtime before the call to snaive, and some variables not defined (StartDate?)

Falling back on a simpler and working sample code from the snaive manual page, this works fine with R.NET 1.5.16 at least. See
this sample program I used
or use something like this in your tests.
      private static void ReproDiscussion569196(REngine engine)
      {
         // UserReported(engine);         
         engine.Evaluate("library(forecast)");
         engine.Evaluate("snaiveRes <- snaive(wineind)");
         engine.Evaluate("str(snaiveRes)");         
      }
The R console and/or exceptions also do provide more information than you report if and when an R statement fails to execute.
Oct 13, 2014 at 12:18 PM
Dear jperraud,

Thanks for your reply and support.

When I try to load the library, i get the error.
Right now i am using R-3.1.1. version.

Please let know, how should i solve this.
Developer
Oct 14, 2014 at 7:53 AM
Please be more precise, at least regarding:
  • what library are you talking about? I would assume things now fail when calling engine.Evaluate("library(forecast)");, but this is not obvious (could be a .NET library for all I know)
  • the REngine method `Evaluate' goes to great length to try to retrieve the error message from R, for use in the exception. Please report this message if it is present.
  • Do you have several versions of R installed in parallel on your machine? or did you uninstall other versions than 3.1.1.
Oct 14, 2014 at 8:24 AM
  • Yes, i am talking about engine.Evaluate("library(forecast)");.
  • when i am parsing this line, the compiler throws the exception like
           Error: package or namespace load failed for 'forecast'
           A first chance exception of type 'RDotNet.ParseException' occurred in RDotNet.dll
    
  • i unable to retrieve the error message.
  • I have installed R-3.0.1, R-3.1.1
Oct 17, 2014 at 8:05 PM
yuvaraj1985, I was having a similar issue and it was due to the fact that I didn't have a couple of things setup prior to running my .NET application:
  1. I didn't add the R path (32 and/or 64 bit) to my system's PATH (environment variable).
  2. I hadn't installed the package via the R Client (I am on 3.1.1).
Once I did that, all references to packages have worked great! I added the 32 and 64 bit R bin folders to my system's Path variable to be sure it found the R Engine. Here's the thread where I got into this issue: (https://rdotnet.codeplex.com/discussions/462947)
if that's not the same as yours, let me know.

Thanks,

sehunley
Oct 29, 2014 at 6:13 AM
Thanks sehunley. I tried these steps, but the i am unable to execute the prediction.

I request jperraud to help to resolve this issue.



Thanks,
Yuvaraj R
Oct 29, 2014 at 8:30 AM
This would be the exact error

Error : .onAttach failed in attachNamespace() for 'forecast', details:
call: readLines(f)
error: 5 arguments passed to .Internal(readLines) which requires 6
Error: package or namespace load failed for 'forecast'
A first chance exception of type 'RDotNet.ParseException' occurred in RDotNet.dll
Feb 9, 2015 at 10:18 AM
Edited Feb 9, 2015 at 12:09 PM
Hi Team,

I have not got any reply on this issue..

Can we have a webex session, so that i can explain it clearly.

Please help me.
Feb 22, 2015 at 5:27 PM
That parse exception indicates something is wrong with your code. Have you tried the raw R code in RStudio to see if it runs correctly?
Feb 24, 2015 at 5:29 AM
Yes, It working fine in R studio, without any error.
But when i try to apply in .net, i get the error.
Mar 8, 2015 at 11:12 PM
Edited Mar 8, 2015 at 11:13 PM
Again, a ParseError is a syntax error in your R code. I've replaced the variables in your code with hardcoded constants as such:
var value = engine.CreateNumericVector(new double[] { 1,2,3,4,5});
engine.SetSymbol("PreprocessedValue", value);
engine.Evaluate("StartDate <- as.Date(1/1/2015) " + 30);
engine.Evaluate("size = length(seq(from=as.Date('1/1/2015'), by='" + "day" + "', to=as.Date(StartDate)))");
engine.Evaluate("startDate <- as.POSIXct('1/1/2015')");
engine.Evaluate("endDate <- StartDate + as.difftime(size, units='" + "days" + "')");
engine.Evaluate("PredictDate = seq(from=startDate, by=1*60, to=endDate)");
engine.Evaluate("freq <- ts(PreprocessedValue, frequency = 20)");
engine.Evaluate("forecastnavie <-snaive(freq, Datapoints)");
engine.Evaluate("PredictValue = (forecastnavie$mean)");
engine.Evaluate("PredictedData = cbind(PredictValue, data.frame(PredictDate))");
engine.Evaluate("PredictedData").AsDataFrame();
After doing that, it was pretty obvious that the first date wasn't in quotes, but after executing I received the following exception message in my debugger:
An unhandled exception of type 'RDotNet.ParseException' occurred in RDotNet.dll

Additional information: Status Error for StartDate <- as.Date(1/1/2015) 30
I think if you fix the quote issue, you should be able to move forward with your project.

Blue Skies,
Ritch