I'm trying to do a linear regression in R, however I have the added constraint the coefficients from the linear regression need to sum to a user given value between 0 and 1. (I understand forcing the coefficients like this will make the fit rather poor, but it's a needed constraint)

I've been though the documentation for lm() and the glmc package, as well as some similar questions, but none seem to tackle how to get the coefficients to sum to a specific value.

For data T, N, and target sum for coefficients p:

Regression <- function(T, N, p) {    
     fit <- lm(T[,1] ~ N)
     coef <- coef(fit)

Ideally I want sum(coef[-1]) = p (ignoring the intercept)

Sorry I can't provide more code, but I don't think I can do what I would like with lm().

Does anyone know of a way to do this, or of a package that will let me do this?

  • 3
    $\begingroup$ You will get better advice if you state why you want to constrain the coefficients. Because you can take any linear transformation to the variables and change the coefficients (but still have the same fit) the constraint is not always meaningful unless transforming the variables would also change the constraint. $\endgroup$
    – Andy W
    Jul 1, 2014 at 15:23

1 Answer 1


I would recommend you to work out theoretically the new regression, and then change the regression inputs in R accordingly. As an example, suppose your original regression equation is: $$ Y = b_0 + b_1X_1+b_2X_2+b_3X_3+\epsilon $$ Then substitute $b_3=p-b_1-b_2 $ in the above equation, rearrange to get:
$$ Y - pX_3 = b_0 + b_1(X_1-X_3) + b_2(X_2-X_3) + \epsilon $$

Calculate $Y^*=Y - pX_3$, $X_1^*=X_1 - X_3$, $X_2^*=X_2 - X_3$. Simply run a regular regression of $Y^*$ on $(X_1^*,X_2^*)$ in R using lm.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.