Skip to content

Proposed fix for bug 17703. #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/library/stats/R/constrOptim.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,37 @@ constrOptim <-
} else function(theta, ...) dR(theta, theta.old, ...)
totCounts <- 0
s.mu <- sign(mu)
a <- NULL
optim_failure <- FALSE

for(i in seq_len(outer.iterations)) {
obj.old <- obj
r.old <- r
theta.old <- theta
a.old <- a

a <- optim(theta.old, fun, gradient, control = control,
method = method, hessian = hessian, ...)
r <- a$value
if (is.finite(r) && is.finite(r.old) &&
abs(r - r.old) < (1e-3 + abs(r)) * outer.eps) break
theta <- a$par
totCounts <- totCounts + a$counts

if (any(ui%*%theta-ci<0) || !is.finite(r) || any(!is.finite(theta))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use consistent indentation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange, the indentation looks fine in local...

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be due to the R sources' inconsistent use of tabs and spaces. Be sure to enable visible whitespace in your editor to help clarify. Generally, I recommend using non-invasive IDEs to work on the R sources to minimize diffs (I typically just use nano, for example, though emacs is also widely used and does better at respecting nearby whitespace choices).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was indeed the issue, thank you for your help! I thought neovim would also respect nearby whitespace choices, but apparently it does not :/

i <- i - 1
a <- a.old
optim_failure <- TRUE
break
}

totCounts <- totCounts + a$counts
obj <- f(theta, ...)
if (s.mu * obj > s.mu * obj.old) break
}
if (optim_failure) {
a$convergence <- 21 # See https://github.com/nashjc/optimx/blob/main/man/optimx.Rd
a$message <- gettextf("Returning solution from outer iteration %d, either because the solution is not in the feasible region or `optim()` provided non-finite outputs. Consider either checking the gradient implementation or using a derivative-free opimizer or reducing `outer.eps`.", i)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest removing the backticks

}
if (i == outer.iterations) {
a$convergence <- 7
a$message <- gettext("Barrier algorithm ran out of iterations and did not converge")
Expand Down
7 changes: 7 additions & 0 deletions src/library/stats/man/constrOptim.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ constrOptim(theta, f, grad, ui, ci, mu = 1e-04, control = list(),
\code{method = "Nelder-Mead"}. It should take arguments matching
those of \code{f} and return a vector containing the gradient.

In some rare cornercases, the call to \code{optim} inside the outer
loop might lead to non-finite function value or parameter values or
parameters outside the feasible region. In these cases, the outer
loop is stopped early and the last useful result is returned. The
\code{message} item in the output list warns the user of this
behaviour.

}
\value{
As for \code{\link{optim}}, but with two extra components:
Expand Down