[R] Stuck using constrOptim
Ravi Varadhan
RVaradhan at jhmi.edu
Thu Apr 23 23:05:24 CEST 2009
Hi,
The constrOptim function used "Nelder-Mead" for the inner iterations, when
gradient is not specified. Nelder-Mead is not good when you have a
non-small number of parameters. I would sugest that you specify method =
"BFGS" in the call to constrOptim. This might help.
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of dre968
Sent: Thursday, April 23, 2009 4:22 PM
To: r-help at r-project.org
Subject: Re: [R] Stuck using constrOptim
Sorry for the not enough info. the code seems to work for 10 or so
variables but stops optimizing if i give it anymore than that.
#Load data
Data <- read.table......csv',head=TRUE,sep=",",stringsAsFactors = FALSE)
#Load goal variables. this is what i'm trying to minimize to
Z<-read.table....csv',head=TRUE,sep=",",stringsAsFactors = FALSE)
#set array
Y<-array(0,dim=c(nrow(Data),15))
#load applicable values into array
for(j in 1:15){
for(k in 1:nrow(Data)){
Y[k,j] = Data[k,3+j]
}
}
#set constraint
A <- array(0,dim=c(2+nrow(Y),nrow(Y)))
B <- array(0,dim=c(2+nrow(Y),1))
# Constraint for total = 100%
for(w in 1:nrow(Data)){
A[1,w] = 1
A[2,w] = -1
}
#each value in x must be greater than 0
for(i in 1:nrow(Data)){
A[i+2,i] = 1
B[i+2] = -.00001
}
B[1,1] = .999
B[2,1] = -1.001
#initial guess
p0<- array(0,dim=c(nrow(Data)))
p0[1] = .5
p0[2] = .5000001
#objective function
minsquare<-function(x,Y,Z){sum((x %*% Y - Z)^2)}
#Optimization with constraints
constrOptim(p0,minsquare,Y=Y,Z=Z,NULL,ui=A,ci=B)
here is the code.
--
View this message in context:
http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23200221.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at 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.
More information about the R-help
mailing list