Quantreg : Unbalanced residuals I'm trying to use the quantreg package to fit an exponential curve.
Here is a reproductible example. IRL I have much more complex data with outliers, that's why I prefer not using nls which is not robust to outliers.
library(quantreg)
library(ggplot2)

x = 1:100
set.seed(42)
y = 500*exp(-0.02*x) +rnorm(100, 0, 5 )
df = data.frame(cbind(x,y))
plot(df)

formula =  y ~ k * exp(b*x) 
qr_exp = nlrq(formula,
                   data = df,
                   start = list(k = 600, b = -0.01),
                   tau = .50,
                   nlrq.control(maxiter=1000))
summary(qr_exp)
sum(qr_exp$m$resid())
[1] -26.52373

I expected to have sum(qr_exp$m$resid()) around 0 since  tau = 0.5but the value is negative which means the model tend to overestimate the real values.
As you can see I have sum of the residual is closer to 0 with tau= 0.47
formula =  y ~ k * exp(b*x) 
qr_exp = nlrq(formula,
              data = df,
              start = list(k = 600, b = -0.01),
              tau = .47,
              nlrq.control(maxiter=1000))
summary(qr_exp)
sum(qr_exp$m$resid())
[1] -4.467781

I don't really understand why !
Is it because there could be an infinite number of solution and so no garantee of having as much negative residual than positive residual ?
If yes what is the best solution if this is very important for me to:


*

*minimise Least absolute deviation  and not least square deviation (not robust with outliers)

*have balanced residual?


Could it make sense to add a small portion of L2 penalty to have something balanced ? (see Huber loss)
 A: You are conducting a median regression (tau=0.5) on asymmetrically distributed data. Here is a simpler example to show what is going on.
Suppose your asymmetric data are lognormal:
set.seed(1)
xx <- rlnorm(100,0,1)

Then what you are doing amounts to finding the median of your data.
median(xx)
[1] 1.121518

Now, the median minimizes the sum of absolute errors. It will not minimize the sum of "raw" errors:
sum(xx-median(xx))
[1] 52.74494

If you want a value that minimizes the sum of "raw" errors, you need to take the mean:
mean(xx)
[1] 1.648967
sum(xx-mean(xx))
[1] -9.992007e-15

So: if it is important to you that your fit yields zero average error, you will need to run an ordinary OLS regression. Which will, of course, be sensitive to outliers. (You incidentally found that the conditional mean is equal to the conditional 47% quantile. But that of course won't minimize absolute deviations.)
There is no way to have both minimal absolute deviation and balanced residuals if your distribution is asymmetric. You can of course find a tradeoff between median and OLS regression, perhaps by taking an average, or by regularizing in some other way (lasso, ridge regression, elastic net).
