# NOT RUN {
#Example 1
##Load the data
data(ais)
attach(ais)
##Setting
y<-BMI
x<-cbind(1,LBM,Sex)
## Fitting a median regression with Normal errors (by default)
modelF = lqr(y[Sex==1], x[Sex==1,1:2])
modelM = lqr(y[Sex==0], x[Sex==0,1:2])
plot(LBM,BMI,col=Sex+1,xlab="Lean Body Mass",ylab="Body4 Mass Index",main="Quantile Regression")
abline(a = modelF$beta[1],b = modelF$beta[2],lwd=2,col=2)
abline(a = modelM$beta[1],b = modelM$beta[2],lwd=2,col=4)
#COMPARING SOME MODELS for median regression
x<-cbind(1,LBM)
modelN = lqr(y,x,dist = "normal")
modelT = lqr(y,x,dist = "t")
modelL = lqr(y,x,dist = "laplace")
#Comparing AIC criterias
modelN$AIC;modelT$AIC;modelL$AIC
#This could be automatically done using best.lqr()
best.model = best.lqr(y, x, p = 0.75, criterion = "AIC")
#Let's use a grid of quantiles
modelfull = lqr(y,x,p = seq(from = 0.10,to = 0.90,by = 0.05),dist = "normal")
#Plotting quantiles 0.10,0.25,0.50,0.75 and 0.90
plot(LBM,BMI,xlab = "Lean Body Mass"
,ylab = "Body Mass Index", main = "Quantile Regression",pch=16)
abline(a = modelfull[[1]]$beta[1],b = modelfull[[1]]$beta[2],lwd=2,lty=1,col=2)
abline(a = modelfull[[17]]$beta[1],b = modelfull[[17]]$beta[2],lwd=2,lty=1,col=2)
abline(a = modelfull[[4]]$beta[1],b = modelfull[[4]]$beta[2],lwd=2,lty=1,col=3)
abline(a = modelfull[[14]]$beta[1],b = modelfull[[14]]$beta[2],lwd=2,lty=1,col=3)
abline(a = modelfull[[9]]$beta[1],b = modelfull[[9]]$beta[2],lwd=2,lty=1,col=4)
#Example 2
##Load the data
data(crabs,package = "MASS")
crabs$sex <- as.character(crabs$sex)
crabs$sex[crabs$sex=="M"]=0
crabs$sex[crabs$sex=="F"]=1
crabs$sex = as.numeric(crabs$sex)
attach(crabs)
head(crabs)
##Setting
y <- BD
x <- cbind(1,FL,sex)
plot(crabs)
## Fitting a median regression with Normal errors (by default)
modelF = lqr(y[sex==1], x[sex==1,1:2])
modelM = lqr(y[sex==0], x[sex==0,1:2])
plot(FL,BD,col=sex+1,xlab="Frontal lobe size",ylab="Body depth",main="Quantile Regression")
abline(a = modelF$beta[1],b = modelF$beta[2],lwd=2,col=2)
abline(a = modelM$beta[1],b = modelM$beta[2],lwd=2,col=4)
#COMPARING SOME MODELS for median regression
x<-cbind(1,FL)
modelN = lqr(y,x,dist = "normal")
modelT = lqr(y,x,dist = "t")
modelL = lqr(y,x,dist = "laplace")
modelS = lqr(y,x,dist = "slash")
modelC = lqr(y,x,dist = "cont" )
#Comparing AIC criterias
modelN$AIC;modelT$AIC;modelL$AIC;modelS$AIC;modelC$AIC
#Let's use a grid of quantiles
modelfull = lqr(y,x,p = seq(from = 0.10,to = 0.90,by = 0.05),dist = "t")
#Plotting quantiles 0.10,0.25,0.50,0.75 and 0.90
plot(FL,BD,xlab = "Frontal lobe size"
,ylab = "Body depth", main = "Quantile Regression",pch=16)
abline(a = modelfull[[1]]$beta[1],b = modelfull[[1]]$beta[2],lwd=2,lty=1,col=2)
abline(a = modelfull[[17]]$beta[1],b = modelfull[[17]]$beta[2],lwd=2,lty=1,col=2)
abline(a = modelfull[[4]]$beta[1],b = modelfull[[4]]$beta[2],lwd=2,lty=1,col=3)
abline(a = modelfull[[14]]$beta[1],b = modelfull[[14]]$beta[2],lwd=2,lty=1,col=3)
abline(a = modelfull[[9]]$beta[1],b = modelfull[[9]]$beta[2],lwd=2,lty=1,col=4)
# }
Run the code above in your browser using DataLab