Skip to content

constrOptim and problem with derivative

3 messages · Michael Griffiths, Berend Hasselman

#
Michael Griffiths wrote
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


--
View this message in context: http://r.789695.n4.nabble.com/constrOptim-and-problem-with-derivative-tp4217531p4217776.html
Sent from the R help mailing list archive at Nabble.com.
#
Berend Hasselman wrote
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




--
View this message in context: http://r.789695.n4.nabble.com/constrOptim-and-problem-with-derivative-tp4217531p4218207.html
Sent from the R help mailing list archive at Nabble.com.