a_matrix(scan("kyphosis.dat"),83,5,byrow=T) y_a[,2] age_a[,3] start_a[,4] number_a[,5] #Fit a quadratic logistic model of y on age logitquad_glm(y~poly(age,2),family=binomial) summary(logitquad) Call: glm(formula = y ~ poly(age, 2), family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -0.9912322 -0.828576 -0.4745756 -0.2718635 2.233231 Coefficients: Value Std. Error t value (Intercept) -1.5659259 0.3402633 -4.6021010 poly(age, 2)1 0.5940142 3.4628418 0.1715395 poly(age, 2)2 -10.2320391 4.3235541 -2.3665806 (Dispersion Parameter for Binomial family taken to be 1 ) Null Deviance: 86.80381 on 82 degrees of freedom Residual Deviance: 78.21608 on 80 degrees of freedom Number of Fisher Scoring Iterations: 4 Correlation of Coefficients: (Intercept) poly(age, 2)1 poly(age, 2)1 0.0201553 poly(age, 2)2 0.5608006 0.2495414 #plot the fitted value (linear predictor) against age quadfit_logitquad$fitted.values plot(age[sort.list(age)],quadfit[sort.list(age)],type="l",lty=1,xlab="age", ylab="fitted value")) #Fit a logistic nonparametric model of y on age using loess logitnon_gam(y~lo(age,span=0.7,degree=1),family=binomial) nonfit_logitnon$fitted.values cage_cbind(age,age) cage_cage[sort.list(age),] fit_cbind(quadfit,nonfit) fit_fit[sort.list(age),] matplot(cage,fit,type="l", lty=c(1,2),xlab="age", ylab="fitted value") legend(150,0.35,lty=c(1,2),legend=c("quad fit","loess fit")) title("Logistic Quadratic and Loess Fits of Kynosis on Age") logitcubic_glm(y~poly(age,3),family=binomial) summary(logitcubic) Call: glm(formula = y ~ poly(age, 3), family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -0.9974528 -0.8438042 -0.4748754 -0.2226955 2.203141 Coefficients: Value Std. Error t value (Intercept) -1.625395 0.3858689 -4.2122994 poly(age, 3)1 -1.295963 6.0719092 -0.2134358 poly(age, 3)2 -12.610944 7.7803757 -1.6208657 poly(age, 3)3 -2.482434 6.3054700 -0.3936953 (Dispersion Parameter for Binomial family taken to be 1 ) Null Deviance: 86.80381 on 82 degrees of freedom Residual Deviance: 78.0554 on 79 degrees of freedom Number of Fisher Scoring Iterations: 5 Correlation of Coefficients: (Intercept) poly(age, 3)1 poly(age, 3)2 poly(age, 3)1 0.4068619 poly(age, 3)2 0.6625294 0.7728313 poly(age, 3)3 0.4441241 0.8058684 0.8134943