# [R] How to fit a linear model to data by minimizing the mean absolute percent error?

Bert Gunter gunter.berton at gene.com
Mon Jan 14 16:34:10 CET 2013

```Take the logs of both side and minimize the absolute error on the log
scale, then transform your results back. The quantreg package does L1
regression.

If you want to know **why**, this works, consult a local statistician
or post to a statistical list like stats.stackexchange.com.  This is
not an R question.

--- Bert

On Mon, Jan 14, 2013 at 4:22 AM, Andre Cesta <aacesta at yahoo.com> wrote:
>
>
> Hi All,
>
> I wonder if you can help me with an aparently simple task.  I have been searching examples for this without any luck:
>
> #Assume
> x<-1:10  #x ranges from 1 to 10.
> y<-x*runif(10)+ 1.5*x  #y is a linear function of x with some error. Add uniform error that is scaled to be larger as x values also become larger
>
> #error is proportional to x size, this should cause heterocedasticity.
>
>
> #I know there are many methods to deal with heterocedasticity, but in my specific case, I want to use percent regression to minimize the mean absolute
> #percentual error as opposed to regular regression that deals with the square of the errors.
>
> #Question, how to fit a linear model to minimize this error on the data y ~ x above?
> #Please do not use model<-lm(y ~ x....) as this will minimize the square of the errors, not the mean absolute percent error
>
> Best regards, André Cesta
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website: