I am quite new to R coding, the TTR/XTS package and random.forest.importance functions.
I am extracting trading data using the xts function, calculating whether the difference between Close and Open is positive/negative/flat, applying a handful of technical indicators using the TTR function , and then combining the indicators to calculate the random.forest.importance function.
When I run the code, I get the
Error in model.frame.default(formula, data, na.action = NULL) : variable lengths differ (found for 'Close').
Data:
Date Time Open High Low Close TVolume
2017-10-12 14:00:00 1.18462 1.18487 1.18334 1.18347 1165
2017-10-12 15:00:00 1.18351 1.18377 1.18295 1.18347 884
2017-10-12 16:00:00 1.18348 1.18348 1.18265 1.18276 1000
2017-10-12 17:00:00 1.18245 1.18329 1.18242 1.18303 1184
2017-10-12 18:00:00 1.18305 1.18373 1.18284 1.18343 469
2017-10-12 19:00:00 1.18343 1.18343 1.18247 1.18303 886
Code as follows:
pkgs <- c('class', 'gmodels', 'quantmod', 'TTR','xts','corrplot','caret','FSelector')
z <- head(tail(hist_r, samples+retro), samples)
z <- as.xts(z[,2:6], order.by=as.POSIXct(z$Timestamp, origin='1970-01-01 00:00', tz='UTC'))
hist <- getHist(z)
h <- as.xts(hist)
price <- z$Close-z$Open
class = ifelse(price > 0,""'UP'"",ifelse(price <0,""'DOWN'"",'""FLAT'""))
forceindex <- (z$Close-z$Open) * z$TVolume
WillR5 <- WPR(z[,c(""'High'"",""'Low'"",""'Close'"")], n = 5)
dataset = data.frame(class,forceindex,WillR5)
dataset = na.omit(dataset)
dput(head(dataset, 10))
set.seed(5)
weights <- random.forest.importance(class~., dataset, importance.type = 1)
print(weights)
When I run dput, i get the following:
tructure(list(Close = structure(c(1L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 3L), .Label = c("DOWN", "FLAT", "UP"), class = "factor"), Close.1 = c(-12.9382400000007, 0.107400000000227, -1.66915000000001, -0.290530000000006, -1.18667999999979, 0.0752800000000753, -0.244080000000094, 0.0653999999999928, -0.395999999999996, 0.372089999999928)
Would sincerely appreciate any help that anyone can give me
Many thanks in advance Kkel