
## com206a of october 2025
#  with exam project stk4021 from 2013 Cox data

# new keyboard!, irritating de lux, soeren klype,
# need to programme my finger and avoid what s written
# where is 'æ¨å å"Å^ØÆ* ø 
# 123 456 789 0+\
# !"# ¤%& /() =?`
# ¦¦ ¡@£ $5¥ 7[9 ]} Å^

# so the #3 is on the British Pound
# so () are om 89 not 90 

leuk <- rbind(
c( 3.36,  65), 
c( 2.88, 156),
c( 3.63, 100),
c( 3.41, 134),
c( 3.78,  16),
c( 4.02, 108),

c( 4.00, 121),
c( 4.23,   4),
c( 3.73,  39),
c( 3.85, 143),
c( 3.97,  56),
c( 4.51,  26),

c( 4.54,  22),
c( 5.00,   1),
c( 5.00,   1),
c( 4.72,   5),
c( 5.00,  65))

xx0 <- leuk[ ,1]
yy0 <- leuk[ ,2]

yy <- yy0/52
xx <- xx0 - mean(xx0)

nn <- length(yy) 

plot(xx,yy)

## Sir David 2-para

logL0 <- function(para)
{
beta0 <- para[1]
beta1 <- para[2]
#
aux <- log( dgamma(yy,exp(beta0-beta1*xx),1) )
sum(aux)
}

logL0(c(1,2))

minuslogL0 <- function(para)
{-logL0(para)}

nils0 <- nlm(minuslogL0,c(1,1),hessian=T)
ML0 <- nils0$estimate
Jhat0 <- nils0$hessian
se0 <- sqrt(diag(solve(Jhat0)))
well0 <- cbind(ML0,se0)
round(well0,4)

## nils 3-para

logL <- function(para)
{
beta0 <- para[1]
beta1 <- para[2]
delta <- para[3]
#
aux <- log( dgamma(yy,exp(beta0-beta1*xx),exp(delta)) )
sum(aux)
}

logL(c(1,2,3))

minuslogL <- function(para)
{-logL(para)}

nils <- nlm(minuslogL,c(1,1,0),hessian=T)
ML <- nils$estimate
Jhat <- nils$hessian
se <- sqrt(diag(solve(Jhat)))
well <- cbind(ML,se)
round(well,4)

#        ML     se
#[1,] 0.3671 0.3085
#[2,] 1.1166 0.2538
#[3,] 0.4083 0.3752

beta0hat <- ML[1]
beta1hat <- ML[2]
deltahat <- ML[3]

x0val <- seq(2.88,5.00,by=0.01)
xval <- x0val - mean(xx0) 

G05 <- qgamma(0.05,exp(beta0hat-beta1hat*xval),exp(deltahat))
G50 <- qgamma(0.50,exp(beta0hat-beta1hat*xval),exp(deltahat))
G95 <- qgamma(0.95,exp(beta0hat-beta1hat*xval),exp(deltahat))

par(bty="l")
par(cex.lab=1.4142) 

plot(xx0,yy,lwd=3,ylim=c(0,max(G95)), 
   xlab="white blood cell counts", ylab="survival time")

matlines(x0val,cbind(G05,G50,G95),type="l",lwd=3,
   lty=3,col=1)
   
sim <- 10^4
mcmc <- 0*(1:sim) %*% t(1:3) 

eps1 <- rnorm(sim)
eps2 <- rnorm(sim)
eps3 <- rnorm(sim)

uu <- runif(sim)
pracc <- 0*(1:sim)
ok <- 0*(1:sim)

mcmc[1, ] <- ML

for (ss in 2:sim)
{
old <- mcmc[ss-1, ]
prop <- old + 0.50*c(eps1[ss],eps2[ss],eps3[ss])
oi <- logL(prop) - logL(old)
pracc[ss] <- min(c(1,exp(oi)))
ok[ss] <- 1*(uu[ss] <= pracc[ss])
mcmc[ss, ] <- (1-ok[ss])*old + ok[ss]*prop
}

plot(mcmc[ ,1])
plot(mcmc[ ,2])
plot(mcmc[ ,3])

beta0sim <- mcmc[ ,1]
beta1sim <- mcmc[ ,2]
deltasim <- mcmc[ ,3]

xx0jones <- 3.33
## xx0jones <- 4.44 
xxjones <- xx0jones - mean(xx0) 

low <- qgamma(0.05,exp(beta0sim - beta1sim*xxjones),exp(deltasim))
high <- qgamma(0.95,exp(beta0sim - beta1sim*xxjones),exp(deltasim))

hist(low,breaks=40)
hist(high,breaks=40)

quantile(low,c(0.05,0.50,0.95))
quantile(high,c(0.05,0.50,0.95))

#     5%       50%       95% 
# 0.2081  0.6194   1.0749 low 
# 3.4552  4.7023   7.1226 high 

jonessim <- rgamma(sim,exp(beta0sim-beta1sim*xxjones),exp(deltasim))

plot(density(jonessim))

hist(jonessim,breaks=40)

2*(logL(ML) - logL0(ML0)) # 1.0168 too small for 3-para nils to be clever

aic0 <- 2*logL0(ML0) - 2*2 
aic1 <- 2*logL(ML) - 2*3

aic1 - aic0

bic0 <- 2*logL0(ML0) - 2*log(nn)
bic1 <- 2*logL(ML) - 3*log(nn)

bic1 - bic0 # negative; Sir David wins 

oi0 <- exp(0.5*bic0)
oi1 <- exp(0.5*bic1)

oi0 / (oi0 + oi1) # 0.7126
oi1 / (oi0 + oi1) # 0.1874


