로지스틱 회귀 추정치에 대한 예측 간격 을 생성하는 방법을 이해하고 싶습니다 .
Collett 's Modeling Binary Data , 2nd Ed p.98-99 의 절차를 따르는 것이 좋습니다 . 이 절차를 구현하고이를 R과 비교 한 후에 predict.glm
실제로이 책은 예측 구간이 아닌 신뢰 구간 을 계산하는 절차를 보여주고 있다고 생각 합니다.
와 비교 한 Collett의 절차 구현 predict.glm
은 다음과 같습니다.
알고 싶습니다. 여기에서 신뢰 구간 대신 예측 구간을 생성하려면 어떻게해야합니까?
#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은 무엇입니까?
—
Rafael