# November 19, 2024 setwd("~/Class/MATH5910/Fall2024/R") ### Example 1 ### See slides (no R) ### Example 2 ex2data<-read.table('ex2data.txt',header=T) ex2.glm<-glm(CHD~Age, family=binomial, data=ex2data) summary(ex2.glm) 1/(1+exp(-(-5.30945+0.11092*50))) 1/(1+exp(-(-5.30945+0.11092*55))) 1/(1+exp(-(-5.30945+0.11092*45))) predict(ex2.glm,type = "response") 1/(1+exp(-(-5.30945+0.11092*20))) 1/(1+exp(-(-5.30945+0.11092*69))) predict(ex2.glm) predict(ex2.glm,newdata=data.frame(Age=50),type = "response") predict(ex2.glm,newdata=data.frame(Age=seq(1,100)),type = "response") plot(predict(ex2.glm,newdata=data.frame(Age=seq(1,100)),type = "response"),type='l') ?predict.glm anova(ex2.glm) anova(ex2.glm, test='Chisq') ### Example 3 xy.data<-read.table("xy.data.txt",header=T) xy.data xy.table<-table(xy.data) xy.table (xy.table[1,1]*xy.table[2,2])/(xy.table[1,2]*xy.table[2,1]) chisq.test(xy.table) xy.glm<-glm(factor(y)~factor(x),family=binomial,data=xy.data) summary(xy.glm) anova(xy.glm, test='Chisq') ### Example 4 ex4data<-read.table('ex4data.txt',header=T) ex4data ex4.glm<-glm(Y~X1*X2,family=binomial,data=ex4data) summary(ex4.glm) anova(ex4.glm, test='Chisq') summary(glm(Y~X1,family=binomial,data=ex4data)) summary(glm(Y~X1+X2,family=binomial,data=ex4data)) ### Example 5 #ex4.glm<-glm(Y~X1*X2,family=binomial,data=ex4data) summary(ex4.glm) anova(ex4.glm, test='Chisq') ex4.glm1<-glm(Y~X1*X2,family=binomial(link="logit"),data=ex4data) summary(ex4.glm1) anova(ex4.glm1, test='Chisq') ex4.glm2<-glm(Y~X1*X2,family=binomial(link="probit"), data=ex4data) summary(ex4.glm2) anova(ex4.glm2, test='Chisq') ex4.glm3<-glm(Y~X1*X2,family=binomial(link="cloglog"), data=ex4data) summary(ex4.glm3) anova(ex4.glm3, test='Chisq') ### Example 6 ?glm counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) data.frame(treatment, outcome, counts) glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) anova(glm.D93) summary(glm.D93) ### Example 7 library(alr4) galapagos ?galapagos summary(galapagos) plot(galapagos) gala.a.poi<-glm(NS~Area, family=poisson, data=galapagos) summary(gala.a.poi) gala.e.poi<-glm(NS~Elevation, family=poisson, data=galapagos) summary(gala.e.poi) gala.ae.poi<-glm(NS~Area+Elevation, family=poisson, data=galapagos) summary(gala.ae.poi) gala.ane.poi<-glm(NS~Area+Anear+Elevation, family=poisson, data=galapagos) summary(gala.ane.poi) gala.ade.poi<-glm(NS~Area+Dist+Elevation, family=poisson, data=galapagos) summary(gala.ade.poi) # Bad practice... gala.all.poi<-glm(NS~., family=poisson, data=galapagos) summary(gala.all.poi) step(gala.all.poi) gala.poi<-glm(NS~., family=poisson, data=galapagos[complete.cases(galapagos),]) summary(gala.poi) gala.poi.step<-step(gala.poi) summary(gala.poi.step) anova(gala.poi.step)