[R] Problem with tq_mutate_xy() from the tidyquant package

Eric Berger ericjberger at gmail.com
Wed Oct 18 16:54:49 CEST 2017


I was able to reproduce the problem with this self-contained example. Maybe
it could be reproduced with an even smaller one ...


library(tidyquant)  # Loads tidyverse, tidyquant, financial pkgs, xts/zoo
library(xts)

dtV <- as.Date("2017-01-01") + 1:100
locL <- list( foo=xts(rnorm(100), order.by=dtV), bar=xts(rnorm(100),
order.by=dtV) )
fullXts <- do.call(merge,locL)
smallXts <- fullXts["2017-02-01::"]

rolling_corrOnePair <- function( aXts, col1, col2, window ) {  #window =
window size in days
  tbl  <- timetk::tk_tbl( aXts[,c(col1,col2)], rename_index="date" )
  colnames(tbl) <- c("date","x","y")
  naV <- sapply(1:nrow(tbl), function(i) any(is.na(tbl[i,])) )
  tbl <- tbl[!naV,]# remove any rows with NAs
   tq_mutate_xy( data=tbl, x = x, y = y, mutate_fun=runCor,
                n = window, use="all.obs",  # runCor args
                col_rename = "rolling_corr" ) # tq_mutate args
}

foo <- rolling_corrOnePair( aXts=smallXts, col1="foo", col2="bar",
window=30 )

## This produces the error
# Error in runCov(x, y, n, use = use, sample = sample, cumulative ) :
#   n = 30 is outside valid range: [1, 1]

Thanks for any help,

Eric

	[[alternative HTML version deleted]]



More information about the R-help mailing list