[R] User defined function within a formula
Kunshan Yin
yinkunshan at gmail.com
Thu Jul 16 21:35:45 CEST 2015
Hello, I have a question about the formula and the user defined function:
I can do following:
###Case 1:
> clotting <- data.frame(
+ u = c(5,10,15,20,30,40,60,80,100),
+ lot1 = c(118,58,42,35,27,25,21,19,18),
+ lot2 = c(69,35,26,21,18,16,13,12,12))
> g1=glm(lot1 ~ log(u) + poly(u,1), data = clotting, family = Gamma)
> dc=clotting
> dc$u=1
> predict(g1,dc)
1 2 3 4 5
6 7 8 9
-0.01398929 -0.01398929 -0.01398929 -0.01398929 -0.01398929 -0.01398929
-0.01398929 -0.01398929 -0.01398929
However, if I just simply wrap the poly as a user defined function ( in
reality I would have my own more complex function) then I will get error:
###Case 2:
> xpoly<-function(x,degree=1){poly(x,degree)}
> g2=glm(lot1 ~ log(u) + xpoly(u,1), data = clotting, family = Gamma)
> predict(g2,dc)
Error in poly(x, degree) :
'degree' must be less than number of unique points
It seems that the predict always treat the user defined function in the
formula with I(). My question is how can I get the results for Case2 same
as case1?
Anyone can have any idea about this?
Thank you very much.
Alex
[[alternative HTML version deleted]]
More information about the R-help
mailing list