3

If I have the following data

n<-1000
x1<-rnorm(n,1,1)
x2<-rnorm(n,2,2)
x3<-rnorm(n,3,3)
e<-rnorm(n)
y<-3+0.5*x1+0.2*x2+0.3*x3+e

I want to fit a linear model between $y$ and $x$ like: $$y=\alpha+\beta_1x_1+\beta_2x_2+\beta_3x_3+\epsilon$$ The unconstraint linear regression in R is

fit=lm(y~x1+x2+x3)

Now if I have some extra constraints for the coefficients:

(i) $\beta_i\ge0$, for $i=1,2,3$;

(ii) $\displaystyle\sum_{i=1}^3\beta_i=1$.

I still want to run a linear regression but with the two constraints above. How can I implement this constrained linear regression in R?

user208618
  • 141
  • 1
  • 13
J.Mike
  • 181
  • 5
  • 1
    To require $\sum_i \beta_i=1$, you can just write $\beta_3=1-\beta_1-\beta_2$, so this is a two parameter optimization. One way to fool R into using only positive values for $\beta$ is use $optim(logPars,ols)$ on the OLS sum of squares and pass $logPars = log(\beta_1,\beta_2)$ as parameters, then exponentiate them before calculating the sum of squares. – Peter Leopold Feb 15 '19 at 04:15

0 Answers0