1

my data frame:

library(usl)
dput(df)
structure(list(load = c(1L, 18L, 36L, 72L, 108L, 144L, 216L), 
    throughput = c(64.9, 995.9, 1652.4, 1853.2, 1828.9, 1775, 
    1702.2)), .Names = c("load", "throughput"), class = "data.frame", row.names = c(NA, 
-7L))

xv=c("load")
yv=c("throughput")

capacity<-c(30)

I create the model using nls:

usl.model <- usl(as.formula(paste(yv, '~', xv)), data = df)

given this model, I need to do some prediction given the growth in load:

growth<-c(20)

pred<-predict(usl.model, data.frame(xv = tail(df[,xv],1)*(1+capacity/100)))

I keep getting this error:

Error in `[.data.frame`(newdata, , object@regr, drop = TRUE) : 
  undefined columns selected

how do I refer to xv in the predict function? Since xv is variable?

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 1
    Until I find the right duplicate: `df[[yv]]`. – joran Apr 06 '16 at 20:23
  • @joran, I don't think it is the duplicate. I'm having problem where I set the new value to the pred function data.frame(xv= how could I reference xv since it is a variable? – user1471980 Apr 06 '16 at 20:33
  • You could convince me of that if you finished making your example reproducible. Where can I find `usl`? What is `capacity`? – joran Apr 06 '16 at 20:35
  • @joran, I've just updated the post. Thanks for the heads up. – user1471980 Apr 06 '16 at 20:39

1 Answers1

2

Before you edited your question, it included an error for which the duplicate I suggested was the solution. You can't refer to column names in a variable using $.

Then you changed the question to reveal that you also had a second problem, which is that your original data frame has a variable named load and the new data frame has to have the column with the same name.

Just use setNames:

new_df <- setNames(data.frame(tail(df[,xv],1)*(1+growth/100)),xv)
pred<-predict(usl.model,newdata = new_df)
joran
  • 169,992
  • 32
  • 429
  • 468