简体   繁体   English

为什么我无法在 R 中使用预测 function 获得置信区间

[英]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.

相关问题 如何使用R获得函数的置信区间 - How to get the confidence interval for a function using R 如何计算 predict() R function 的预测差异的置信区间? - How to calculate the confidence interval for the difference in predictions of the predict() R function? 如何在不使用 confint 的情况下计算 R 中均值的置信区间 - How can I calculate confidence interval for a mean in R not using confint 为什么不能使用R的predict()函数在给定一个功率值的情况下预测MPG的单个值? - Why can't I predict one single value of MPG given one value of horsepower using R's predict() function? 使用具有置信区间的lm列表预测 - Using predict on lm list with confidence interval R:使用预测功能将标准误差和置信区间添加到预测中 - R: Using the predict function to add standard error and confidence intervals to predictions 我可以使用forecast.Arima(包预测)获得置信区间而不是预测区间吗? - Can I get confidence interval instead of prediction interval using forecast.Arima (package forecast)? R Function 获得均值差的置信区间 - R Function to get Confidence Interval of Difference Between Means 如何使用方程式中的95%置信区间在r中使用95%置信区间? - How do I use the 95% confidence interval in r using the equation for a 95% confidence interval? 如何使用 R 中的 tidymodels 调整后的 model 预测测试集的置信区间? - How to predict the test set's confidence interval using a tuned model from tidymodels in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM