Why am I getting an error with TTR rollSFM loading a CSV file

72 Views Asked by At

I'm trying to plot a rolling linear regression, but get the following error:

Error in if (any(by < 0L) || any(by > nc)) stop("'by' must match numbers of columns") : missing value where TRUE/FALSE needed

The relevant code is:

library(TTR)

mydata <-read.csv("EURUSD2.csv", sep=",",header=TRUE)
gh<-as.matrix(mydata)
reg <- rollSFM(mydata[, "PRICE"], 1:nrow(mydata), n=5)

and here is a sample of the CSV file:

BAR,PRICE,HIGH,LOW
0,113.3035,113.306,113.282
1,113.306,113.311,113.296
2,113.306,113.312,113.302
3,113.297,113.308,113.297
4,113.2875,113.304,113.285
5,113.2975,113.298,113.29
6,113.278,113.303,113.278
7,113.2915,113.294,113.28
8,113.3,113.3,113.284
9,113.298,113.302,113.296
10,113.3195,113.32,113.3
11,113.3275,113.336,113.32

Why won't it accept this data?

1

There are 1 best solutions below

0
On

I figured it out. You just have to provide your own index to the data. For example:

xts1 <- xts(mydata[, "PRICE"], order.by=Sys.Date()+1:length(mydata[, "BAR"]))
reg <- rollSFM(xts1, .index(xts1), 2)
rma <- reg$alpha + reg$beta*.index(xts1)