title: Hosmer-Lemeshow statistic in R
body: <p>I have run the Hosmer Lemeshow statistic in R, but I have obtained an p-value of 1. This seems strange to me. I know that a high p-valvalue means that we do not reject the null hypothesis that observed and expected are the same, but is it possible i have an error somewhere?</p>
  
  <p>How do i interpret such p-value?</p>
  
  <p>Below is the code i have used to run the test. I also attach how my model looks like. Response variable is a count variable, while all regressors are continous. I have run a negative binomial model, due to detected overdispersion in my initial poisson model. </p>
  
  <pre><code>&gt; hosmerlem &lt;- function(y, yhat, g=10)
  +     {cutyhat &lt;- cut(yhat, breaks = quantile(yhat, probs=seq(0,1, 1/g)), include.lowest=TRUE)
  +     obs &lt;- xtabs(cbind(1 - y, y) ~ cutyhat)
  +     expect &lt;- xtabs(cbind(1 - yhat, yhat) ~ cutyhat)
  +     chisq &lt;- sum((obs - expect)^2/expect)
  +     P &lt;- 1 - pchisq(chisq, g - 2)
  +     return(list(chisq=chisq,p.value=P))}
  &gt;   hosmerlem(y=TOT.N, yhat=fitted(final.model))
  $chisq
  [1] -2.529054
  
  $p.value
  [1] 1
  
  &gt; final.model &lt;-glm.nb(TOT.N ~ D.PARK + OPEN.L + L.WAT.C + sqrt(L.P.ROAD))
  &gt; summary(final.model)
  
  Call:
  glm.nb(formula = TOT.N ~ D.PARK + OPEN.L + L.WAT.C + sqrt(L.P.ROAD), 
      init.theta = 4.979895131, link = log)
  
  Deviance Residuals: 
       Min        1Q    Median        3Q       Max  
  -3.08218  -0.70494  -0.09268   0.55575   1.67860  
  
  Coefficients:
                   Estimate Std. Error z value Pr(&gt;|z|)    
  (Intercept)     4.032e+00  3.363e-01  11.989  &lt; 2e-16 ***
  D.PARK         -1.154e-04  1.061e-05 -10.878  &lt; 2e-16 ***
  OPEN.L         -1.085e-02  3.122e-03  -3.475  0.00051 ***
  L.WAT.C         1.597e-01  7.852e-02   2.034  0.04195 *  
  sqrt(L.P.ROAD)  4.924e-01  3.101e-01   1.588  0.11231    
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  
  (Dispersion parameter for Negative Binomial(4.9799) family taken to be 1)
  
      Null deviance: 197.574  on 51  degrees of freedom
  Residual deviance:  51.329  on 47  degrees of freedom
  AIC: 383.54
  
  Number of Fisher Scoring iterations: 1
  
  
                Theta:  4.98 
            Std. Err.:  1.22 
  
   2 x log-likelihood:  -371.542 
  </code></pre>