0

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

loki
  • 9,816
  • 7
  • 56
  • 82
KKel
  • 61
  • 6
  • 1
    Welcome to SO. Please have a look at how to write a [reproducible](https://stackoverflow.com/q/5963269/3250126) example. – loki Dec 11 '17 at 09:12
  • Thanks Loki....am struggling to follow as I am not not learnt coding at all. I took a look at the reproducible example page - any tips on what else I can provide to augment the above? – KKel Dec 11 '17 at 09:21
  • you should give an example of your data. You can use `dput(head(your_data, 10))` for example – loki Dec 11 '17 at 09:22
  • Hi Loki, appreciate the direction, does this help? (sorry if I have misunderstood): 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) – KKel Dec 11 '17 at 14:49
  • you should [edit](https://stackoverflow.com/posts/47749429/edit) that to your question. also show the original `dput()` command, so that we can identify which object it is. Also try to run the code in a clean R session, so that you can find out if we can also reproduce everything with the code given. – loki Dec 11 '17 at 14:52
  • also please add the libraries you are using at the top of your code – loki Dec 11 '17 at 14:55
  • tried again Loki - thanks again for being patient with my amateurish attempts – KKel Dec 11 '17 at 15:02
  • Hi Loki - any chance that the above information was helpful? – KKel Dec 12 '17 at 05:00
  • No not yet. We cannot see what the original dput was and furthermore there are typos in the S(!) tructure. Please make sure it is completely reproducible for other users. Only thus we are able to help you. Otherwise you risk your question being closed as "not reproducible". – loki Dec 12 '17 at 06:58

0 Answers0