通过逻辑回归模型预测CI以获取预测值

时间:2020-04-30 02:21:25

标签: r

因此,我有一个使用逻辑回归计算的特定预测值,现在我需要找到该概率的CI。这是我的代码:

cheese_out <- glm(taste~acetic+person,data=cheese,family = "binomial")
probabilities <- predict(cheese_out,newdata=cheese, type="response")
testdat <- data.frame(acetic = 6, person = "Child")
pred_accp <- predict(cheese_out, newdata=testdat, type="response")

我得到的pred_accp值为0.1206,但是如何根据该值计算置信区间?

1 个答案:

答案 0 :(得分:1)

您可以使用se.fit=TRUE函数的选项predict。这为您提供了标准误差,您可以根据这些标准误差来计算置信区间。 示例:

out <- glm(I(Sepal.Length > 5.8) ~ Sepal.Width + Species, iris, family=binomial())
testdat <- data.frame(Sepal.Width=3, Species="versicolor")
pred_accp <- predict(out, newdata=testdat, type="response", se.fit=TRUE)

alpha <- .05  ## confidence level
cc <- -qt(alpha/2, df=Inf)*pred_accp$se.fit

setNames(
  pred_accp$fit + cc * c(-1, 0, 1), 
  c("lower", "estimate", "upper"))
#     lower  estimate     upper 
# 0.5505699 0.7072896 0.8640093 

注意,这里假设数据是z分布的,即df=Inf。对于t分布,您可能需要在此处指定正确的自由度。