# Finding a linear regression model that minimized percentage error in R

What is the way to find a linear regression model in R which minimizes the mean square error of residuals expressed in percents rather than the absolute difference?

Easy. There are actually several choices, first is to take the logarithm of the y-axis values, that converts multiplication into addition thus it converts relative error into absolute error.

Second choice, that you didn't ask for, would be to do the regression minimizing a different norm. That is, usually one minimizes $||model-y_{data}||$, where $||.||$ is the norm, A.K.A. the L2 norm, A.K.A. the absolute value of a vector difference, A.K.A. the square root of the sum of squares of the difference. To do this for proportional modeling one minimizes $||\frac{model}{y_{data}}-1||$.

• The question is how to do it in R. Currently, I am using a standard linear model such as fit<-lm(y~x1+x2+x3, data=df). Is there a way to specify a different error function? Commented Sep 30, 2016 at 23:56
• Simplest thing to do, $Y_{log}=logarithm(y)$, fit<-Im($Y_{log}$~x1+x2+x3,dat=df), whatever that is.
– Carl
Commented Oct 1, 2016 at 0:02
• This doesn't do exactly what I need though. It only scales the data, but what I want is to scale the errors. Also after applying a log to data it is not a linear model anymore so the fit is not good. For example if I have data which is y=ax+b, then log(y) is not linear in x anymore and linear fit will fail to achieve a good result. Commented Oct 1, 2016 at 15:12
• @Sasha To minimize $y=m x +b$ for proportional error with an offset of $b$, find a routine that minimizes and apply it to $||\frac{model-b}{y_{data}-b}-1||$.
– Carl
Commented Oct 1, 2016 at 15:55

The answer here is simple. The R function lm has a weighting option. If we want to minimize the sum of square percentage errors instead of just the sum of square error, simply weight the regression with 1/Y^2.

fit<-lm(y~x1+x2+x3, data=df, weights = 1/df\$y^2)


Note though however that this results in a biased estimator, it aint BLUE.