# [ R + regression ] How do you set up a regression with restrictions on coefficients?

#### FinQnt

##### New Member
Hello,

I would like help imposing restrictions on coefficients estimated in the regression model show below:

lm(Y ~ C + S, weights = W)​

Y = stock returns
C = set of country membership binaries (1 or 0)
I = set of industry membership binaries (1 or 0)
W = market capitalization weights​

I'd like to estimate the country and industry effects as deviations from the capitalization weighted mean stock return. To do that, I need to impose two restrictions on the two sets of membership variable coefficients:

country coefficient betas * country weights = 0​
industry coefficient betas * industry weights = 0​

I can not seem to figure out how to implement these restrictions in R through the "lm" function. Does anyone know how?

#### chetan.apa

##### Member
Re: [ R + regression ] How do you set up a regression with restrictions on coefficien

I am not sure if there is any way to use lm for any function. But how about this

$$Y=X\beta + \epsilon ; R\beta=r$$

Introduce Lagrangian parameter (lambda) and minimize the function below wrt beta and lambda
$$(Y-X\beta)^t (Y-X\beta) + \lambda(r-R\beta)$$

#### FinQnt

##### New Member
Re: [ R + regression ] How do you set up a regression with restrictions on coefficien

Thanks so much for the response. As a finance guy first, I don't fully understand what your suggesting (totally on me). It seems like an optimization you are suggesting.... I guess if I could set up an optimization that minimizes the weighted squared residuals subject to constraints that the sector and country factors have a weighted mean of zero it would probably work. Do you happen to know how to do that with quadprog or some other R package?

#### chetan.apa

##### Member
Re: [ R + regression ] How do you set up a regression with restrictions on coefficien

Yes, it is an optimization..if you look at it carefully, the first term is squared error term and the second term is the restriction.
I have no idea if what you suggested would work and right now I am not aware of those packages.
However, the minimization I suggested has a closed form solution

$$\hat \beta^*=\hat \beta + (X^tX)^{-1} R^t[R(X^tX)^{-1}R^t]^{-1}(r-R\hat \beta)$$
where
$$\hat \beta =(X^tX)^{-1}X^tY$$

You need to just create your matrix X (which is nothing but a column of ones and rest of the columns as explanatory variable) and your vector R (which would be weights) and r will be 0. This will you give you beta coefficients at least.