### R code from vignette source 'matchedCC-sol.rnw'

###################################################
### code chunk number 1: matchedCC-sol.rnw:7-11
###################################################
library( Epi )
casexp <- c(1,1,0,1,0,1,1,1,1,0, 0,1,1,0,1,1,1,1,0,1)
conexp <- c(0,0,0,1,1,0,0,0,1,0, 1,1,0,0,0,0,0,1,0,0)
cbind(casexp,conexp)


###################################################
### code chunk number 2: matchedCC-sol.rnw:17-25
###################################################
D1 <- sum(casexp)
D0 <- length(casexp) - sum(casexp)
C1 <- sum(conexp)
C0 <- length(conexp) - sum(conexp)
table.u <- rbind(c(D1, D0), c(C1, C0))
rownames(table.u) <- c("Cases", "Controls")
colnames(table.u) <- c("Exposed", "Unexposed")
table.u


###################################################
### code chunk number 3: matchedCC-sol.rnw:29-34
###################################################
EOR.un <- (D1/D0)/(C1/C0)
SE.lun <- sqrt( 1/D1 + 1/D0 + 1/C1 + 1/C0 )
EOR.un95low <- EOR.un / exp(1.96*SE.lun)
EOR.un95up <- EOR.un * exp(1.96*SE.lun)
round( cbind(EOR.un, SE.lun, EOR.un95low, EOR.un95up), 3 )


###################################################
### code chunk number 4: matchedCC-sol.rnw:40-46
###################################################
( table.m <- table( casexp, conexp ) )
EOR.mh  <- table.m[2,1] / table.m[1,2]
SE.lmh <- sqrt( 1/table.m[2,1] + 1/table.m[1,2] )
EOR.mh95lo <- EOR.mh / exp(1.96*SE.lmh)
EOR.mh95up <- EOR.mh * exp(1.96*SE.lmh)
round( cbind(EOR.mh, SE.lmh, EOR.mh95lo, EOR.mh95up), 3 )


###################################################
### code chunk number 5: matchedCC-sol.rnw:63-69
###################################################
exp <- c(casexp,conexp)
cc <- rep(1:0,each=length(casexp))
mc <- glm( cc ~ factor(exp), family=binomial )
summary( mc )
library(Epi)
ci.lin( mc, subset="exp", Exp=TRUE )[,5:7,drop=FALSE]


###################################################
### code chunk number 6: matchedCC-sol.rnw:77-78
###################################################
twoby2(table.u)


###################################################
### code chunk number 7: matchedCC-sol.rnw:87-89
###################################################
mp <- rep(1:length(casexp),2)
cbind( cc, exp, mp )


###################################################
### code chunk number 8: matchedCC-sol.rnw:93-96
###################################################
library( survival )
mm <- clogit( cc ~ exp + strata(mp) )
ci.lin( mm, subset="exp", Exp=TRUE )[,5:7,drop=FALSE]


