This is not so much a question as a contribution, but comments are welcome. Comments: 1) thank you very much to Paul Smith in the post https://stat.ethz.ch/pipermail/r-help/2008-March/157249.html This is intended to build on that example with something more complex than a 2x2 set of constraints 2) "L-BFGS-B" does not appear to work in optimConst
Problem: let's say you have a function y= a + b1*x1 + b2*x2 + b3*x3 + b4*x4 and you want to minimize (where b and x are the matrices of the b's and x's) error = (a + b %*% x - y) ^ 2 subject to the constraints sum(b) = 1 0<b<1 The code: # rm(list=ls()) # FULL ON EXAMPLE # MASTER FORMULA: ui %*% theta >= ci # STEP 1: CREATE DATA x=matrix(rnorm(45),nrow=9)/100 y=matrix(rnorm(9),nrow=9)/100 thetas=matrix(1/5,nrow=5) # STEP 2: DEFINE ERROR FUNCTION fn=function(ws){ t(x) %*% y sum((x %*% ws-y)^2) } # STEP 3: DEFINE CONSTRAINTS a=rbind(c(0, 1, 1, 1, 1),c(0,-1,-1,-1,-1)) temp=cbind(0,rbind(diag(4),-diag(4))) adj=c(1.0000001,.9999999) a=rbind(a*adj,temp) b=c(1,-1,rep(0,4),rep(-1,4)) # STEP 3: DEFINE INITIAL STARTING POINT thetas=matrix(c(1/5,1/4,1/4,1/4,1/4),nrow=5) # STEP 3 (ALT): DEFINE INITIAL STARTING POINT #thetas=matrix(runif(5),nrow=5) #thetas[2:5]=thetas[2:5]/sum(thetas[2:5]) # ((TESTING, make sure at<b)) list('ci'=a,'ui'=b,data.frame('t'=thetas,'at'=a%*%thetas,'atb'=a%*%thetas-b)) # STEP 4: OPTIMIZATION o=constrOptim(thetas,fn,grad=NULL,ui=a,ci=b) o o$par sum(o$par)-o$par[1] [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.