I can fit a line to a set of given points, e.g.,

> sm.fit <- smooth.spline(1:4,1:4)
> lm.fit <- lm(y~x, data=list(x=1:4,y=1:4))

Now I have two objects representing the straight line y(x)=x, of class
"smooth.spline" and "lm", respectively.
And as could be expected in object orientation, both have a method
"predict", which I could use for
interpolating some new points on the line.  So far so good.  BUT the two
methods require different
arguments, and return different structures:

> predict(sm.fit, 1.5:4)
$x
[1] 1.5 2.5 3.5

$y
[1] 1.5 2.5 3.5

> predict(lm.fit, list(x=1.5:4))
  1   2   3
1.5 2.5 3.5

I probably don't understand the motivation behind this design, but it seems
ugly.  If, hoping for nice
polymorphism, I call predict(lm.fit, 1.5:4), I get an error: "numeric
'envir' arg not of length one".

I expected something like the classic OO example, class Shape:  you can call
area() or paint() on
any object of the class.

Questions:
* Why does "predict" act so inconsistently?
* Is there a nicer interface that hides it?
* Are there plans to change the current design?

Assuming "no" to the latter two -- what are my options?  Create
as.smooth.spline(...) that would accept an "lm" object?
My goal is to write OO style code, that doesn't need to know which kind it's
working with.
Thanks,
/Christian

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to