3
# Loading packages

require(forecast)
require(quantmod)

# Loading OHLC xts object

getSymbols('SPY', from = '1950-01-01')

# Selecting weekly Close prices

x <- Cl(to.weekly(SPY))

# ARIMA(p,d,q) estimation and forecasting function

a.ari.fun <- function(x) {

  a.ari <- auto.arima(x = x, d = 1, max.p = 50, max.q = 50, max.P = 50,
                      max.Q = 50, ic = 'aic', approximation = TRUE)
  fore <- forecast.Arima(object = a.ari, h = 4, level = c(.9))
  supp <- tail(fore$lower, 1)
  rest <- tail(fore$upper, 1)
  return(c(supp, rest))

}

# Roll apply ARIMA(p,d,q) in rolling window

rollapplyr(data = tail(x, 800), width = 750, FUN = a.ari.fun)

This code returns me an error because of

return(c(supp, rest))

at the end of a.ari.fun() function I wrote; I'm sure about that because if a.ari.fun() returns just

return(rest)

it works fine.

How do I have to arrange the a.ari.fun() in order to obtain an object suitable to rollapplyr()?

GSee
  • 48,880
  • 13
  • 125
  • 145
Lisa Ann
  • 3,345
  • 6
  • 31
  • 42
  • what do you expect the output to look like? Can you use rollapply twice? Maybe [this post](http://stackoverflow.com/questions/11942884/r-how-to-use-rollapply-on-functions-from-other-package-like-maxdrawdown-from-t) will give you some ideas? – GSee Aug 18 '12 at 14:03
  • Unfortunately it takes lot of time to complete all the iterations, I cannot afford to duplicate waiting time; the result I would like is a matrix with _n_ rows and 2 columns: the first column contains 'supp' time series, while the second one contains 'rest' time series. – Lisa Ann Aug 18 '12 at 14:08
  • It looks like exactly the opposite of the post you linked: that friend had a multiple output result and he needed just a single value; I would need the opposite, i.e. a multiple value output :) – Lisa Ann Aug 18 '12 at 14:13

1 Answers1

5

It looks like using by.column=FALSE will give what you request.

tail(rollapplyr(data = as.zoo(x), width = 750, FUN = a.ari.fun, by.column=FALSE))

2012-07-13 126.0730 145.8036
2012-07-20 126.1342 145.8616
2012-07-27 128.9303 148.6576
2012-08-03 129.7640 149.4975
2012-08-10 130.5752 150.2954
2012-08-17 132.3789 152.0963

If you have PerformanceAnalytics loaded rollapply.xts will be dispatched instead of rollapply.zoo. I edited to convert the object to zoo first to make sure the right rollapply is called.

EDIT:

Thanks to some patching from @JoshuaUlrich, this now works with rollapply.xts,so you do not have to convert to zoo. Also, rollapply.xts is now registered in the xts package instead of PerformanceAnalytics, so you will get the same results regardless of whether or not PerformanceAnalytics is loaded. You'll need the under development version of xts -- Rev. 765 or later -- which is on R-Forge.

rollapplyr(x, 750, a.ari.fun, by.column=FALSE)
Community
  • 1
  • 1
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Wow. I'm gonna try that, but... wasn't `by.column` needed just to deal with multivariate inputs? Or, at least, that was my thinking... – Lisa Ann Aug 18 '12 at 14:31
  • I get an error with that: `> rollapplyr(data = x, width = 750, FUN = a.ari.fun, by.column = FALSE)` I get `Errore in xts(xx, tt, if (by == 1) attr(data, "frequency")) : NROW(x) must match length(order.by)` – Lisa Ann Aug 18 '12 at 14:44
  • Do you perhaps have `PerformanceAnalytics` loaded. Simply having that package loaded may cause problems here. – GSee Aug 18 '12 at 14:48
  • Yep. That's the problem. PerformanceAnalytics does not play well with others. – GSee Aug 18 '12 at 14:49
  • Ok, with a new session it works. Maybe you're right, it was `PerformanceAnalytics`, though it was not sufficient to detach it. Well, this is a some sort of issue: does one have to forget about using `PerformanceAnalytics` if he wants to make some back test? – Lisa Ann Aug 18 '12 at 14:58
  • I'm getting the same `NROW(x) must match length(order.by)` error and I do not have `PerformanceAnalytics` loaded. I'm on R 3.0.0 with `xts_0.9-3`. – neverfox Jul 26 '13 at 03:12
  • 1
    @neverfox, update `xts`. The current version [on CRAN](http://cran.r-project.org/web/packages/xts/index.html) is 0.9-5. – GSee Jul 26 '13 at 03:52