我试图使用泊松回归预测故障率。然而,我不知何故得到一个错误,我不明白。下面是我的代码:
library("survival")
poisson.fit = glm(status ~ offset(log(time)) + ph.karno + age, family = "poisson", data = lung)
# Predict for ph.karno = 50 and age = 55
predict(poisson.fit, newdata = data.frame(ph.karno = 50, age = 55), type="response")
我得到的错误如下:
Error in log(time) : non-numeric argument to mathematical function
这是什么意思,我该如何修复它?
predict
函数需要指定所有的解释变量,包括偏移变量。由于在函数调用中没有指定time
,因此传递了time
(一个R函数)的默认值。这可能会导致'非数字参数'错误。
你可以试着用你调用中指定的time
运行代码,例如
predict(poisson.fit, newdata = data.frame(time=20, ph.karno = 50, age = 55), type="response")