Forum: help


RE: Calling specifyModel inside function? [ Reply ] By: John Stanford on 2009-11-29 23:16 | [forum:2242] |
I've solved this problem. As it turns out, it has to do with the way formulas are constructed. I was experiencing the same problem with buildData and other functions that required a formula as an arg. Here is an example of a working formula construction. call this function as: f <- buildGapFormula(quote(IBM)) buildGapFormula <- function(x) { left <- paste("Op(",x,")") r1 <- paste("Lag(Hi(",x,"))") r2 <- paste("Lag(Lo(",x,"))") r3 <- paste("Delt(Lag(Hi(",x,")),Op(",x,"))") r4 <- paste("I(-1 * Delt(Lag(Lo(",x,")),Op(",x,")))") r5 <- paste("Vo(",x,")") fStr <- paste(left,"~", r1, "+", r2, "+", r3, "+", r4, "+", r5) fo <- formula(fStr) return(fo) } Thanks, John |
Calling specifyModel inside function? [ Reply ] By: John Stanford on 2009-11-28 18:59 | [forum:2240] |
Hi, I'm trying to get up to speed on R and quantmod. I think I'm getting the hang of it. That said, I'm stuck on putting specifyModel inside a function. Here's an example function. specifyGapModel <- function(x) { cat(labels(x)[[2]]) class(fo <- Op(x) ~ Lag(Hi(x)) + Lag(Lo(x)) + Delt(Lag(Hi(x)),Op(x)) + I(-1 * Delt(Lag(Lo(x)),Op(x))) + Vo(x)) m <- specifyModel(fo,na.rm = TRUE) return(m) } Here's what happens when I call it (the cat is there to confirm that the argument has the right labels): m <- specifyGapModel(IBM) IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.AdjustedError in Op(x) : subscript out of bounds: no column name containing "Open" > Can someone tell me what I'm doing wrong? Thanks, John |