0

Following workflow for nonlinear quantile regression seems to work. However I don´t know how to plot the resulting curve.

btw.: I´d prefer to use the function graphics::curve() instead of graphics::lines()

require(quantreg)

# load sample data
dat <- DNase
# introduce variable
x <- DNase$conc
y <- DNase$density
# introduce function 
f <- function(a, b, x) {(a*x/(b+x))}
# fit the model
fm0 <- nls(log(y) ~ log(f(a,b,x)), dat, start = c(a = 1, b = 1))
# fit a nonlinear least-square regression
fit <- nls(y ~ f(a,b,x), dat, start = coef(fm0))
# receive coeffientes
co <- coef(fit)
a=co[1]
b=co[2]
# plot
plot(y~x) 
# add curve
curve((a*x/(b+x)), add=T)

# then fit the median using nlrq
dat.nlrq <- nlrq(y ~ SSlogis(x, Asym, mid, scal), data=dat, tau=0.5)
# add curve
???

EDIT: What I´m looking for is a way to plot various quantile regressions of a formula, like a*x/(b+x). Inserting the formula leads me to the question what to put as 'start' argument

dat.nlrq.075 <-  nlrq(formula=fit, data = dat, start=???, tau = 0.75)
N'ya
  • 347
  • 3
  • 13

1 Answers1

2

curve uses lines so there is really no reason to use curve when it's easier to use lines.

First ensure that data is sorted so plots come out right. Then fit with nls or nlrq and use fitted for the fitted line.

library(quantreg)

dat <- DNase[order(DNase$conc), ]
fit.nlrq <-  nlrq(density ~ SSlogis(conc, Asym, mid, scal), data = dat, tau = 0.5)
plot(density ~ conc, dat)
lines(fitted(fit.nlrq) ~ conc, dat)

If you want to plot the fit at a different number of equally spaced points such as 250 then do the same except use predict instead of fitted:

x <- seq(min(dat$conc), max(dat$conc), length = 250)
lines(predict(fit.nlrq, list(conc = x)) ~ x, lty = 2, col = "red")

The same style works with nls.

Note that if you use require its value should be checked. If you don't want to do that use library instead.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • thanks for the advices! your code works fine for the example. Maybe it´s not clearly formulated by me that I´m looking for a way to integrate a formula, like (a*x/(b+x), into the nlrq(). I´d like to plot curves of various quantiles of the the formula (a*x/(b+x) – N'ya Jul 09 '18 at 08:07