# How can I constrain my regression coefficients such that they are positive and sum to be less than or equal to 1? (In R)

I've seen a few similar questions about constraining coefficients so they sum to 1, but I'm not sure if there's a simple change in these approaches to allow the sum to be anything in [0,1]. I need to implement this in R.

How do I fit a constrained regression in R so that coefficients total = 1?

Constrained linear regression through a specified point

The data sets I'm doing the regression on vary in size, so my model looks something like this: $Y = {\pi}_{1}{X}_{1}+{\pi}_{2}{X}_{2}+...+{\pi}_{n}{X}_{n}+\epsilon \quad s.t. \quad \sum_{i=1}^{n}{\pi}_{i} \le 1$ and ${\pi}_{i} \ge 0 \quad \forall i$

Before I realized I needed these constraints, I was using quadratic programming, finding $min||Y - (\sum_{i=1}^{n}{\pi}_{i}{X}_{i})||^{2}$

Thanks!

Edit: I'm not sure that what I'm doing actually has any validity, I'm just experimenting at the moment. Essentially it's sort of a mixture model where $Y$ has contributions from ${X}_{1}...{X}_{n}$ and then from some other source, and I want to see if I can estimate what proportion of Y comes from any X. The reason they don't necessarily sum to 1 is because of that unknown other source. Again, I'm just experimenting, so not sure if this will even work, just curious.

• How about adding a pi_0 with X_0=0, and using the technique for the coefficients totaling 1? Jul 30 '14 at 15:17
• There are many possible solutions, but choosing an appropriate one depends on understanding what $Y$ represents, on why these coefficients must lie on a simplex, and on the likely form of the errors. Could you include such information in edits to the question?
– whuber
Jul 30 '14 at 15:21
• @Aaron that makes sense, but in practice won't ${\pi}_{0}$ just be assigned as 0? Jul 30 '14 at 18:00
• @whuber for the moment I'm justing trying this with randomly generated matrices to see if I can get it to work. In practice, I'd like to apply it to some biological population data. Jul 30 '14 at 18:06
• Some tests I have done with simulated data indicate that a simple approach works extremely well when the posited model is the correct one--even when $n$ is large, the variance of $\epsilon$ is large, and there are not much data. This approach (a) includes an intercept term to account for the mean effect of the "other source", (b) applies ordinary least squares to develop initial estimates, and (c) forcibly renormalizes the coefficients to be positive and sum to unity. If this naive method does not work with your data, how exactly does it fail?
– whuber
Jul 30 '14 at 19:08