Trying to use constrOptim to minimize the sum of squared deviations. I put
the objective function in as: sum((x %*% Y - Z)^2) so i'm trying to get
values for x to minimize the sum of the squared deviations between the
product of x and Y and Z.
Anyways i have no problem using this when x is a 3x1 test variable. it
works great with the constraints and everything. when i actually use it on
my data x is 3x900 or so and it wont optimize it just gives me back my
initial guess....this is the output i'm getting:
$value
[1] 22.7438
$counts
function gradient
906 NA
$convergence
[1] 1
$message
NULL
$outer.iterations
[1] 1
$barrier.value
[1] 1.381551e-06
this is definitely not the right answer and it is just spitting back my
initial guess back.
Any ideas? are there limits as to how big the variables can be for this
function?
--
View this message in context:
http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23197912.html
Sent from the R help mailing list archive at Nabble.com.
Sorry i Mean 900x1 not 3x900 below dre968 wrote:> > Trying to use constrOptim to minimize the sum of squared deviations. I > put the objective function in as: sum((x %*% Y - Z)^2) so i'm trying to > get values for x to minimize the sum of the squared deviations between the > product of x and Y and Z. > > Anyways i have no problem using this when x is a 3x1 test variable. it > works great with the constraints and everything. when i actually use it > on my data x is 3x900 or so and it wont optimize it just gives me back my > initial guess....this is the output i'm getting: > > $value > [1] 22.7438 > > $counts > function gradient > 906 NA > > $convergence > [1] 1 > > $message > NULL > > $outer.iterations > [1] 1 > > $barrier.value > [1] 1.381551e-06 > > this is definitely not the right answer and it is just spitting back my > initial guess back. > > Any ideas? are there limits as to how big the variables can be for this > function? >-- View this message in context: http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23198151.html Sent from the R help mailing list archive at Nabble.com.
The information you have provided is not enough. Please read the posting
guide on how ask for help. Provide a reproducible example so we can help
you.
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 3:20 PM
To: r-help at r-project.org
Subject: [R] Stuck using constrOptim
Trying to use constrOptim to minimize the sum of squared deviations. I put
the objective function in as: sum((x %*% Y - Z)^2) so i'm trying to get
values for x to minimize the sum of the squared deviations between the
product of x and Y and Z.
Anyways i have no problem using this when x is a 3x1 test variable. it
works great with the constraints and everything. when i actually use it on
my data x is 3x900 or so and it wont optimize it just gives me back my
initial guess....this is the output i'm getting:
$value
[1] 22.7438
$counts
function gradient
906 NA
$convergence
[1] 1
$message
NULL
$outer.iterations
[1] 1
$barrier.value
[1] 1.381551e-06
this is definitely not the right answer and it is just spitting back my
initial guess back.
Any ideas? are there limits as to how big the variables can be for this
function?
--
View this message in context:
http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23197912.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.
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.
if i run the same program with constrOptim(p0,minsquare,keys=keys,benKeys=benKeys,NULL,ui=A,ci=B,method="BFGS") i get the following error: Error in dR(theta, theta.old, ...) : could not find function "grad" i'm not very mathematically inclined, i dont even know what a gradient is. any suggestions? dre968 wrote:> > Trying to use constrOptim to minimize the sum of squared deviations. I > put the objective function in as: sum((x %*% Y - Z)^2) so i'm trying to > get values for x to minimize the sum of the squared deviations between the > product of x and Y and Z. > > Anyways i have no problem using this when x is a 3x1 test variable. it > works great with the constraints and everything. when i actually use it > on my data x is 3x900 or so and it wont optimize it just gives me back my > initial guess....this is the output i'm getting: > > $value > [1] 22.7438 > > $counts > function gradient > 906 NA > > $convergence > [1] 1 > > $message > NULL > > $outer.iterations > [1] 1 > > $barrier.value > [1] 1.381551e-06 > > this is definitely not the right answer and it is just spitting back my > initial guess back. > > Any ideas? are there limits as to how big the variables can be for this > function? >-- View this message in context: http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23203595.html Sent from the R help mailing list archive at Nabble.com.
so i set gr=Null:> constrOptim(p0,minsquare,Y=Y,Z=Z,NULL,gr=NULL,ui=A,ci=B,method="BFGS")and i recieved the following error: Error in optim(theta.old, fun, gradient, control = control, method = method, : objective function in optim evaluates to length 0 not 1 -- View this message in context: http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23204289.html Sent from the R help mailing list archive at Nabble.com.
I'm very interested. If you wouldn't mind I would love to see it. Thank you for the help. dre968 wrote:> > Trying to use constrOptim to minimize the sum of squared deviations. I > put the objective function in as: sum((x %*% Y - Z)^2) so i'm trying to > get values for x to minimize the sum of the squared deviations between the > product of x and Y and Z. > > Anyways i have no problem using this when x is a 3x1 test variable. it > works great with the constraints and everything. when i actually use it > on my data x is 3x900 or so and it wont optimize it just gives me back my > initial guess....this is the output i'm getting: > > $value > [1] 22.7438 > > $counts > function gradient > 906 NA > > $convergence > [1] 1 > > $message > NULL > > $outer.iterations > [1] 1 > > $barrier.value > [1] 1.381551e-06 > > this is definitely not the right answer and it is just spitting back my > initial guess back. > > Any ideas? are there limits as to how big the variables can be for this > function? >-- View this message in context: http://www.nabble.com/Stuck-using-constrOptim-tp23197912p23214410.html Sent from the R help mailing list archive at Nabble.com.