### R code from vignette source 'cohort-measures-sol.rnw'

###################################################
### code chunk number 1: cohort-measures-sol.rnw:2-3
###################################################
options( width=110 )


###################################################
### code chunk number 2: cohort-measures-sol.rnw:11-14
###################################################
Y <- 2.5 + 3.5 + 1.5 + 3.0 + 4.5 + 0.5 +
1.0 + 2.5 + 2.5 + 2.5 + 1.5 + 1.5
Y


###################################################
### code chunk number 3: cohort-measures-sol.rnw:18-19
###################################################
5/Y * 100


###################################################
### code chunk number 4: cohort-measures-sol.rnw:25-27
###################################################
Q <- 1 - exp( -5/Y * 3 )
Q


###################################################
### code chunk number 5: cohort-measures-sol.rnw:30-31
###################################################
round( Q*100, 1 )


###################################################
### code chunk number 6: cohort-measures-sol.rnw:37-38
###################################################
YY <- Y + 2 + 1.5 + 1 + 0.5 + 0.5


###################################################
### code chunk number 7: cohort-measures-sol.rnw:42-43
###################################################
1/YY


###################################################
### code chunk number 8: cohort-measures-sol.rnw:46-49
###################################################
Q <- 1 - exp( -1/YY * 3 )
Q
round( Q*100, 1 )


###################################################
### code chunk number 9: cohort-measures-sol.rnw:58-60
###################################################
4/YY
round( 4/YY * 100, 1 )


###################################################
### code chunk number 10: cohort-measures-sol.rnw:65-67
###################################################
Yc <- 2 + 1.5 + 1 + 0.5 + 0.5
Yc


###################################################
### code chunk number 11: cohort-measures-sol.rnw:70-72
###################################################
2 / Yc
round( 2/Yc*100, 1 )


###################################################
### code chunk number 12: cohort-measures-sol.rnw:76-79
###################################################
Q <- 1 - exp( -2/Yc * 3 )
Q
round( Q*100, 1 )


###################################################
### code chunk number 13: cohort-measures-sol.rnw:83-85
###################################################
1/7
3/5


###################################################
### code chunk number 14: cohort-measures-sol.rnw:88-91
###################################################
prv <- c(1,3)/c(7,5)
names(prv) <- c("30sep1996","31dec1998")
round( prv*100, 1 )


###################################################
### code chunk number 15: cohort-measures-sol.rnw:113-114
###################################################
library( Epi )


###################################################
### code chunk number 16: cohort-measures-sol.rnw:121-154
###################################################
coh <- data.frame( doe=c("1994-01-01",
                         "1994-01-01",
                         "1994-01-01",
                         "1994-07-01",
                         "1994-07-01",
                         "1995-01-01",
                         "1995-07-01",
                         "1996-01-01",
                         "1996-01-01",
                         "1996-07-01",
                         "1997-01-01",
                         "1997-01-01" ),
                   dox=c("1998-07-01",
                         "1999-07-01",
                         "1995-07-01",
                         "1997-07-01",
                         "1999-07-01",
                         "1996-07-01",
                         "1996-07-01",
                         "1998-07-01",
                         "1999-07-01",
                         "1999-07-01",
                         "1999-07-01",
                         "1998-07-01" ),
                   ddx=c("1996-07-01",
                         "1997-07-01",rep(NA,3),
                         "1995-07-01",rep(NA,2),
                         "1998-07-01",NA,
                         "1998-07-01",NA),
                   xst=factor(c(2,1,3,3,1,3,1,1,2,1,1,1),
                              labels= c("Well","Dead-Ca","Dead-Oth")),
                   id=1:12 )
coh


###################################################
### code chunk number 17: cohort-measures-sol.rnw:162-167
###################################################
cL <- Lexis( entry = list(per=cal.yr(doe)),
              exit = list(per=cal.yr(dox)),
       exit.status = xst,
                id = id,
              data = coh )


###################################################
### code chunk number 18: cohort-measures-sol.rnw:171-172
###################################################
summary( cL )


###################################################
### code chunk number 19: cohort-measures-sol.rnw:176-181
###################################################
cL <- cutLexis( cL,
               cut = cal.yr(cL$ddx),
         new.state = "Cancer",
  precursor.states = "Well" )
summary( cL )


###################################################
### code chunk number 20: cohort-measures-sol.rnw:191-192
###################################################
cL[order(cL$lex.id,cL$per),]


###################################################
### code chunk number 21: times
###################################################
par( mar=c(3,3,1,1), mgp=c(3,1,0)/1.6 )
plot( cL, ylim=c(12.5,0.5), ylab="", xlab="Date of follow-up",
            bty="n", las=1, yaxt="n", xlim=c(1994,1999.5),
            lty=1, col=c("transparent","black")[as.integer(cL$lex.Cst)], lwd=4 )
abline( v=1993+seq(0,7,0.5), col="gray" )
lines( cL, lty="11", col=c("black","transparent")[as.integer(cL$lex.Cst)], lwd=4 )
axis( side=2, labels=1:12, at=1:12, las=1, lty=0 )
# This is just to get the points of death to look nice
points( cL, col="white", pch=c(NA,NA,16,16)[as.integer(cL$lex.Xst)], cex=1.6 )
points( cL, col="black", pch=c(NA,NA,16,1 )[as.integer(cL$lex.Xst)], cex=1.6, lwd=3 )
points( cL, col="black", pch=c(NA,NA,1 ,1 )[as.integer(cL$lex.Xst)], cex=1.6, lwd=3 )


###################################################
### code chunk number 22: boxes
###################################################
boxes( cL, boxpos=T )


###################################################
### code chunk number 23: cohort-measures-sol.rnw:230-231
###################################################
cS <- splitLexis( cL, breaks=1999 )


###################################################
### code chunk number 24: cohort-measures-sol.rnw:243-244
###################################################
cS[order(cS$lex.id),1:8]


###################################################
### code chunk number 25: boxes-r
###################################################
boxes( subset(cS,per<1999), boxpos=TRUE )


