postscript(file="RMB_c6.ps")
WCGS <- read.table("wcgs.txt",header=TRUE,sep="\t")
WCGS$chd69 <- factor(WCGS$chd69,labels=c("Noncases","Cases"))
stripchart(age~as.numeric(chd69),data=WCGS,method="jitter",pch="+",
group.names=levels(WCGS$chd69),las=1,cex.axis=.8,cex=.8)
title("WCGS Study: Coronary disease and Age")
WCGS.glm <- glm(chd69~age,data=WCGS,family=binomial())
summary(WCGS.glm)
logLik(WCGS.glm) exp(coef(WCGS.glm)[2]*10) library(MASS)
confint(WCGS.glm) confint.default(WCGS.glm)
WCGS.glm.fit <- fitted(WCGS.glm)
exp(-5.940+0.074*55)/(1+exp(-5.940+0.074*55))
pred.df <- data.frame(age=c(55))
predict(WCGS.glm,pred.df,type="response",se=TRUE)
x0 <- c(1,55)
eta0 <- sum(x0*coef(WCGS.glm))
library(boot)
inv.logit(eta0)
(vcm <- summary(WCGS.glm)$cov.unscaled)
se <- sqrt(t(x0) %*% vcm %*% x0) inv.logit(eta0+c(-1,1)*qnorm(0.975)*se)
anova(WCGS.glm,test="Chisq")
library(Design)
lrm(chd69~age,data=WCGS)
resid(lrm(chd69~age,data=WCGS,x=T,y=T), "partial", pl="loess")
tab.chd69.age <- with(WCGS, table(chd69,age))
obs.logit <- logit(tab.chd69.age[2,]/apply(tab.chd69.age,2,sum))
plot(obs.logit~as.numeric(names(obs.logit)),pch=19,cex=.8,xlab="Age (years)",
ylab="Logit CHD Risk",ylim=c(-4,-1))
abline(lm(obs.logit~as.numeric(names(obs.logit))),lty=2)
lines(as.numeric(names(obs.logit)),
predict(loess(obs.logit~as.numeric(names(obs.logit)))),lty=3)
legend("bottom",c("Linear fit","LOWESS smooth"),lty=2:3,horiz=TRUE)
summary(glm(chd69~age+I(scale(age,scale=F)^2),data=WCGS,family=binomial))
WCGS$arcus <- factor(WCGS$arcus,labels=c("Unexposed","Exposed"))
mosaicplot(t(table(WCGS$chd69,WCGS$arcus)),col=c(4,2),main="WCGS Study")
WCGS.glm2 <- glm(chd69~arcus,data=WCGS,family=binomial())
summary(WCGS.glm2)
library(epitools)
oddsratio(table(WCGS$chd69,WCGS$arcus),method="wald")
table(WCGS$chd69,WCGS$agec)
WCGS.glm3 <- glm(chd69~as.factor(agec),data=WCGS,family=binomial())
summary(WCGS.glm3)
WCGS.glm3.OR <- as.numeric(exp(coef(WCGS.glm3))[2:5])
res <- matrix(NA,nr=4,nc=3)
for (i in 2:5) {
test <- oddsratio(table(WCGS$chd69,WCGS$agec)[,c(1,i)],method="wald")
res[i-1,] <- test$measure[2,]
}
colnames(res) <- c("OR","LB-CI","UB-CI")
rownames(res) <- paste("agec",1:4,sep="_")
print(res,digits=4)
agec2 <- relevel(as.factor(WCGS$agec),ref="3")
table(agec2)
oddsratio(table(WCGS$chd69,agec2)[,c(1,5)],method="wald")
WCGS.glm4 <- glm(chd69~age+chol+sbp+bmi+smoke,data=WCGS,family=binomial())
summary(WCGS.glm4)
WCGS.glm4.coeff <- coef(WCGS.glm4)
WCGS.glm4.CI <- confint(WCGS.glm4)
opar <- par(mar=c(5,6,4,2),las=1)
plot(exp(WCGS.glm4.coeff)[2:6],1:5,xlim=c(0.5,2.5),axes=FALSE,pch=19,
cex=1.2,ylab="",xlab="OR",main="WCGS Study")
axis(1)
axis(2,at=1:5,labels=names(WCGS.glm4.coeff)[2:6],tick=FALSE)
segments(exp(WCGS.glm4.CI[2:6,1]),1:5,exp(WCGS.glm4.CI[2:6,2]),1:5,lwd=2)
abline(v=1,lty=2,col="lightgray")
par(opar)
plot(WCGS$age_10,scale(WCGS$age,scale=F)/10)
WCGS.glm5 <- glm(chd69~age_10+chol_50+sbp_50+bmi_10+smoke,data=WCGS,
family=binomial())
summary(WCGS.glm5)
table(WCGS$behpat)
WCGS.glm6 <- update(WCGS.glm5,.~.+as.factor(behpat))
summary(WCGS.glm6)
exp(confint.default(WCGS.glm6))
anova(WCGS.glm6,WCGS.glm5,test="Chisq")
2*(logLik(WCGS.glm6)-logLik(WCGS.glm5))
lrtest(WCGS.glm6,WCGS.glm5)
detach(package:Design)
library(lmtest)
lrtest(WCGS.glm6,WCGS.glm5)
WCGS.glm7 <- glm(chd69~age_10+chol_50+sbp_50+bmi_10+smoke+dibpat,data=WCGS,
family=binomial())
summary(WCGS.glm7)
exp(confint.default(WCGS.glm7))
anova(WCGS.glm7,WCGS.glm6,test="Chisq")
WCGS.glm8 <- glm(dibpat~age_10+chol_50+sbp_50+bmi_10+smoke,data=WCGS,
family=binomial())
summary(WCGS.glm8)
exp(coef(WCGS.glm8)[-1])
oddsratio(table(WCGS$chd69,WCGS$dibpat))
exp(coef(WCGS.glm7)[["dibpat"]])
with(WCGS, table(bage_50,arcus))
xtabs(~as.numeric(chd69)+as.numeric(bage_50)+as.numeric(arcus),data=WCGS)
summary(xtabs(~as.numeric(chd69)+as.numeric(bage_50)+as.numeric(arcus),data=WCGS))
WCGS.glm9 <- glm(chd69~bage_50*arcus,data=WCGS,family=binomial())
summary(WCGS.glm9)
exp(coef(WCGS.glm9))
library(Design)
WCGS$bage_50 <- as.factor(WCGS$bage_50)
WCGS.glm9bis <- lrm(chd69~bage_50*arcus,data=WCGS)
cc <- contrast(WCGS.glm9bis,list(bage_50='1',arcus='Exposed'),
list(bage_50='0',arcus='Exposed'))
exp(unlist(cc[2:5]))
WCGS.glm10 <- update(WCGS.glm9,.~.-bage_50:arcus)
anova(WCGS.glm10,WCGS.glm9,test="Chi")
WCGS.glm11 <- glm(chd69~age*arcus,data=WCGS,family=binomial())
summary(WCGS.glm11)
pred.df2 <- expand.grid(age=seq(min(WCGS$age),max(WCGS$age)),
arcus=factor(levels(WCGS$arcus)))
WCGS.glm11.pred <- predict(WCGS.glm11,pred.df2,se=TRUE)
OR.11 <- WCGS.glm11.pred$fit[pred.df2$age==55 & pred.df2$arcus=="Exposed"] -
WCGS.glm11.pred$fit[pred.df2$age==55 & pred.df2$arcus=="Unexposed"]
exp(OR.11)
WCGS.glm11bis <- lrm(chd69~age*arcus,data=WCGS)
cc2 <- contrast(WCGS.glm11bis,list(age=55,arcus='Exposed'),
list(age=55,arcus='Unexposed'))
exp(unlist(cc2[2:5]))
interaction.plot(pred.df2$age,pred.df2$arcus,WCGS.glm11.pred$fit,legend=FALSE,
xlab="Age (years)",ylab="Log CHD Risk")
legend("bottom",c("Unexposed","Exposed"),lty=1:2,horiz=TRUE)
WCGS.glm12 <- glm((as.numeric(chd69)-1)~bage_50*arcus,data=WCGS,family=poisson)
summary(WCGS.glm12)
WCGS.glm13 <- glm(chd69~age_10+chol_50+sbp_50+bmi_10+smoke+dibpat
+bmi_10:chol_50+bmi_10:sbp_50,
data=WCGS,family=binomial())
summary(WCGS.glm13)
set.seed(143)
five.indiv <- sample(1:nrow(WCGS),5,replace=F)
WCGS.glm13.pred <- predict(WCGS.glm13,type="response")[five.indiv]
WCGS.five.indiv <- WCGS[five.indiv,c("chd69","age","chol","sbp","bmi","smoke","dibpat")]
cbind(WCGS.five.indiv,WCGS.glm13.pred)
library(Epi)
WCGS.glm13.predAll <- predict(WCGS.glm13,type="response")
tmp <- cbind(predicted=WCGS.glm13.predAll,observed=WCGS$chd69[as.numeric(names(WCGS.glm13.predAll))])
ROC(tmp[,"predicted"],tmp[,"observed"]-1,plot="ROC")
plot(residuals(WCGS.glm13,type="pearson"),ylab="Standardized Pearson Residuals",
xlab="Observation Number",pch=19,cex=.8)
ifl.glm13 <- influence(WCGS.glm13,do.coef=FALSE)
dfbetas.glm13 <- dfbetas(WCGS.glm13)
plot(residuals(WCGS.glm13,type="response")~WCGS.glm13.predAll,pch=19,cex=.8,
xlab="Fitted values",ylab="Response Residuals")
hi <- influence(WCGS.glm13)$hat
plot(hi~WCGS.glm13.predAll,xlab="Fitted values",
ylab=expression(paste("Leverage (",h[i],")")),pch=19,cex=.8)
hh <- which(hi>0.06)
text(WCGS.glm13.predAll[hh],hi[hh],hh,cex=.8,pos=4)
data(esoph)
esoph$ditob <- ifelse(as.numeric(esoph$tobgp)==1,"O-9g/day","10+ g/day")
esoph$ditob <- factor(esoph$ditob,levels=c("O-9g/day","10+ g/day"),ordered=TRUE)
with(esoph, tapply(ncases,ditob,sum))
esoph.glm <- glm(cbind(ncases,ncontrols)~alcgp+ditob+agegp,
data=esoph,family=binomial)
summary(esoph.glm)
esoph$age.num <- strsplit(as.character(esoph$agegp),"-")
for (i in 1:77)
esoph$age.num[[i]] <- (as.numeric(esoph$age.num[[i]][2])
+as.numeric(esoph$age.num[[i]][1]))/2
esoph$age.num <- unlist(esoph$age.num)
esoph$age.num[78:88] <- "75"
esoph$age.num <- as.numeric(esoph$age.num)
esoph.glm2 <- glm(cbind(ncases,ncontrols)~alcgp+ditob+age.num,
data=esoph,family=binomial)
summary(esoph.glm2)
dev.off()