2

The below code has always worked until recently.

library("quantmod")
library("PerformanceAnalytics")
library("ggplot2")
library("fPortfolio")
library("fAssets")
library("reshape2")
library("corrplot")
library("gridExtra")    
library("dplyr")

Data_SP <- new.env()

### Download the data from yahoo finance
SP_tickers<-c("SPY","AAGPX","AUIAX","BHBFX","CABDX","CGRWX","EHSTX","HDGYX","KDCAX","LEIFX","NBPBX","OLVAX","PGRWX")

start.time<-proc.time()
getSymbols(SP_tickers, from="1998-12-29", env=Data_SP)    
Returns <- eapply(Data_SP, function(s) ROC(Ad(s), type="discrete"))
proc.time()-start.time

The system comes back with the following error:

 Error in lag(x, n, na.pad = na.pad) : unused argument (na.pad = na.pad) 

Any suggestions how to get the above working again?

(All packages have been updated since this error appeared)

Below the Traceback:

4 lag(x, n, na.pad = na.pad) 
3 ROC(Ad(s), type = "discrete") 
2 FUN(list(structure(c(20.7, 20.57, 20.49, 20.52, 20.76, 21.2, 
21.22, 21.42, 21.21, 20.84, 20.73, 20.27, 20.74, 20.83, 20.84, 
20.57, 20.35, 20.41, 20.53, 20.3, 20.6, 20.82, 20.62, 20.48, 
20.62, 20.35, 20.32, 20.32, 20.01, 20.1, 20.39, 20.1, 20.28,  ... 
1 eapply(Data_SP, function(s) ROC(Ad(s), type = "discrete")) 
smci
  • 32,567
  • 20
  • 113
  • 146
New_code
  • 594
  • 1
  • 5
  • 16
  • What packages are you using for all these non-base function? Please include all appropriate `library()` calls to make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Oct 28 '14 at 05:51

1 Answers1

1

Find out which package lag is actually coming from. Which packages do you have loaded and in what order?

Apparently dplyr::lag() disagrees with xts::lag() https://github.com/hadley/dplyr/issues/277

"But if we now attach the dplyr library it does not extend the lag generic function. It replaces it with an explicit function which breaks xts functionality."

smci
  • 32,567
  • 20
  • 113
  • 146
  • Brilliant!!! Restarted RStudio, and reran the code but took out the library("dplyr"). Fixed!! – New_code Oct 28 '14 at 06:02
  • Had to reposition the line of code for library("dplyr") further down the Script. – New_code Oct 28 '14 at 06:09
  • 2
    If you have namespace collisions, it's advisable to call the namespace directly. Use `dplyr::lag` not just `lag`. – Hugh Oct 28 '14 at 07:31
  • 1
    @Hugh: this was a bit different from the usual masking issue between packages. `dplyr::lag` was masking the generic `stats::lag`, which prevented method dispatch. I don't think this would have been a problem if TTR "Imports" xts and zoo instead of having them in "Depends" (which is on my to-do list). – Joshua Ulrich Oct 28 '14 at 12:05
  • (Btw the tag [tag:masking] is not applicable here, it's for masking input fields.) – smci Oct 28 '14 at 15:08