gpt4 book ai didi

c++ - forecast.HoltWinters 未映射到 C++

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:14 26 4
gpt4 key购买 nike

根据示例代码,我尝试使用 c++RInside 运行预测方法,但我得到了 Read 100 items
捕获异常:不是矩阵

谁能看看我的代码。

  #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 包中的 types 妨碍了。试试这个:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com