> ####### > # Blood Pressure and Heart Disease > ####### > > y1 <- c(3,17,12,16,12,8,16,8) > y2 <- c(153,235,272,255,127,77,83,35) > > y <- cbind(y1,y2) > x <- c(111.5,121.5,131.5,141.5,151.5,161.5,176.5,191.5) > > g1 <- glm(y~x,fam=binomial) > summary(g) Call: glm(formula = y ~ x, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.0617 -0.5977 -0.2245 0.2140 1.8501 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -6.082031 0.724091 -8.400 <2e-16 *** x 0.024338 0.004842 5.026 5e-07 *** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 30.0226 on 7 degrees of freedom Residual deviance: 5.9092 on 6 degrees of freedom AIC: 42.61 Number of Fisher Scoring iterations: 3 > 1-pchisq(5.9092,6) [1] 0.4334381 ######## # I fit a logistic model with x (blood pressure) as the redictor variable. # The fitted mode is # log(p/(1-p))=-6.082+0.0243 x # The residual deviance is 5.909 based on 6 degrees of freedom which indicates # the fit is good. # The independent model is not good since the null deviance is 30.02 based on 7 degrees of freedom. # The goodness of the fit can be read from the last p-value reported. If the p-value is less than 0.05 # then, the fit is not good. ######### # Next I compute the Pearson chisq goodness of fit and its p-value. ######### > ###### > # Goodness of Fit > ###### > # Pearson chi-square: > ###### > > y.total <- apply(y,1,sum) > y.pred <- cbind(g1$fit*y.total,(1-g1$fit)*y.total) > > pearson.chisq <- sum((y-y.pred)^2/y.pred) > pearson.chisq [1] 6.289936 > 1-pchisq(6.2899,6) [1] 0.3915111 ######### # It also indicates the fit is good. ######### > ##### > # loglikelihood ratio Chi-square > ##### > > loglikelihood.chisq <- 2*sum(y*log(y/y.pred)) > loglikelihood.chisq [1] 5.909158 ###### # It is exactly the residuce deviance. ###### > ####### > # O-rings > ####### > > orings <- read.table("u:\\stat526\\dataset\\ascdata\\orings.txt",h=T) > g2 <- glm(cbind(damage,6-damage)~temp,fam=binomial,data=orings) > summary(g2) Call: glm(formula = cbind(damage, 6 - damage) ~ temp, family = binomial, data = orings) Deviance Residuals: Min 1Q Median 3Q Max -0.9529 -0.7345 -0.4393 -0.2079 1.9565 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 11.66299 3.29575 3.539 0.000402 *** temp -0.21623 0.05317 -4.067 4.76e-05 *** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 38.898 on 22 degrees of freedom Residual deviance: 16.912 on 21 degrees of freedom AIC: 33.675 Number of Fisher Scoring iterations: 5 > 1-pchisq(16.912,21) [1] 0.7164267 ############## # The fit is also good. #############