【发布时间】:2016-12-12 08:54:42
【问题描述】:
下面是一组虚构的概率数据,我用threshold of 0.5 将其转换为二项式。我在离散数据上运行了glm() 模型,以测试从 glm() 返回的区间是“平均预测区间”(“置信区间”)还是“点预测区间”(“预测区间”)。从下图中可以看出,返回的区间是后者——“点预测区间”;请注意,在 95% 的置信度下,此样本中有 2/20 点落在线之外。
如果确实如此,我如何使用 glm() 为由 0 和 1 绑定的二项式数据集在 R 中生成“平均预测区间”(即“置信区间”)?请在给定概率、“置信区间”和“预测区间”的情况下,显示与我相似的代码和绘图。
# Fictitious data
xVal <- c(15,15,17,18,32,33,41,42,47,50,
53,55,62,63,64,65,66,68,70,79,
94,94,94,95,98)
randRatio <- c(.01,.03,.05,.04,.01,.2,.1,.08,.88,.2,
.2,.99,.49,.88,.2,.88,.66,.87,.66,.90,
.98,.88,.95,.95,.95)
# Converted to binomial
randBinom <- ifelse(randRatio < .5, 0, 1)
# Data frame for model
binomData <- data.frame(
randBinom = randBinom,
xVal = xVal
)
# Model
mode1 <- glm(randBinom~ xVal, data = binomData, family = binomial(link = "logit"))
# Predict all points in xVal range
frame <- data.frame(xVal=(0:100))
predAll <- predict(mode1, newdata = frame,type = "link", se.fit=TRUE)
# Params for intervals and plot
confidence <- .95
score <- qnorm((confidence / 2) + .5)
frame <- data.frame(xVal=(0:100))
#Plot
with(binomData, plot(xVal, randBinom, type="n", ylim=c(0, 1),
ylab = "Probability", xlab="xVal"))
lines(frame$xVal, plogis(predAll$fit), col = "red", lty = 1)
lines(frame$xVal, plogis(predAll$fit + score * predAll$se.fit), col = "red", lty = 3)
lines(frame$xVal, plogis(predAll$fit - score * predAll$se.fit), col = "red", lty = 3)
points(xVal, randRatio, col = "red") # Original probabilities
points(xVal, randBinom, col = "black", lwd = 3) # Binomial Points used in glm
这是情节,大概是“点预测区间”(即“预测区间”)用红色虚线表示,而平均拟合用红色实线表示。黑点代表randRatio中原始概率的离散二项式数据:
【问题讨论】:
-
我认为你的前提是不正确的。我认为您没有看到您所谓的“点预测间隔”以及大多数人简称为“预测间隔”的内容。您所说的“平均预测区间”(可能)是大多数人所说的“置信区间”,它们适用于估计参数的合理位置。
-
@42- 我编辑了一些措辞以更好地与您的评论保持一致。
-
@ZheyuanLi 请查看修改后的问题。如果有使用 glm() 的方法,我很想看看您的解决方案,甚至更感兴趣。在具有“信心”或“预测”的 lm() 上使用 predict() 似乎不是 glm() 的选项。见:stackoverflow.com/questions/12544090/…
-
使用 type =
link为您提供置信区间(在 logit 尺度上)。您在概率尺度上呈现它们,但它们仍然不是预测区间。 -
想一想。在二项式情况下,“Y”值的“预测”需要为 1 或 0。
predict.glm值都不是这些数字。
标签: r statistics intervals prediction glm