Read about the 'makepredictcall' generic function. There is a method, makepredictcall.poly(), for poly() that attaches the polynomial coefficients used during the fitting procedure to the call to poly() that predict() makes. You ought to supply a similar method for your xpoly(), and xpoly() needs to return an object of a a new class that will cause that method to be used.
E.g., xpoly <- function(x,degree=1,...){ ret <- poly(x,degree=degree,...); class(ret) <- "xpoly" ; ret } makepredictcall.xpoly <- function (var, call) { if (is.call(call)) { if (identical(call[[1]], quote(xpoly))) { if (!is.null(tmp <- attr(var, "coefs"))) { call$coefs <- tmp } } } call } g2 <- glm(lot1 ~ log(u) + xpoly(u,1), data = clotting, family = Gamma) predict(g2,dc) # 1 2 3 4 5 #-0.01398928608 -0.01398928608 -0.01398928608 -0.01398928608 #-0.01398928608 # 6 7 8 9 #-0.01398928608 -0.01398928608 -0.01398928608 -0.01398928608 You can see the effects of makepredictcall() in the 'terms' component of glm's output. The 'variables' attribute of it gives the original function calls and the 'predvars' attribute gives the calls to be used for prediction: > attr(g2$terms, "variables") list(lot1, log(u), xpoly(u, 1)) > attr(g2$terms, "predvars") list(lot1, log(u), xpoly(u, 1, coefs = list(alpha = 40, norm2 = c(1, 9, 8850)))) Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Jul 16, 2015 at 12:35 PM, Kunshan Yin <yinkuns...@gmail.com> wrote: > 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]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.