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)