rollapply with a function taking a matrix returns "incorrect number of dimensions"

finance, r, rollapply

Solution

`rollapply` works by column unless the `by.column=FALSE` argument is used so try this:

rollapply(dat, 20, SharpeRatio, by.column = FALSE)

Problem

I designed my own function called `SharpeRatio(data)` Where data is an nx2 matrix. The function works fine for a given matrix dat, however when I try to use `rollapply(dat, 20, SharpeRatio)` I get the following error: Error in dat[, 1] : incorrect number of dimensions The following is the function definition: ``` SharpeRatio <- function(dat){ Returns = dat[,1] RiskFree = dat[,2] ER = (Returns - RiskFree)/100 Volatility = sd(Returns/100) return((exp(mean(log(1+ER))) - 1)/Volatility) } ```

Original source