# [R] optimize linear function

Michaell Taylor mt at michaelltaylor.com
Sat Jun 12 23:15:11 CEST 2004

```
I am attempting to optimize a regression model's parameters to meet a specific
target for the sum of positive errors over sum of the dependent variable
(minErr below).

I see two courses of action , 1) estimate a linear model then iteratively
reduce the regressors to achieve the desired positive error threshold
(naturally the regressors and predicted values are biased - but this is
acceptable).  Were the problem only a single independent variable problem,
this approach would be fairly trivial.  But the two variable model proves
more troublesome from an efficiency point of view (efficiency being the mean
negative error for a given minErr).

The second method, which seems more efficient, is to optimize the linear
regressors until the criteria is met.  There are several optimizer packages
out there, but none seem particularly well suited to this sort of task.

Any suggestions out there?

===========  Sample Problem = first approach  ================

# if we say...
B1 <- .8
B2 <- .2

# then construct some data..
x1 <- rnorm(100,mean=100,sd=20)
x2 <- rnorm(100,mean=10,sd=2)
y <- B1*x1+B2*x2+rnorm(100,mean=5,sd=10)
# normally of course B1 and B2 are unknown to begin with

# linear model  (presumably yielding B1=.8 and B2=.2)
m<-lm(y ~ x1+ x2)
# test on summation of positive errors.
e <- resid(m)
minErr <- (sum(ifelse(e<0,0,e))/sum(y))-.03
while (minErr>.03){
Make some adjustment to B1 and B2
minErr <- (sum(ifelse(e<0,0,e))/sum(y))-.03
}

======== Second Approach ===========

construct a function to minimize minErr with beginning values from 'm'.

Any suggestions for approaches greatly appreciated.

```