[英]why I can't get a confidence interval using predict function in R
I am trying to get a confidence interval for my response in a poisson regression model.我试图在泊松回归 model 中获得我的响应的置信区间。 Here is my data:
这是我的数据:
X <- c(1,0,2,0,3,1,0,1,2,0)
Y <- c(16,9,17,12,22,13,8,15,19,11)
What I've done so far: (i) read my data (ii) fit a Y by poisson regression with X as a covariate到目前为止我所做的:(i)读取我的数据(ii)通过泊松回归拟合 Y,X 作为协变量
model <- glm(Y ~ X, family = "poisson", data = mydata)
(iii) use predict() (iii) 使用预测()
predict(model,newdata=data.frame(X=4),se.fit=TRUE,interval="confidence",level=0.95, type = "response")
I was expecting to get "fit, lwr, upr" for my response but I got the following instead:我期待我的回复得到“fit,lwr,upr”,但我得到了以下信息:
$fit
1
30.21439
$se.fit
1
6.984273
$residual.scale
[1] 1
Could anyone offer some suggestions?有人可以提供一些建议吗? I am new to R and struggling with this problem for a long time.
我是 R 的新手,长期以来一直在努力解决这个问题。 Thank you very much.
非常感谢。
First, the function predict() that you are using is the method predict.glm().首先,您使用的 function predict() 是方法 predict.glm()。 If you look at its help file, it does not even have arguments 'interval' or 'level'.
如果您查看它的帮助文件,它甚至没有 arguments 'interval' 或 'level'。 It doesn't flag them as erroneous because predict.glm() has the (in)famous... argument, that absorbs all 'extra' arguments.
它不会将它们标记为错误,因为 predict.glm() 具有(in)着名的...参数,它吸收了所有“额外的”arguments。 You can write confidence=34.2 and interval="woohoo" and it still gives the same answer.
你可以写 confidence=34.2 和 interval="woohoo" ,它仍然给出相同的答案。 It only produces the estimate and the standard error.
它只产生估计值和标准误差。
Second, one COULD then take the fit +/- 2*se to get an approximate 95 percent confidence interval.其次,然后可以采用 +/- 2*se 的拟合来获得大约 95% 的置信区间。 However, without getting into the weeds of confidence intervals, pivotal statistics, non-normality in the response scale, etc., this doesn't give very satisfying intervals because, for instance, they often include impossible negative values.
但是,如果不涉及置信区间、关键统计数据、响应量表中的非正态性等,这并不能给出非常令人满意的区间,因为例如,它们通常包含不可能的负值。
So, I think a better approach is to form an interval in the link scale, then transform it (this is still an approximation, but probably better):所以,我认为更好的方法是在链接比例中形成一个区间,然后对其进行转换(这仍然是一个近似值,但可能更好):
X <- c(1,0,2,0,3,1,0,1,2,0)
Y <- c(16,9,17,12,22,13,8,15,19,11)
model <- glm(Y ~ X, family = "poisson")
tmp <- predict(model, newdata=data.frame(X=4),se.fit=TRUE, type = "link")
exp(tmp$fit - 2*tmp$se.fit)
1
19.02976
exp(tmp$fit + 2*tmp$se.fit)
1
47.97273
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.