[R] Using constrOptim() function

spencerg spencer.graves at prodsyse.com
Thu Jun 4 14:14:02 CEST 2009


      It looks to me like you need to add "x" to the arguments in your 
call to "constrOptim", something like the following: 

constrOptim(beta_i, myFunction, NULL, ui, ci, mu = 1e-04, control = list(),
method = "Nelder-Mead",    outer.iterations = 100, outer.eps = 1e-05, x=x)


      The first "x" in "x=x" tells "constrOptim" that this is a named 
argument.  Since it's NOT the name of an argument for "constrOptim", it 
must be part of the "..." argument, which is offered to "f", which in 
this case is "myFunction".  The second "x" in "x=x" tells "constrOptim" 
to pass your global variable "x" for this purpose. 


      If this does not work, please submit another post, providing a 
simpler, self contained example.  Define "myFunction" as something 
simple to define that still generates your error.  Then someone can copy 
your code into R, run it and see your error.  That will tend on average 
to increase the probability of a response as well as its speed and 
utility. 


      Hope this helps,
      Spencer


Ali Mahani wrote:
> I have a function myFunction(beta,x) where beta is a vector of coefficients
> and x is a data frame (think of it as a matrix). I want to optimize the
> function myFunction() by ONLY changing beta, i.e. x stays constant, with 4
> constraints. I have the following code (with a separate source file for the
> function):
>
> rm(list=ls())
> source('mySourceFile')
> x=read.csv("myFile.csv",head=TRUE,sep=",")
> beta_i=c(1,1,1,1,1,1,-1)
> ui=rbind(c(1,0,0,0,0,0,0),c(0,1,0,0,0,0,0),c(0,0,1,0,0,0,0),c(0,0,0,0,0,0,-1))
> ci=c(0,0,0,0)
> constrOptim(beta_i, myFunction, NULL, ui, ci, mu = 1e-04, control = list(),
> method = "Nelder-Mead",    outer.iterations = 100, outer.eps = 1e-05)
>
> I am getting this error:
>
> Error in f(theta, ...) : argument "x" is missing, with no default
>
> If I replace myFunction with myFunction(beta,x) I get this error:
>
> Error in beta * feature_1[i, ] : non-numeric argument to binary operator
>
> If I try myFunction(beta_i,x) I get:
>
> Error in constrOptim(beta_i, mirror_lf(beta_i, x), NULL, ui, ci, mu = 1e-04, 
> : 
>   could not find function "f"
>
> Clearly, I don;t have a good understanding of how to use constrOptim() or
> optim() for that matter. Any guidance? Thank you!
>




More information about the R-help mailing list