### R code from vignette source 'C:/Bendix/teach/NSCE/2022/pracs/cohort-measures-sol.rnw'

###################################################
### code chunk number 1: cohort-measures-sol.rnw:10-15
###################################################
options( width=110 )

Y.todis <- 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.todis


###################################################
### code chunk number 2: cohort-measures-sol.rnw:19-22
###################################################
Cases <- 5
Irate <- 100*Cases/Y.todis
round(Irate, 1)


###################################################
### code chunk number 3: cohort-measures-sol.rnw:27-29
###################################################
Y.todth <- Y.todis + 2 + 1.5 + 1 + 0.5 + 0.5
Y.todth


###################################################
### code chunk number 4: cohort-measures-sol.rnw:34-37
###################################################
Dth.C <- 1
Mrate.C <- 100*Dth.C/Y.todth
round(Mrate.C, 1)


###################################################
### code chunk number 5: cohort-measures-sol.rnw:41-44
###################################################
Dth.all <- 4
Mrate.all <- 100*Dth.all/Y.todth
round( Mrate.all, 1 )


###################################################
### code chunk number 6: cohort-measures-sol.rnw:48-50
###################################################
Mprop3.all <- 1 - exp( - (Mrate.all/100)*3 )
round(100*Mprop3.all, 1)


###################################################
### code chunk number 7: cohort-measures-sol.rnw:57-59
###################################################
Y.distodth <- Y.todth - Y.todis
Y.distodth


###################################################
### code chunk number 8: cohort-measures-sol.rnw:62-65
###################################################
D.pts <- 2
Mrate.pts <- 100*D.pts / Y.distodth
round( Mrate.pts, 1 )


###################################################
### code chunk number 9: cohort-measures-sol.rnw:69-71
###################################################
Mprop3.pts <- 1 - exp( -(Mrate.pts/100)* 3 )
round( 100*Mprop3.pts, 1 )


###################################################
### code chunk number 10: cohort-measures-sol.rnw:75-79
###################################################
N1 <- 7 ; N2 <- 5
D1 <- 1 ; D2 <- 3
P1 <- 100*D1/N1; P2 <- 100*D2/N2
round( c(P1, P2), 1)


###################################################
### code chunk number 11: cohort-measures-sol.rnw:82-85
###################################################
Prev <- c(D1, D2)/c(N1, N2)
names(Prev) <- c("30sep2006","31dec2008")
round( 100*Prev, 1 )


###################################################
### code chunk number 12: cohort-measures-sol.rnw:111-112
###################################################
library( Epi )


###################################################
### code chunk number 13: cohort-measures-sol.rnw:119-152
###################################################
coh <- data.frame( doe=c("2004-01-01",
                         "2004-01-01",
                         "2004-01-01",
                         "2004-07-01",
                         "2004-07-01",
                         "2005-01-01",
                         "2005-07-01",
                         "2006-01-01",
                         "2006-01-01",
                         "2006-07-01",
                         "2007-01-01",
                         "2007-01-01" ),
                   dox=c("2008-07-01",
                         "2009-07-01",
                         "2005-07-01",
                         "2007-07-01",
                         "2009-07-01",
                         "2006-07-01",
                         "2006-07-01",
                         "2008-07-01",
                         "2009-07-01",
                         "2009-07-01",
                         "2009-07-01",
                         "2008-07-01" ),
                   ddx=c("2006-07-01",
                         "2007-07-01",rep(NA,3),
                         "2005-07-01",rep(NA,2),
                         "2008-07-01",NA,
                         "2008-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 14: cohort-measures-sol.rnw:160-165
###################################################
cL <- Lexis( entry = list(per=cal.yr(doe)),
              exit = list(per=cal.yr(dox)),
       exit.status = xst,
                id = id,
              data = coh )


###################################################
### code chunk number 15: cohort-measures-sol.rnw:169-170
###################################################
summary( cL )


###################################################
### code chunk number 16: cohort-measures-sol.rnw:174-179
###################################################
cL <- cutLexis( cL,
               cut = cal.yr(cL$ddx),
         new.state = "Cancer",
  precursor.states = "Well" )
summary( cL )


###################################################
### code chunk number 17: cohort-measures-sol.rnw:189-190
###################################################
cL[order(cL$lex.id,cL$per),]


###################################################
### code chunk number 18: 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(2004,2009.5),
            lty=1, col=c("transparent","black")[as.integer(cL$lex.Cst)], lwd=4 )
abline( v=2003+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 19: boxes
###################################################
boxes( cL, boxpos=T )


###################################################
### code chunk number 20: cohort-measures-sol.rnw:228-229
###################################################
cS <- splitLexis( cL, breaks=2009 )


###################################################
### code chunk number 21: cohort-measures-sol.rnw:241-242
###################################################
cS[order(cS$lex.id),1:8]


###################################################
### code chunk number 22: boxes-r
###################################################
boxes( subset(cS,per<2009), boxpos=TRUE )


