Hosmer-Lemeshow拟合优度检验

Hosmer-Lemeshow拟合优度检验是基于根据预测的概率或风险将样本分开。具体而言，基于估计的参数值，对于样本中的每个观察，基于每个观察的协变量值计算概率。

在实践中，只要我们的一些模型协变量是连续的，每个观测将具有不同的预测概率，因此预测的概率将在我们形成的每个组中变化。为了计算我们预期的观察数量，Hosmer-Lemeshow测试取组中预测概率的平均值，并将其乘以组中的观察数。测试也执行相同的计算，然后计算Pearson拟合优度统计量。

R

``````n < -  100
x < -  rnorm（n）
xb < -  x
pr < -  exp（xb）/（1 + exp（xb））
y < -  1 *（runif（n）<pr）
mod < -  glm（y~x，family = binomial）``````

``````        Hosmer and Lemeshow goodness of fit (GOF) test

data:  mod\$y, fitted(mod)
X-squared = 7.4866, df = 8, p-value = 0.4851``````

``````cbind(hl\$observed,hl\$expected)
y0 y1    yhat0    yhat1
[0.0868,0.219]  8  2 8.259898 1.740102
(0.219,0.287]   7  3 7.485661 2.514339
(0.287,0.329]   7  3 6.968185 3.031815
(0.329,0.421]   8  2 6.194245 3.805755
(0.421,0.469]   5  5 5.510363 4.489637
(0.469,0.528]   4  6 4.983951 5.016049
(0.528,0.589]   5  5 4.521086 5.478914
(0.589,0.644]   2  8 3.833244 6.166756
(0.644,0.713]   6  4 3.285271 6.714729
(0.713,0.913]   1  9 1.958095 8.041905``````

``````pihat <- mod\$fitted
pihatcat <- cut(pihat, brks=c(0,quantile(pi 1,0.9,0.1)),1),  els=FALSE)``````

``````meanprobs <- array(0, dim=c(10,2))
expevents <- array(0, dim=c(10,2))
obsevents <- array(0, dim=c(10,2))

for (i in 1:10) {
meanprobs[i,1] <- mean(pihat[pihatcat==i])

obsevents[i,2] <- sum(1-y[pihatcat==i])
}``````

``````
[1] 7.486643
``````

``````for（i in 5:15）{
print（hoslem.test（mod \$ y，fits（mod），g = i）\$ p.value）
}``````
``````[1] 0.4683388
[1] 0.9216374
[1] 0.996425
[1] 0.9018581
[1] 0.933084
[1] 0.4851488
[1] 0.9374381
[1] 0.9717069
[1] 0.5115724
[1] 0.4085544
[1] 0.8686347
``````

``````pvalues < -  array（0,1000）

for（i in 1：1000）{
n < -  100
x < -  rnorm（n）
pr < -  exp（xb）/（1 + exp（xb））
mod < -  glm（y~x，family = binomial）
}``````

``````
[1] 0.04
``````

`` ``

``````[1] 0.648
``````