I have a problem where LAD regressions is not giving me a solution as the R package (L1pack
) errors whenever there is an infinite number of possible solutions*. This occurs in the following example: Suppose you want to find the constant $c$ that minimises the absolute distance between it and two $y$ values, that is, minimise $f(c) = |c - y_1| + |c - y_2|$. The median value of ${y_1, y_2}$ is a solution for $c$, as is any value between $y_1$ and $y_2$. For example:
set.seed(0)
tmp_df <- data.frame(y = sort(runif(2))) # generate data with 2 y values
> tmp_df
y
1 0.2655087
2 0.8966972
But running the regression L1pack
leads to the following error:
> L1pack::lad(y ~ 1, tmp_df)
Error in L1pack::lad(y ~ 1, tmp_df) :
L1FIT optimal solution is probably non-unique.
Now what I would like is for this regression to return the 'median' value in all cases where the solution is not unique. In this very simple example it is clear that if I took the mean value of $c, c \in [y_1, y_2]$, I would obtain the median. How do I extend this to a more general regression?
*: This is not an R specific question but I am using R to demonstrate the problem, also quantreg
package has no problems with supplying one solution even if the supplied solution is not the median.