我想了解如何为逻辑回归估计生成预测间隔。
建议我遵循Collett的Modeling Binary Data,第二版,第98-99页中的步骤。在实现了此过程并将其与R进行比较之后predict.glm
,我实际上认为这本书展示的是计算置信区间而非预测区间的过程。
与相比predict.glm
,Collett实施了该程序,如下所示。
我想知道:如何从这里开始产生预测间隔而不是置信区间?
#Derived from Collett 'Modelling Binary Data' 2nd Edition p.98-99
#Need reproducible "random" numbers.
seed <- 67
num.students <- 1000
which.student <- 1
#Generate data frame with made-up data from students:
set.seed(seed) #reset seed
v1 <- rbinom(num.students,1,0.7)
v2 <- rnorm(length(v1),0.7,0.3)
v3 <- rpois(length(v1),1)
#Create df representing students
students <- data.frame(
intercept = rep(1,length(v1)),
outcome = v1,
score1 = v2,
score2 = v3
)
print(head(students))
predict.and.append <- function(input){
#Create a vanilla logistic model as a function of score1 and score2
data.model <- glm(outcome ~ score1 + score2, data=input, family=binomial)
#Calculate predictions and SE.fit with the R package's internal method
# These are in logits.
predictions <- as.data.frame(predict(data.model, se.fit=TRUE, type='link'))
predictions$actual <- input$outcome
predictions$lower <- plogis(predictions$fit - 1.96 * predictions$se.fit)
predictions$prediction <- plogis(predictions$fit)
predictions$upper <- plogis(predictions$fit + 1.96 * predictions$se.fit)
return (list(data.model, predictions))
}
output <- predict.and.append(students)
data.model <- output[[1]]
#summary(data.model)
#Export vcov matrix
model.vcov <- vcov(data.model)
# Now our goal is to reproduce 'predictions' and the se.fit manually using the vcov matrix
this.student.predictors <- as.matrix(students[which.student,c(1,3,4)])
#Prediction:
this.student.prediction <- sum(this.student.predictors * coef(data.model))
square.student <- t(this.student.predictors) %*% this.student.predictors
se.student <- sqrt(sum(model.vcov * square.student))
manual.prediction <- data.frame(lower = plogis(this.student.prediction - 1.96*se.student),
prediction = plogis(this.student.prediction),
upper = plogis(this.student.prediction + 1.96*se.student))
print("Data preview:")
print(head(students))
print(paste("Point estimate of the outcome probability for student", which.student,"(2.5%, point prediction, 97.5%) by Collett's procedure:"))
manual.prediction
print(paste("Point estimate of the outcome probability for student", which.student,"(2.5%, point prediction, 97.5%) by R's predict.glm:"))
print(output[[2]][which.student,c('lower','prediction','upper')])
一个基本问题,为什么将sqrt(sum(model.vcov * square.student))假定为标准误差?它不是标准偏差,需要除以sqrt(n)吗?如果是这样,应使用哪个n,用于拟合模型的n或用于预测的新数据框的n?
—
拉斐尔