[R-SIG-Finance] quantmod and forecast

Brian G. Peterson brian at braverock.com
Mon Jun 25 12:00:14 CEST 2012


On 06/24/2012 10:20 PM, Eric Thungstom wrote:
> Trying to get quantmod and the forecast package working together but having
> some trouble with just the simple stuff. Any ideas on why this code doesn't
> work ?
>
> require(quantmod)
> require(forecast)
>
>
> getSymbols('^GSPC', from='1950-01-06', to=Sys.Date())
> z<-to.monthly(GSPC)[,6]
> names(z)<-"GSPC"
>
> train<-z['::2009-01']
> test<-z['2009-02::2012-01']
>
> train.model<-rwf(train, drift=TRUE,h=1)
> acc.train<-accuracy(train.model)
> acc.test<-accuracy(train.model,test)
>
> Seems to work all the way through the acc.train statement but then gets
> hung up at acc.test. I get the following error:
>
>
> Error in `[.xts`((c(xx[2:n])/c(xx[1:(n - 1)]) - 1), test - 1) :
>    subscript out of bounds

 From the documentation for ?accuracy :

     test: Indicator of which elements of x and f to test. If
           ‘test=="all"’, all elements are used. Otherwise test is a
           numeric vector containing the indices of the elements to use
           in the test.

your 'test' is an xts object, the entire time series, not a numeric 
vector of indices.

Also, since your dates don't line up, this can never work.

rwf makes a number of forecasts specified by 'h'.  You only make one, 
for Feb 2009.

Try summary(train.model), and try some more documentation reading and 
examples.  From here, it looks like everything is fine so far.

GSPC.model <-rwf(z, drift=TRUE,h=20)
summary(GSPC.model)


-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list