### R code from vignette source 'testis-types-sol.rnw'

###################################################
### code chunk number 1: testis-types-sol.rnw:2-3
###################################################
library(Epi)


###################################################
### code chunk number 2: testis-types-sol.rnw:16-18
###################################################
th <- read.table( "../data/testis-hist.txt", header=T )
str( th )


###################################################
### code chunk number 3: testis-types-sol.rnw:28-32
###################################################
th <- subset( th, hist != 3 & age>15 & age<65 )
names(th)[match(c("age","diag","d","y"),names(th))] <- 
         c("A","P","D","Y")
th <- transform( th, hist=factor(hist,labels=c("Seminoma","non-Semi")) )
str( th )


###################################################
### code chunk number 4: testis-types-sol.rnw:37-39
###################################################
with( th, addmargins( tapply(D,list(floor(A/5)*5,hist),sum) ) )
with( th, addmargins( tapply(Y,list(floor(A/5)*5,hist),sum) ) )


###################################################
### code chunk number 5: testis-types-sol.rnw:56-67
###################################################
rr <- function( one, two )
{
one[,-1] <- log(one[,-1] )
two[,-1] <- log(two[,-1] )
sd.dif <- sqrt( ((one[,4]-one[,3])/3.92)^2 +
                ((two[,4]-two[,3])/3.92)^2 )
rat <- one
rat[,-1] <- exp( cbind( one[,2]-two[,2], sd.dif ) %*%
                 rbind( c(1,1,1), 1.96*c(0,-1,1) ) )
rat
}


###################################################
### code chunk number 6: APC-rr
###################################################
sem.1945 <- apc.fit( subset(th,hist=="Seminoma"),
                     ref.c=1945,
                     npar=c(8,5,15), scale=10^5 )
n.s.1945 <- apc.fit( subset(th,hist=="non-Semi"),
                     ref.c=1945,
                     npar=c(8,5,15), scale=10^5 )
sem.1920 <- apc.fit( subset(th,hist=="Seminoma"),
                     ref.c=1920,
                     npar=c(8,5,15), scale=10^5 )
n.s.1920 <- apc.fit( subset(th,hist=="non-Semi"),
                     ref.c=1920,
                     npar=c(8,5,15), scale=10^5 )
rrA.1945 <- rr( sem.1945$Age, n.s.1945$Age )
rrA.1920 <- rr( sem.1920$Age, n.s.1920$Age )
rrP.1945 <- rr( sem.1945$Per, n.s.1945$Per )
rrP.1920 <- rr( sem.1920$Per, n.s.1920$Per )
rrC.1945 <- rr( sem.1945$Coh, n.s.1945$Coh )
rrC.1920 <- rr( sem.1920$Coh, n.s.1920$Coh )
apc.plot( sem.1945,col="blue",lwd=1)
apc.lines(n.s.1945,col="red" ,lwd=1)
apc.lines(sem.1920,col="blue",lty="44",lwd=1)
apc.lines(n.s.1920,col="red" ,lty="44",lwd=1)
lines( rrA.1945[,1], rrA.1945[,2], lwd=2 )
lines( rrA.1920[,1], rrA.1920[,2], lwd=2, lty="22" )
pc.lines( rrP.1945[,1], rrP.1945[,2], lwd=2 )
pc.lines( rrP.1920[,1], rrP.1920[,2], lwd=2, lty="22" )
pc.lines( rrC.1945[,1], rrC.1945[,2], lwd=2 )
pc.lines( rrC.1920[,1], rrC.1920[,2], lwd=2, lty="22" )
abline(h=1)


