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

Andre Cesta aacesta at yahoo.com
Mon Jan 14 19:04:19 CET 2013

```
Hi Bert,

Your recommendation seems to minimize the error transformed with log, but not the percent error, that is, the model error divided by the observed value (for each observation).

I need to minimize the percent error.

With log, there could be a very large observation, with a very large error and even with log it would be larger than other errors and have more influence in the regression.  But if I take the percent error to minimize, this observation with large error would not have a larger influence.

Best regards, André

________________________________
From: Bert Gunter <gunter.berton at gene.com>
To: Andre Cesta <aacesta at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Monday, 14 January 2013, 13:34
Subject: Re: [R] How to fit a linear model to data by minimizing the mean absolute percent error?

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: