### R code from vignette source 'Lexis-sol.rnw'
### Encoding: UTF-8

###################################################
### code chunk number 1: Lexis-sol.rnw:2-7
###################################################
options( width=90,
#        prompt=" ", continue=" ", # Makes it easier for students to
                                   # copy from the final pdf document
         SweaveHooks=list( fig=function()
         par(mar=c(3,3,1,1),mgp=c(3,1,0)/1.6,las=1,bty="n") ) )


###################################################
### code chunk number 2: Lexis-sol.rnw:35-38
###################################################
library( Epi )
data( occup )
occup


###################################################
### code chunk number 3: Lexis-sol.rnw:42-48
###################################################
oL <- Lexis( entry = list( age=AoE, per=DoE ),
              exit = list(          per=DoX ),
      entry.status = factor( rep("W",nrow(occup)) ),
       exit.status = factor( Xst ),
              data = occup )
summary( oL )


###################################################
### code chunk number 4: Lexis-sol.rnw:54-57
###################################################
sL <- splitLexis( oL, time="age", breaks=seq(0,100,5) )
sL <- splitLexis( sL, time="per", breaks=seq(0,100,5)+1900 )
sL[order(sL$lex.id,sL$age),]


###################################################
### code chunk number 5: Lexis-sol.rnw:61-62
###################################################
table( timeBand(sL,"age","left"), timeBand(sL,"per","left"))


###################################################
### code chunk number 6: BD0
###################################################
plot( sL )


###################################################
### code chunk number 7: BD1
###################################################
# This is the code-bit from the example on the help-page
par( mai=c(3,3,1,1)/4, mgp=c(3,1,0)/1.6 )
plot( sL, time.scale=2:1,
      las=1,col="black",lty.grid=1,lwd=2,type="l",
      xlim=c(1940,1960),ylim=c(40,55),xaxs="i",yaxs="i",yaxt="n",
      xlab="Calendar year", ylab="Age (years)")
axis( side=2, at=seq(40,55,5), las=1 )
points(sL,pch=c(NA,16)[(sL$lex.Xst=="D")+1] )
box()
# Annotation with the person-years
PY.ann.Lexis( sL, cex=0.8 )


###################################################
### code chunk number 8: Lexis-sol.rnw:90-94
###################################################
FU <- with( sL, cbind(lex.Xst=="D",lex.dur) )
colnames(FU) <- c("D","Y")
Age    <- timeBand(sL,"age","left")
Period <- timeBand(sL,"per","left")


###################################################
### code chunk number 9: Lexis-sol.rnw:98-100
###################################################
FUtab <- xtabs( FU ~ Age + Period )
ftable( FUtab, col.vars=2:3 )


###################################################
### code chunk number 10: Lexis-sol.rnw:106-112
###################################################
BC <- subset(sL,per-age>1902 & per-age<1912)
FU <- with( BC, cbind(lex.Xst=="D",lex.dur) )
colnames(FU) <- c("D","Y")
Age   <- timeBand(BC,"age","left")
FUctab <- xtabs( FU ~ Age )
FUctab


###################################################
### code chunk number 11: Lexis-sol.rnw:121-122
###################################################
sum( FUctab[,1] / FUctab[,2]*5 )


###################################################
### code chunk number 12: Lexis-sol.rnw:140-144
###################################################
p.rates <- data.frame( rate=c(100,200,400), Age=c(40,45,50) )
sL$Age <- timeBand(sL,"age","left")
sL <- merge( sL, p.rates)
sL


###################################################
### code chunk number 13: Lexis-sol.rnw:147-150
###################################################
O <- with( sL, sum( lex.Xst=="D" ) )
E <- with( sL, sum( lex.dur*rate/10^5 ) )
c(O,E,O/E)


