简体   繁体   English

Forecast.HoltWinters没有映射到C ++

[英]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 根据示例代码,我试图使用c++RInside运行预测方法,但我却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. 这看起来像是我在RInside源码中包含的十几个示例之一的直接改编版-因此这是一个很好的起点。

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. 您引用的错误是R错误,而不是C ++错误,因此我将首先尝试在R中自己尝试几行R代码。特别注意要分配的返回值的class() ,以确保执行将其转换为正确的C ++类型。

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. 您很亲密,但由于我怀疑forecast包中的类型会妨碍您。 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$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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