# Quantile curves (strange results at 0.975 quantile)

I want to calculate the quantile curves (0.025,0.25,0.5,0.75,0.975). The distribution of age is So I decided to work with quantile regression (nonlinear) so I run the following code

library(quantreg)
plot(dataExample$age,dataExample$y,xlab = "age", ylab = "y",type="n")

#' Fit first a nonlinear least-square regression
Data.nls <- nls(y ~ SSlogis(age, Asym, mid, scal), data=dataExample)
lines(0.008:19, predict(Data.nls, newdata=list(age=0.008:19)), col=1)

#' Then fit the median using nlrq
Data.nlrq <- nlrq(y ~ SSlogis(age, Asym, mid, scal), data=dataExample, tau=0.5, trace=TRUE)
lines(0.008:19, predict(Data.nlrq, newdata=list(age=0.008:19)), col=2)

#' The 1st and 3rd quartiles regressions
Data.nlrq <- nlrq(y ~ SSlogis(age, Asym, mid, scal), data=dataExample, tau=0.25, trace=TRUE)
lines(0.008:19, predict(Data.nlrq, newdata=list(age=0.008:19)), col=3)
Data.nlrq <- nlrq(y ~ SSlogis(age, Asym, mid, scal), data=dataExample, tau=0.75, trace=TRUE)
lines(0.008:19, predict(Data.nlrq, newdata=list(age=0.008:19)), col=3)

#' And finally "external envelopes" holding 95 percent of the data
Data.nlrq <- nlrq(y ~ SSlogis(age, Asym, mid, scal), data=dataExample, tau=0.025, trace=TRUE)
lines(0.008:19, predict(Data.nlrq, newdata=list(age=0.008:19)), col=4)

Data.nlrq <- nlrq(y ~ SSlogis(age, Asym, mid, scal), data=dataExample, tau=0.975, trace=TRUE)
lines(0.008:19, predict(Data.nlrq, newdata=list(age=0.008:19)), col=4)

leg <- c("least squares","median (0.5)","quartiles (0.25/0.75)",".95 band (0.025/0.975)")
legend(10, 30, legend=leg, lty=1, col=1:4)


But this is the results which I got (please look at the curve for 0.975 percentile curve). Any advice? Should I consider another method to calculate these quantile curves? Here is the result of applying Restricted cubic spline and I define the knots at knots=c(0.025,0.25,0.50,0.75,0.975). The p-value for nonlinear test for 0.975 is not significant,P-value=0.3. f1 <- Rq(y ~ rcs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), x=TRUE, y=TRUE, tau=0.025, data=dataExample)
anova(f1)
f2 <- Rq(y ~ rcs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), x=TRUE, y=TRUE, tau=0.25, data=dataExample)
anova(f2)
f3 <- Rq(y ~ rcs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), x=TRUE, y=TRUE, tau=0.50, data=dataExample)
anova(f3)
f4 <- Rq(y ~ rcs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), x=TRUE, y=TRUE, tau=0.75, data=dataExample)
anova(f4)
f5 <- Rq(y ~ rcs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), x=TRUE, y=TRUE, tau=0.975, data=dataExample)
anova(f5)
plot(dataExample$age,dataExample$y,xlab="Age",ylab="y",cex=0.25)
lines(dataExample$age, predict(f1, newdata=dataExample), col=4, lwd=2) lines(dataExample$age, predict(f2, newdata=dataExample), col=3, lwd=2)
lines(dataExample$age, predict(f3, newdata=dataExample), col=2, lwd=2) lines(dataExample$age, predict(f4, newdata=dataExample), col=3, lwd=2)
lines(dataExample\$age, predict(f5, newdata=dataExample), col=4, lwd=2)
leg <- c("median (0.5)","quartiles (0.25/0.75)",".95 band (0.025/0.975)")
legend("bottomright", legend=leg, lty=1, col=2:4)


Also this is the result of applying bs function (B-spline).

f1 <- rq(y ~ bs(age, knots=c(0.025,0.25,0.50,0.75,0.975)), tau=0.025, data=dataExample) • If you are sure about your data and nobody here has any better ideas then I would contact the package maintainer as this might be something malfunctioning. – mdewey Jan 11 '18 at 17:22
• Further exploration continued from link. I like your second plot better, but if you are going to do that maybe try B-spline, with some derivative fitting and more automatic node selection. Moreover, data transform may be better, post data so I can play with it to give you some hints. Disadvantage of spline fitting is that it is heuristic, and you may want to say something more revealing about your data. – Carl Jan 11 '18 at 18:05
• @Carl, thanks so much for your kindness, unfortunately, the data is confidential and I can not share it. But I will do my best to simulate something close to it. I use the B-spline smoothing. Please check the result (fourth plot). What do you think about this result? I defined knots=c(0.025,0.25,0.5,0.75,0.975) as I did not know how to define the degree of freedom. Is that ok? – shosho Jan 11 '18 at 20:44
• Try this as an explanation of knots. Maybe try 0 and 1 for your first and last knots, I think, if I am reading it correctly. – Carl Jan 11 '18 at 21:42
• @Carl, could you please check this link "stats.stackexchange.com/questions/322727/…". I used power normal transformation to transformed variables and you can see the new scatter plot between the transformed variables. – shosho Jan 12 '18 at 2:43