R: Restrict OLS Coefficients i am currently setting up a portfolio on the Basis of cointegrating relationships for an assignment at uni. Therefore, I need to restrict my OLS coeffients, so that the total sum of coefficients is equal to 1. The unrestricted OLS estimation is given by:
lm(mran_port[,1] ~ mran_port[,2:draws])
where the dependent variable is the corresponding index. The explanatory variables are set up dynamically, so that the number of regressors depends on the value of draws. I already tried a reparamererization, but this does not give sufficient results. Is there a suitable function in R?
I hope someone can help me out.
 A: You can easily restrict OLS coefficients in R.  What I normally try and do is write my own likelihood function and optimize it with optim. In your case I would do the following:
w.fun=function(r){ #invertible function from R^(n-1) to unit n weight space (where coef's sum to 1)
  b=exp(c(0,r));
  b/sum(b)
} 

rest.reg=function(Y,X){ #restricted regression function
  n=ncol(X);
  start.init=rep(0,n-1) #loose one dimention from restirction

  nll=function(b) #negative log likelihood function Y~X
  {
    beta=w.fun(b); #restrict beta's to sum to 1
    resid=Y-X%*%beta;

    -sum(dnorm(resid,sd=sqrt(mean(resid^2)),log=TRUE)) ##output neg. log likelihood
  }
  return(optim(start.init,nll,method="BFGS",hessian=TRUE))
}
# random example
X=cbind(1,matrix(rnorm(1e3),ncol=10))

Y=rnorm(1e2)

m1=rest.reg(Y,X)
##coeficients
coef=w.fun(m1$par)
print(round(coef,3))
sum(coef)

the optim function optimizes the log-likelihood over the real-space and the parameters are transformed inside the log-likelihood to sum to 1 (I do this to make the function easier to optimize).  In the case of OLS, maximizing the log-likelihood (minimizing the negative log likelihood) is synonymous with minimizing mean squared error.   
Getting standard errors for the coefficients is a little tricky.  There are two basic ways to do it.  Either (1) apply the Delta method to the Hessian that is returned by rest.reg or (2) use a bootstrap.  I was lazy so I didn't do either of those.  The bootstrap is usually preferred if you have a lot of data, otherwise you could use the delta method. 
In either case, I am not so sure you need this restriction for finding a cointigration vector.  Since in a regression framework you are already effectively restricting the coefficient on the dependent to 1, you are guaranteed a unique solution if one exists...but maybe I do not understand what you are trying to do.
