How to constrain regression coefficient of two variables to have opposite sign? I am running a simple linear regression with a few variables but the meanings of the variables are such that certain pairs of variables should have coefficients with opposite signs. How should I specify the constraint?
 A: What you propose seems either dangerous or unnecessary.
Even if, on some theoretical basis, the 2 predictor variables have influences in different directions on the outcome variable, it's not necessarily the case that they will have opposite effects in the linear approximation that underlies the regression, particularly if there are other predictor variables involved. Forcing the coefficients to have opposite signs is making the very strong (and potentially dangerous) assumption that their opposing effects hold specifically in the linear model.
But if that is the case, then the regression should give you opposite signs for their coefficients anyway--as it apparently did. In that case the regression demonstrates directly your assumption about their opposing influences, and no one can complain that your results turned out that way because you forced them to.
So I'd recommend just sticking with the unconstrained regression.
A: Here is a straightforward, if inelegant, way of achieving this.
Let's say that the 2 parameters in question, i.e., those constrained to have opposite sign, are $a$ and $b$. Use a bound (or linearly constrained) least squares routine to solve two different problems, then pick the solution of those two which has the lowest sum of squared residuals.
Problem 1: Constrain $a \ge 0, b \le 0$, and all other parameters unconstrained.
Problem 2: Constrain $a \le 0, b \ge 0$, and all other parameters unconstrained.
Problems having a large number of such constraints, and even more complicated relationships among the parameters, could be solved systematically using a mixed integer (quadratic programming or second order cone problem) solver to impose the needed constraints without resorting to multiple problem solves.
EDIT: I don't disagree with the statements from @EdM and others regarding the need for judiciousness in deciding whether to impose constraints. I have merely stated how to do so if their imposition is appropriate.
