我对公式和用户定义函数有疑问:
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
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是简单地将poly包装为用户定义的函数(实际上我将拥有自己更复杂的函数),那么我将得到错误:
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' …Run Code Online (Sandbox Code Playgroud)