# \donttest{
##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,ylab="score",xlab="type")
#Student't median logistic quantile regression
res = Log.lqr(score~type,data = resistance,a=0,b=4,dist="t")
# 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)
#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
p05 = best.lqr(score~poly(dose,3),data = resistance,p = 0.05)
p50 = best.lqr(score~poly(dose,3),data = resistance,p = 0.50)
p95 = best.lqr(score~poly(dose,3),data = resistance,p = 0.95)
res3 = list(p05,p50,p95)
plot(dose,score,ylim=c(-1,5),col="gray");abline(h=c(0,4),lty=2)
lines(sort(dose), p05$fitted.values[order(dose)], col='red', type='l')
lines(sort(dose), p50$fitted.values[order(dose)], col='blue', type='l')
lines(sort(dose), p95$fitted.values[order(dose)], col='red', type='l')
#Using Student's t logistic quantile regression for obtaining preditypeBions inside bounds
logp05 = Log.lqr(score~poly(dose,3),data = resistance,p = 0.05,b = 4,dist = "t") #a = 0 by default
logp50 = Log.lqr(score~poly(dose,3),data = resistance,p = 0.50,b = 4,dist = "t")
logp95 = Log.lqr(score~poly(dose,3),data = resistance,p = 0.95,b = 4,dist = "t")
res4 = list(logp05,logp50,logp95)
#No more predited curves out-of-bounds
plot(dose,score,ylim=c(-1,5),col="gray");abline(h=c(0,4),lty=2)
lines(sort(dose), logp05$fitted.values[order(dose)], col='red', type='l')
lines(sort(dose), logp50$fitted.values[order(dose)], col='blue', type='l')
lines(sort(dose), logp95$fitted.values[order(dose)], col='red', type='l')
#EXAMPLE 1.3
############
#A full model using dose and type for a grid of quantiles
res5 = Log.lqr(formula = score ~ poly(dose,3)*type,data = resistance,
a = 0,b = 4,
p = seq(from = 0.05,to = 0.95,by = 0.05),dist = "t",
silent = TRUE)
#A nice plot
if(TRUE){
par(mfrow=c(1,2))
typeB = (resistance$type == "B")
plot(dose,score,
ylim=c(0,4),
col=c(8*typeB + 1*!typeB),main="Type A")
abline(h=c(0,4),lty=2)
lines(sort(dose[!typeB]),
res5[[2]]$fitted.values[!typeB][order(dose[!typeB])],
col='red')
lines(sort(dose[!typeB]),
res5[[5]]$fitted.values[!typeB][order(dose[!typeB])],
col='green')
lines(sort(dose[!typeB]),
res5[[10]]$fitted.values[!typeB][order(dose[!typeB])],
col='blue',lwd=2)
lines(sort(dose[!typeB]),
res5[[15]]$fitted.values[!typeB][order(dose[!typeB])],
col='green')
lines(sort(dose[!typeB]),
res5[[18]]$fitted.values[!typeB][order(dose[!typeB])],
col='red')
plot(dose,score,
ylim=c(0,4),
col=c(1*typeB + 8*!typeB),main="Type B")
abline(h=c(0,4),lty=2)
lines(sort(dose[typeB]),
res5[[2]]$fitted.values[typeB][order(dose[typeB])],
col='red')
lines(sort(dose[typeB]),
res5[[5]]$fitted.values[typeB][order(dose[typeB])],
col='green')
lines(sort(dose[typeB]),
res5[[10]]$fitted.values[typeB][order(dose[typeB])],
col='blue',lwd=2)
lines(sort(dose[typeB]),
res5[[15]]$fitted.values[typeB][order(dose[typeB])],
col='green')
lines(sort(dose[typeB]),
res5[[18]]$fitted.values[typeB][order(dose[typeB])],
col='red')
}
# }
Run the code above in your browser using DataLab