# NOT RUN {
##Load the data
data(resistance)
attach(resistance)
#EXAMPLE 1.1
#Comparing the resistence to death of two types of tumor-cells.
#The response is a score in [0,4].
boxplot(score~type)
#Median logistic quantile regression (Best fit distribution)
res = Log.best.lqr(y = score,x = cbind(1,type),a=0,b=4)
# The odds ratio of median score in type B vs type A
exp(res$beta[2])
#Proving that exp(res$beta[2]) is approx median odd ratio
medA = median(score[type=="A"])
medB = median(score[type=="B"])
rateA = (medA - 0)/(4 - medA)
rateB = (medB - 0)/(4 - medB)
odd = rateB/rateA
round(c(exp(res$beta[2]),odd),3) #better fitted
#EXAMPLE 1.2
############
#Comparing the resistence to death depending of dose.
#descriptive
plot(dose,score,ylim=c(0,4),col="dark gray");abline(h=c(0,4),lty=2)
dosecat<-cut(dose, 6, ordered = TRUE)
boxplot(score~dosecat,ylim=c(0,4))
abline(h=c(0,4),lty=2)
#(Non logistic) Best quantile regression for quantiles
# 0.05, 0.50 and 0.95
xx1 = dose
xx2 = dose^2
xx3 = dose^3
res31 = best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),p = 0.05)
res32 = best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),p = 0.50)
res33 = best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),p = 0.95)
res3 = list(res31,res32,res33)
seqq=seq(min(dose),max(dose),length.out = 1000)
dd = matrix(data = NA,nrow = 1000,ncol =3)
for(i in 1:3)
{
dd[,i] = rep(res3[[i]]$beta[1],1000) + res3[[i]]$beta[2]*seqq +
res3[[i]]$beta[3]*seqq^2 + res3[[i]]$beta[4]*seqq^3
}
plot(dose,score,ylim=c(-1,5),col="gray");abline(h=c(0,4),lty=2)
lines(seqq,dd[,1],lwd=1,col=2)
lines(seqq,dd[,2],lwd=1,col=1)
lines(seqq,dd[,3],lwd=1,col=2)
#Using logistic quantile regression for obtaining predictions inside bounds
res41 = Log.best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),a=0,b=4,p = 0.05)
res42 = Log.best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),a=0,b=4,p = 0.50)
res43 = Log.best.lqr(y = score,x = cbind(1,xx1,xx2,xx3),a=0,b=4,p = 0.95)
res4 = list(res41,res42,res43)
dd = matrix(data = NA,nrow = 1000,ncol =3)
for(i in 1:3)
{
dd[,i] = rep(res4[[i]]$beta[1],1000) + res4[[i]]$beta[2]*seqq +
res4[[i]]$beta[3]*seqq^2 + res4[[i]]$beta[4]*seqq^3
}
#Computing quantiles for the original response (Inverse trnasformation)
pred = function(predlog,a,b)
{
return((b*exp(predlog)+a)/(1+exp(predlog)))
}
for(i in 1:3)
{
dd[,i] = pred(dd[,i],a=0,b=4)
}
#No more prediction curves outof bounds
plot(dose,score,ylim=c(0,4),col="gray");abline(h=c(0,4),lty=2)
lines(seqq,dd[,1],lwd=1,col=2)
lines(seqq,dd[,2],lwd=1,col=1)
lines(seqq,dd[,3],lwd=1,col=2)
# }
Run the code above in your browser using DataLab