Constrained least squares estimation I need to fit a regression model using least squares in R with the constraint that the parameters are positive. they DON'T need to sum to one. because some times parameter sum exceeds one.
Can someone help me with the code pease
 A: One way to accomplish this task is to alter your model slightly. Rather than fitting the typical model of $$Y=\beta_0+\sum_{i=1}^{p}\beta_iX_i+\varepsilon$$ instead fit the model $$Y=e^{\beta_0}+\sum_{i=1}^{p}e^{\beta_i}X_i+\varepsilon$$ This can still be fit by least squares fairly easily in R and it has the effect of forcing the coefficients to be non-negative.
Edit
Here's some code to accomplish the model fitting. Using nls gives both parameter estimates and variances.
# generate data

set.seed(1)
x <- cbind(1, matrix(rnorm(900, 1:9), nrow=100))
beta <- runif(10, 0, 1)
y <- x %*% beta + rnorm(100)

# try using nls function

ests <- nls(y ~ exp(b0) + exp(b1)*x[,2] + exp(b2)*x[,3] + exp(b3)*x[,4] +
                          exp(b4)*x[,5] + exp(b5)*x[,6] + exp(b6)*x[,7] + 
                          exp(b7)*x[,8] + exp(b8)*x[,9] + exp(b9)*x[,10],
            start = list(b0 = runif(1), b1 = runif(1), b2 = runif(1), b3 = runif(1),
                         b4 = runif(1), b5 = runif(1), b6 = runif(1), b7 = runif(1),
                         b8 = runif(1), b9 = runif(1)))

summary(ests)

exp(coef(ests))

A: 1) You could fit it by putting box-type constraints to an optimizer that can deal with them - see ?optim and ?nlminb, for example.
2) You could use code like this: http://www.stat.colostate.edu/~meyer/constrparam.R
3) You could use a package designed to have positivity-constrained models, such as the package penalized
4) you can use nonlinear LS in various ways to ensure non-negative or strictly positive regressions.
A: This is a quadratic programming problem:
Let $\mathbf I\in\mathbb R^{n\times 9}$, be your design matrix ($n$ is the number of measurements, so each row contains one measurement with the investments $I_1,\ldots, I_9$), and $\boldsymbol \beta\in\mathbb R^9$ your linear coefficients, which you want to be nonnegative (at least some of them). Then you want to minimize the cost function $c(\mathbf I, \boldsymbol \beta)$, which is the sum of the quadratic residuals,  under the nonnegativity constraint on $\boldsymbol\beta$:
$$
\begin{align}
c(\mathbf X, \boldsymbol\beta) &:= \sum_{i=1}^n \left(\sum_{k=1}^9 I_{ik}\beta_k - Y_i\right)^2\\
\beta_i &\ge 0,\quad i=1,\ldots,9.
\end{align}
$$
Since $c$ is a quadratic function of $\boldsymbol \beta$ and you have inequality constraints on $\boldsymbol \beta$, this is a quadratic programming problem, and there is software for solving it.
A: The pcls() function in the mgcv package does partially constrained least squares. You can include equality and/or inequality constraints on some or all of the parameter estimates, and in fact on linear combinations of them. (It also allows for quadratic penalties, which are probably not relevant in your case.)
