### R code from vignette source 'apc-fit-sol.rnw'

###################################################
### code chunk number 1: apc-fit-sol.rnw:2-3
###################################################
library(Epi)


###################################################
### code chunk number 2: apc-fit-sol.rnw:17-19
###################################################
th <- read.table( "../data/testis-hist.txt", header=T )
str( th )


###################################################
### code chunk number 3: apc-fit-sol.rnw:25-29
###################################################
tc <- aggregate( th[,c("age","diag","d","y")], list(A=th$age,P=th$diag), sum )
str( tc )
names( tc ) <- toupper( names(tc) )
tc <- tc[,c("A","P","D","Y")]


###################################################
### code chunk number 4: apc-fit-sol.rnw:34-38
###################################################
tc$Y <- tc$Y/3
tc$C <- tc$P - tc$A
str( tc )
head( tc )


###################################################
### code chunk number 5: rateplot
###################################################
par( mfrow=c(2,2), mar=c(3,3,1,1), mgp=c(3,1,0)/1.6 )
rateplot(
with( subset( tc, A>15 & A<60 ),
      tapply( D, list(floor(A/5)*5+2.5,
                      floor((P-1943)/5)*5+1945.5), sum ) /
      tapply( Y, list(floor(A/5)*5+2.5,
                      floor((P-1943)/5)*5+1945.5), sum ) * 10^5 ),
          col=topo.colors(12) )


###################################################
### code chunk number 6: apc-fit-sol.rnw:69-70
###################################################
tapc <- apc.fit( subset( tc, A>15 & A<60 ), npar=c(10,10,10), parm="ACP", ref.c=1918 )


###################################################
### code chunk number 7: apc1
###################################################
apc.plot( tapc, ci=TRUE )


###################################################
### code chunk number 8: apc2
###################################################
tapc <- apc.fit( subset( tc, A>15 & A<60 ), npar=c(10,10,20), 
                 parm="ACP", ref.c=1918, scale=10^5 )
fp <- apc.plot( tapc, ci=TRUE )


###################################################
### code chunk number 9: apc3
###################################################
tac.p <- apc.fit( subset( tc, A>15 & A<60 ), npar=c(10,10,20), 
                 parm="AC-P", ref.c=1918, scale=10^5 )
fp <- apc.plot( tapc, ci=TRUE )
apc.lines( tac.p, ci=TRUE, col="red", frame.par=fp )


###################################################
### code chunk number 10: apc-fit-sol.rnw:129-140
###################################################
par( mar=c(3,4,1,4), mgp=c(3,1,0)/1.7, las=1 )
fp <- apc.frame( a.lab=seq(20,60,10),
                 a.tic=seq(10,60,5),
                cp.lab=seq(1900,2000,20),
                cp.tic=seq(1885,2000,5),
                 r.lab=c(c(1,2,5)/10,1,2,5,10),
                 r.tic=c(1:9/10,1:10),
                   gap=8,
                rr.ref=1)
apc.lines( tapc, ci=TRUE, col="blue", frame.par=fp )
apc.lines( tac.p, ci=TRUE, col="red", frame.par=fp )                              


###################################################
### code chunk number 11: apc-fit-sol.rnw:145-147
###################################################
tap.c <- apc.fit( subset( tc, A>15 & A<60 ), npar=c(10,10,20), 
                  parm="AP-C", ref.p=1950, scale=10^5 )


###################################################
### code chunk number 12: apc4
###################################################
par( mar=c(3,4,1,4), mgp=c(3,1,0)/1.7, las=1 )
fp <- apc.frame( a.lab=seq(20,60,10),
                 a.tic=seq(10,60,5),
                cp.lab=seq(1900,2000,20),
                cp.tic=seq(1885,2000,5),
                 r.lab=c(c(1,2,5)/10,1,2,5,10),
                 r.tic=c(1:9/10,1:10),
                   gap=8,
                rr.ref=1)
apc.lines( tapc, ci=TRUE, col="blue", frame.par=fp )
apc.lines( tac.p, ci=TRUE, col="red", frame.par=fp )                              
apc.lines( tap.c, ci=TRUE, col=c("black","gray","black"), frame.par=fp ) 


###################################################
### code chunk number 13: apc-fit-sol.rnw:163-164 (eval = FALSE)
###################################################
## apc.lines( tap.c, ci=TRUE, col=c("black","gray","black"), frame.par=fp ) 


