根据示例代码,我试图使用c++RInside运行预测方法,但是我正在获取Read 100 items Exception caught: not a matrix
有人可以看看我的代码吗?

  #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;
        }

    }

最佳答案

这看起来像是我在RInside源代码中包含的十几个示例之一的直接改编版-因此这是一个很好的起点。

您引用的错误是R错误,而不是C++错误,因此我将首先尝试在R中自己尝试几行R代码。请特别注意要分配的返回值的class(),以确保您进行了强制转换正确的C++类型。

编辑:好的,有一些时间来看看它。您很亲密,但是由于我怀疑forecast包中的类型妨碍了您。尝试这个:

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))"));

并为我工作:
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$

关于c++ - Forecast.HoltWinters没有映射到C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9720317/

10-12 15:34