r - Using specifyModel from quantmod package within a loop -
i trying replicate stock prediction case study data mining r book, , trying place of code within loop can deal more 1 stock. however, cannot figure out how specifymodel function work within loop. want calculate number of technical indicators each stock, , use random forest select useful ones. below example code.
library(quantmod) # list of stocks want investigate stock.symbols = c("mmm", "ibm") # download daily stock prices global environment getsymbols(stock.symbols, src = "yahoo", quotes = c("open", "high", "low", "close", "volume", "adjusted"), = "2011-01-01", = "2012-01-01") # "custom" indicator functions myaroon = function(x) aroon(cl(x))[,1] myadx = function(x) adx(hlc=x)[,4] # try create quantmod model each stock for(i in stock.symbols) { my.model = specifymodel(cl(mmm) ~ myaroon(mmm) + myadx(mmm)) # line works - looks mmm in global environment # my.model = specifymodel(cl(i) ~ myaroon(i) + myadx(i)) # }
i understand if refer stock symbol, example "mmm", specifymodel looks in current workspace. know how make more general. @ minute getting following error:
no applicable method 'as.xts' applied object of class "character"
i have tried going through documentation specifymodel, no avail (it doesn't seem used function in world). have tried using get() , as.name(). realise may doing wrong here, apologies in advance! @ appreciated.
using get
works me.
my.model <- list() for(i in stock.symbols) { stock <- get(i) my.model = c(my.model, specifymodel(cl(stock) ~ myaroon(stock) + myadx(stock)) ) } lapply(my.model, function(x) head(x@model.data))
Comments
Post a Comment