1

I applied a different data set to Joshua Ulrich's answer at previous question (Automatic vlookup and multiply coefficients with R) and it gives an error I haven't been able to solve.

New dataset:Brazil's Bovespa index

(sp<-read.csv("http://www.bolsapt.com/download/historico/%5EBVSP/de-01-01-2000-a-04-02-2013/")

spLag <- lag(sp)

Error in hasTsp(x) : invalid time series parameters specified

Best regards

Community
  • 1
  • 1
Pedro9
  • 186
  • 1
  • 11

1 Answers1

6

@JoshuaUlrich used getSymbols which returns an xts object by default. xts objects are specifically designed to work with time series data. You're trying to apply the same code to a data.frame which is a more general data class. To create an xts object, you must provide a timeBased index.

sp.xts <- xts(sp[, 5:9], order.by=as.Date(sp[, 3], format="%Y%m%d"))
spLag <- lag(sp.xts)

The above converts the YYYYMMDD column to proper Dates and uses that as the index. Note that all columns of an xts object must be of the same class, so I only included numeric columns.

GSee
  • 48,880
  • 13
  • 125
  • 145
  • @Pedro9: if you found this answer helpful, you should up-vote it and/or mark it as the best/accepted answer. This helps others know your question was answered sufficiently. – Joshua Ulrich Feb 05 '13 at 19:44
  • @GSee , do I substitute sp for sp.xts elsewhere? Because it gives me "Error in `colnames<-`(`*tmp*`, value = c("Datelag",(...)length of 'dimnames' [2] not equal to array extent" afterwards in "> colnames(spLag) <- (...) following @JoshuaUlrich code. Thanks for the help! – Pedro9 Feb 05 '13 at 20:01
  • @JoshuaUlrich when I clicked the button the first time, it said I needed 15 reputation points. I tried again now and worked. Thanks again. – Pedro9 Feb 05 '13 at 20:04
  • @Pedro9, yes you need to use `sp.xts` elsewhere, or just overwrite it with `sp <- sp.xts` and then use `sp`. p.s. you should have over 15 rep now. ;) – GSee Feb 05 '13 at 20:07