1

dlply() gives me an error: "Object '...' not found" when I try the smooth.spline() function with it. The example below creates some data and shows how "lm" will work but "smooth.spline" won't. Note that I am doing some arithmetic in the function arguments but that's not the reason for the error.

#some data:
df <- data.frame(count=rep(1:5,2),VSS=runif(10,0.45,0.55), 
      TSS=runif(10,0.9,1.3),sl=c(rep("a",5),rep("b",5)))
#works:
dlply(df,.(sl),lm,formula=VSS/TSS~count)
#doesn't work:
dlply(df,.(sl),smooth.spline,x=count,y=VSS/TSS,all.knots=TRUE)
#output:
Error in xy.coords(x,y) : Object 'VSS' not found

Any ideas???

lambu0815
  • 311
  • 1
  • 2
  • 9

1 Answers1

2

Ugh, too much tangled evaluation and environments is likely the problem. I generally don't fight these things, I just work around them:

foo <- function(x,xvar,yvar,...){
    smooth.spline(x = x[,xvar],y = x[,yvar],...)
}
df$rat <- with(df,VSS/TSS)
dlply(df,.(sl),foo,xvar = "count",yvar = "rat",all.knots = TRUE)

But there may be way to trick dlply into doing this, I don't know.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Of course. Well, a one-liner would have been nice for readability and the call is no more complicated than that for lm(). Must be smooth.spline() that is too complicated because of all the cross validation and stuff. Thank you anyway. – lambu0815 Jun 18 '12 at 04:51