Function to get odds ratios or rate ratios
Define a function (see bottom for result)
..glm.ratio <- function(GLM.RESULT, DIGITS = 2, P.DIGITS = 3, CONF.LEVEL = 0.95) {
if (GLM.RESULT$family$family == "binomial") {
LABEL <- "OR"
} else if (GLM.RESULT$family$family == "poisson") {
LABEL <- "RR"
} else {
stop("Not logistic or Poisson model")
}
## Extract coefficients and confidence interval
COEF <- stats::coef(GLM.RESULT)
CONFINT <- suppressMessages(stats::confint(GLM.RESULT, level = CONF.LEVEL))
TABLE <- cbind(coef = COEF, CONFINT)
## Turn them into OR
TABLE.EXP <- round(exp(TABLE), DIGITS)
colnames(TABLE.EXP)[1] <- LABEL
## Extract p-value
PVAL <- round(summary(GLM.RESULT)$coef[,4], P.DIGITS)
TABLE.EXP <- cbind(TABLE.EXP, "P" = PVAL)
TABLE.EXP
}
Load dataset
## Fix cases 10 and 39 to make them identical to Dr. Orav's dataset
lbw <- read.table("http://www.umass.edu/statdata/statdata/data/lowbwt.dat", head = T, skip = 4)
lbw[c(10,39),"BWT"] <- c(2655,3035)
## Change variable names to lower cases
names(lbw) <- tolower(names(lbw))
Recode data
## Recoding
lbw <- within(lbw, {
## Relabel race
race.cat <- factor(race, levels = 1:3, labels = c("White","Black","Other"))
## Categorize ftv (frequency of visit)
ftv.cat <- cut(ftv, breaks = c(-Inf, 0, 2, Inf), labels = c("None","Normal","Many"))
ftv.cat <- relevel(ftv.cat, ref = "Normal")
## Dichotomize ptl
preterm <- factor(ptl >= 1, levels = c(FALSE,TRUE), labels = c("0","1+"))
## Categorize smoke ht ui
smoke <- factor(smoke, levels = 0:1, labels = c("No","Yes"))
ht <- factor(ht, levels = 0:1, labels = c("No","Yes"))
ui <- factor(ui, levels = 0:1, labels = c("No","Yes"))
})
Get odds ratios and Wald p-values
logit.full <- glm(low ~ age + lwt + smoke + ht + ui + ftv.cat + race.cat + preterm, data = lbw, family = binomial)
..glm.ratio(logit.full)
OR 2.5 % 97.5 % P
(Intercept) 1.56 0.13 19.43 0.724
age 0.97 0.89 1.04 0.355
lwt 0.99 0.97 1.00 0.032
smokeYes 2.16 0.94 5.03 0.070
htYes 6.09 1.57 26.74 0.011
uiYes 2.07 0.82 5.14 0.118
ftv.catNone 1.29 0.59 2.87 0.518
ftv.catMany 2.23 0.53 9.11 0.262
race.catBlack 3.29 1.15 9.61 0.027
race.catOther 2.12 0.87 5.32 0.101
preterm1+ 3.50 1.41 8.96 0.007
Using epicalc package
library(epicalc)
logistic.display(logit.full)
Logistic regression predicting low
crude OR(95%CI) adj. OR(95%CI) P(Wald's test) P(LR-test)
age (cont. var.) 0.95 (0.89,1.01) 0.97 (0.9,1.04) 0.355 0.351
lwt (cont. var.) 0.99 (0.97,1) 0.99 (0.97,1) 0.032 0.023
smoke: Yes vs No 2.02 (1.08,3.78) 2.16 (0.94,4.95) 0.07 0.068
ht: Yes vs No 3.37 (1.02,11.09) 6.09 (1.51,24.52) 0.011 0.009
ui: Yes vs No 2.58 (1.14,5.83) 2.07 (0.83,5.13) 0.118 0.122
ftv.cat: ref.=Normal 0.514
None 1.84 (0.95,3.59) 1.29 (0.59,2.83) 0.518
Many 2.34 (0.66,8.28) 2.23 (0.55,9.09) 0.262
race.cat: ref.=White 0.055
Black 2.33 (0.94,5.77) 3.29 (1.15,9.42) 0.027
Other 1.89 (0.96,3.74) 2.12 (0.86,5.23) 0.101
preterm: 1+ vs 0 4.32 (1.92,9.73) 3.5 (1.4,8.75) 0.007 0.007
Log-likelihood = -97.7515
No. of observations = 189
AIC value = 217.5031