

Dear List,
I am using constrOptim to solve the following
fr1 < function(x) {
b0 < x[1]
b1 < x[2]
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3
}
As you can see, my objective function is
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3 and I would
like to solve for both b0 and b1.
If I were to use optim then I would derive the gradient of the function
(grr) as follows:
fr2 <
expression(((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3)
grr < deriv(fr2,c("b0","b1"), func=TRUE)
and then simply use optim via
optim(c(5.2,0.22), fr1, grr)
My problem is that I wish to place constraints (b0>=0.2 and b1>= 0.1) upon
the values of b0 and b1. I can set the constraints matrix and boundary
values to
ui=rbind(c(1,0),c(0,1)) and ci=c(0.2,0.1), however, when I come to run
constrOptim function via
constrOptim(c(0.1,0.2), fr1, grr, ui=rbind(c(1,0),c(0,1)), ci=c(0.2,0.1))
I get the following error message:
"Error in .expr1 + b1 : 'b1' is missing"
So, it seems to me that I am doing something incorrectly in my
specification of grr in constrOptim.
If the list could help, I would be most grateful.
Regards
Mike Griffiths

*Michael Griffiths, Ph.D
*Statistician
*Upstream Systems*
8th Floor
Portland House
Bressenden Place
SW1E 5BH
< http://www.google.com/url?q=http%3A%2F%2Fwww.upstreamsystems.com%2F&sa=D&sntz=1&usg=AFrqEzfKYfaAalqvahwrpywpJDL9DxUmWw>
Tel +44 (0) 20 7869 5147
Fax +44 207 290 1321
Mob +44 789 4944 145
www.upstreamsystems.com< http://www.google.com/url?q=http%3A%2F%2Fwww.upstreamsystems.com%2F&sa=D&sntz=1&usg=AFrqEzfKYfaAalqvahwrpywpJDL9DxUmWw>
* [hidden email] < [hidden email]>*
< http://www.upstreamsystems.com/>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Michael Griffiths wrote
Dear List,
I am using constrOptim to solve the following
fr1 < function(x) {
b0 < x[1]
b1 < x[2]
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3
}
As you can see, my objective function is
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3 and I would
like to solve for both b0 and b1.
If I were to use optim then I would derive the gradient of the function
(grr) as follows:
fr2 <
expression(((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3)
grr < deriv(fr2,c("b0","b1"), func=TRUE)
and then simply use optim via
optim(c(5.2,0.22), fr1, grr)
My problem is that I wish to place constraints (b0>=0.2 and b1>= 0.1) upon
the values of b0 and b1. I can set the constraints matrix and boundary
values to
ui=rbind(c(1,0),c(0,1)) and ci=c(0.2,0.1), however, when I come to run
constrOptim function via
constrOptim(c(0.1,0.2), fr1, grr, ui=rbind(c(1,0),c(0,1)), ci=c(0.2,0.1))
I get the following error message:
"Error in .expr1 + b1 : 'b1' is missing"
So, it seems to me that I am doing something incorrectly in my
specification of grr in constrOptim.
grr is a function with two arguments. Do this
grr
and then you will see.
But the gradient function passed to constrOptim wants a function with a vector argument.
So if you do
gradr < function(x) {
b0 < x[1]
b1 < x[2]
grr(b0,b1)
}
and after testing with
gradr(c(0.1,0.2))
this should work
constrOptim(c(0.1,0.2), fr1, gradr, ui=rbind(c(1,0),c(0,1)), ci=c(0.2,0.1))
Berend


Berend Hasselman wrote
Michael Griffiths wrote
Dear List,
I am using constrOptim to solve the following
fr1 < function(x) {
b0 < x[1]
b1 < x[2]
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3
}
As you can see, my objective function is
((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3 and I would
like to solve for both b0 and b1.
If I were to use optim then I would derive the gradient of the function
(grr) as follows:
fr2 <
expression(((1/(1+exp(b0+b1))+(1/(1+exp(b0)))+(1/(1+exp(b0b1)))))/3)
grr < deriv(fr2,c("b0","b1"), func=TRUE)
and then simply use optim via
optim(c(5.2,0.22), fr1, grr)
My problem is that I wish to place constraints (b0>=0.2 and b1>= 0.1) upon
the values of b0 and b1. I can set the constraints matrix and boundary
values to
ui=rbind(c(1,0),c(0,1)) and ci=c(0.2,0.1), however, when I come to run
constrOptim function via
constrOptim(c(0.1,0.2), fr1, grr, ui=rbind(c(1,0),c(0,1)), ci=c(0.2,0.1))
I get the following error message:
"Error in .expr1 + b1 : 'b1' is missing"
So, it seems to me that I am doing something incorrectly in my
specification of grr in constrOptim.
grr is a function with two arguments. Do this
grr
and then you will see.
But the gradient function passed to constrOptim wants a function with a vector argument.
So if you do
gradr < function(x) {
b0 < x[1]
b1 < x[2]
grr(b0,b1)
}
This is incorrect.
The gradient function should return a vector. It was returning a scalar with attributes.
The gradient function should be
# Correct
gradr < function(x) {
b0 < x[1]
b1 < x[2]
g < grr(b0,b1)
attr(g,"gradient")
}
and this looks better
gradr(c(0.1,0.2))
str(gradr(c(0.1,0.2)))
constrOptim(c(0.1,0.2), fr1, gradr, ui=rbind(c(1,0),c(0,1)), ci=c(0.2,0.1))
I'm puzzled why constrOptim or optim didn't issue an error message in the original case.
Berend

