简体   繁体   中英

forecast.HoltWinters is not getting mapped into C++

Based on the example code I was trying to run forecast method using c++ and RInside , but I am getting Read 100 items Exception caught: not a matrix

Can somebody please take a look at my code.

  #include <RInside.h>
    int main ( int argc, char **argv) {
        try {
            // create an embedded R instance 
            RInside R ( argc, argv);
            std::string txt =
                "rain <- scan(\"http://robjhyndman.com/tsdldata/hurst/precip1.dat\",skip=1);"
                "rainseries <- ts(rain,start=c(1813));"
                "rainseriesforecasts <- HoltWinters(rainseries, beta=FALSE, gamma=FALSE);"
                "suppressMessages(require(forecast));";

            R.parseEvalQ(txt); // eval command, no return
            Rcpp::NumericMatrix M((SEXP)R.parseEval("rainseriesforecasts2 <- forecast.HoltWinters(rainseriesforecasts, h=8)"));
            Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(rainseriesforecasts2)"));
            Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(rainseriesforecasts2)"));

            std::cout << "\n\nAnd now from C++\n\n\t\t\t";
            for (int i=0; i<cnames.size(); i++) {
                std::cout << std::setw(11) << cnames[i] << "\t";
            }
            std::cout << std::endl;
            for (int i=0; i<rnames.size(); i++) {
                std::cout << std::setw(16) << rnames[i] << "\t";
                for (int j=0; j<cnames.size(); j++) {
                    std::cout << std::setw(11) << M(i,j) << "\t";
                }
                std::cout << std::endl;
            }
            std::cout << std::endl;

        } catch(std::exception& ex) {
            std::cerr << "Exception caught: " << ex.what() << std::endl;
        } catch(...) {
            std::cerr << "Unknown exception caught" << std::endl;
        }

    }

This looks like a straight-up adaptation of one of the over a dozen examples I have included in the RInside sources -- so that is a good starting point.

The error you quote is an R error, not a C++ error so I would start by trying the few lines of R code by themselves in R. Pay particular attention to the class() of the returns you want to assign to make sure you do cast it to the right C++ types.

Edit: Ok, had some time to look at it. You were close, but as I suspected the types from the forecast package get in the way. Try this:

R.parseEvalQ(txt); // eval command, no return
Rcpp::NumericMatrix M((SEXP)R.parseEval("rainseriesforecasts2 <- as.matrix(as.data.frame(forecast.HoltWinters(rainseriesforecasts, h=8)))"));
Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(as.data.frame(rainseriesforecasts2))"));
Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(as.data.frame(rainseriesforecasts2))"));

and with that it works for me:

edd@dexter:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample12
Read 100 items


And now from C++

            Point Forecast        Lo 80       Hi 80       Lo 95       Hi 95 
            1913        24.6782     19.1749     30.1815     16.2617     33.0947 
            1914        24.6782     19.1733     30.1831     16.2592     33.0972 
            1915        24.6782     19.1717     30.1847     16.2568     33.0996 
            1916        24.6782     19.1701     30.1863     16.2543      33.102 
            1917        24.6782     19.1685     30.1879     16.2519     33.1045 
            1918        24.6782     19.1669     30.1895     16.2495     33.1069 
            1919        24.6782     19.1653     30.1911      16.247     33.1094 
            1920        24.6782     19.1637     30.1926     16.2446     33.1118 

edd@dexter:~/svn/rinside/pkg/inst/examples/standard$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM