[Rcpp-devel] Error while using arima function (TSA package) through RInside (Rcpp)

Dirk Eddelbuettel edd at debian.org
Tue Nov 29 14:26:06 CET 2011


On 29 November 2011 at 11:03, Kowagunga wrote:
| Hello everyone,
| I'm working to get some predictions using ARIMA model in R. This is the code
| I'm using, at it works as expected:
| 
| 
|     library(TSA)
|     load(' path... /R/i686-pc-linux-gnu-library/2.14/TSA/data/airpass.rda')
|     show(airpass)
|     m <- arima(airpass,order=c(0,1,12),seasonal=list(order=c(0,1,1),period=24))
|     m
|     plot(m,n.ahead=72,xlab='Time',type='o',ylab='Passengers')
|     abline(h=0)
| 
| 
| I use example data from the TSA package, and the prediction is plotted well.
| The issue is that what I really want is use it within a bigger C++ project, so
| I need to use RInside package. I've tested every example of RInside, and it
| seems perfectly operative in my machine.
| I have programmed it like this:
| 
|     //Ejemplo que carga los datos airpass.rda del package TSA y realiza una
|     prediccion ARIMA sobre ellos
|     #include <RInside.h>                           
|     // for the embedded R via RInside
|     int main(int argc, char *argv[]) {
|         RInside R(argc, argv);                      //
|     create an embedded R instance
|         std::string evalstr = "library(TSA); load(' ...path... /R/
|     i686-pc-linux-gnu-library/2.14/TSA/data/airpass.rda'); show(airpass);"; //
|     Cargamos la libreria TSA necesaria y el archivo de datos
|         R.parseEvalQ(evalstr);
|         std::string evalstr2 = "m <- arima(airpass,order=c(0,1,12),seasonal=
|     list(order=c(0,1,1),period=24)); m"; //Calculo de la prediccion sobre los
|     datos. Coeficientes manualmente precalculados
|         R.parseEvalQ(evalstr2);
|         std::string evalstr3 = "plot(m,n.ahead=72,xlab='Tiempo',type=
|     'o',ylab='Pasajeros'); abline(h=0)"; //Dibujamos los datos y la prediccion.
|         R.parseEvalQ(evalstr3);
|         std::string evalstr4 = "cat('AIC del modelo:\n'); m$aic"; //Muestra
|     el estimador de bondad AIC
|         R.parseEvalQ(evalstr4);
| 
|         exit(0);
|     }
| 
| (The comments are in spanish, sorry)
| When I run this code, the output of the first evalstr is the same that in R.
| TSA & airpass are loaded correctly. But the next line seems not to work.
| It tooks several seconds calculating arima model, but it the problem is that
| "m" command to show the model produces no output, the same that "m$aic". With
| "no output" I mean nothing (including any error).

RInside is by default silent and only prints if you are explicit. 

So try an explicit 'print(m)' as in:

      std::string evalstr2 = "m <- arima(airpass,order=c(0,1,12),"
                             "seasonal=list(order=c(0,1,1),period=24)); print(m)"; 
      R.parseEvalQ(evalstr2);

which will now print m.

If you want a result returned you must

   a) use parseEval() and NOT parseEvalQ() which is 'quiet'

   b) assign to some variable -- I don't know of the top of my head what
      arima() returns, but if it were just a vector you'd do

      std::string evalstr2 = "m <- arima(airpass,order=c(0,1,12),"
                             "seasonal=list(order=c(0,1,1),period=24)); m"; 
      Rcpp::NumericVector myM = R.parseEval(evalstr2);

This is all relatively basic and I encourage you to study the over ten examples
that come with RInside a little more closely.

Dirk 


| I can't get the graphic too, but I know that maybe I would have to plot to a
| file to get it work. The problem is that I also get no error. This is the
| output of my code (there are some warning lines at the beginning, but the rest
| of examples always worked with the same lines):
| 
|     During startup - Warning messages:
|     1: Setting LC_CTYPE failed, using "C"
|     2: Setting LC_COLLATE failed, using "C"
|     3: Setting LC_TIME failed, using "C"
|     4: Setting LC_MESSAGES failed, using "C"
|     5: Setting LC_PAPER failed, using "C"
|     6: Setting LC_PAPER failed, using "C"
|     7: Setting LC_MEASUREMENT failed, using "C"
|     Loading required package: leaps
|     Loading required package: locfit
|     Loading required package: akima
|     Loading required package: lattice
|     locfit 1.5-6      2010-01-20
|     Loading required package: mgcv
|     This is mgcv 1.7-9. For overview type 'help("mgcv-package")'.
|     Loading required package: tseries
|     Loading required package: quadprog
|     Loading required package: zoo
| 
|     Attaching package: 'zoo'
| 
|     The following object(s) are masked from 'package:base':
| 
|         as.Date, as.Date.numeric
| 
| 
|     Attaching package: 'TSA'
| 
|     The following object(s) are masked from 'package:stats':
| 
|         acf, arima
| 
|     The following object(s) are masked from 'package:utils':
| 
|         tar
| 
|          Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
|     1960 112 118 132 129 121 135 148 148 136 119 104 118
|     1961 115 126 141 135 125 149 170 170 158 133 114 140
|     1962 145 150 178 163 172 178 199 199 184 162 146 166
|     1963 171 180 193 181 183 218 230 242 209 191 172 194
|     1964 196 196 236 235 229 243 264 272 237 211 180 201
|     1965 204 188 235 227 234 264 302 293 259 229 203 229
|     1966 242 233 267 269 270 315 364 347 312 274 237 278
|     1967 284 277 317 313 318 374 413 405 355 306 271 306
|     1968 315 301 356 348 355 422 465 467 404 347 305 336
|     1969 340 318 362 348 363 435 491 505 404 359 310 337
|     1970 360 342 406 396 420 472 548 559 463 407 362 405
|     1971 417 391 419 461 472 535 622 606 508 461 390 432
|     AIC del modelo:
| 
|  
| I have tried everything that came to my mind, and I have no more ideas! I think
| I have explained my best, hope somebody can help me with this.
| Sorry if I made some language mistake ;) Thank you very much!
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list