# March 26, 2009 Generalized Additive Models library("mgcv") # to get gam program library("MASS") # to get dataset birthwt low - birth wt less than 2.5 kg age - mother's age lwt - mother's weight (lbs) race - white/black/other smoke - yes/no ptl - number of previous premature labors ht - history of hypertension ui - uterine irritabiliy ftv - number of MD visits in first trimester bwt - birth wt (grms) b = gam( low ~ s(age), data=birthwt) attach(birthwt) race = factor( race, labels=c("white","black","other")) table(ptl) # 159 24 5 1 compress to =0 >0 ptd = factor( ptl>0 ) table(ftv) # 100 47 30 7 4 0 1 compress to 0 1 >=2 ftv = factor(ftv); levels(ftv)[-(1:2)] = "2+" bwt = data.frame( low=factor(low), age, lwt, race, smoke = (smoke>0), ptd, ht = (ht>0), ui = (ui>0), ftv ) detach(); rm(race,ptd,ftv) ####### logistic regression (full) ######## birthwt.glm = glm( low ~ ., family=binomial, data=bwt ) summary(birthwt.glm) plot(birthwt.glm) table( bwt$low, predict(birthwt.glm)>0 ) ## FALSE TRUE ## 0 116 14 ## 1 37 22 ######### use GAM to see if linear ############# attach(bwt) b = gam( low ~ s(age,k=3,fx=TRUE,bs="cr") + s(lwt,k=5,fx=TRUE,bs="cr") + smoke + ptd + ht + ui + ftv, binomial, bwt ) summary(b) plot(b) table( bwt$low, predict(b)>0 ) ## FALSE TRUE ## 0 112 18 ## 1 38 21