Skip to content
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

Add Standard Error for optim(), if Hessian is available #529

Closed
billdenney opened this issue Nov 17, 2018 · 5 comments · Fixed by #530
Closed

Add Standard Error for optim(), if Hessian is available #529

billdenney opened this issue Nov 17, 2018 · 5 comments · Fixed by #530

Comments

@billdenney
Copy link
Contributor

billdenney commented Nov 17, 2018

It would be helpful if tidy_optim() supported summarizing the Hessian, if present.

library(broom)

tidy_optim <- function(x, ...) {
  if (is.null(names(x$par))) {
    names(x$par) <- paste0("parameter", seq_along(x$par))
  }
  ret <- tibble(parameter = names(x$par), value = unname(x$par))
  if ("hessian" %in% names(x)) {
    ret$std.error <- sqrt(diag(solve(x$hessian)))
  }
  ret
}

r <- function(x) (3 - x[1])^2 + 4 * (x[2] - x[1]^2)^2

res <- optim(1:2, r)
tidy_optim(res)
#> Error in tibble(parameter = names(x$par), value = unname(x$par)): could not find function "tibble"

resH <- optim(1:2, r, hessian=TRUE)
tidy_optim(resH)
#> Error in tibble(parameter = names(x$par), value = unname(x$par)): could not find function "tibble"

Created on 2018-11-16 by the reprex package (v0.2.0).

(As mentioned in #505)

@bbolker
Copy link
Contributor

bbolker commented May 28, 2019

the only tricky part here is that this assumes that the objective function is a negative log-likelihood function. If someone provides, e.g., a sum-of-squares function, the "standard errors" (sqrt of diag of Hessian) will be bogus. Don't know if this is mentioned in the docs ...

@alexpghayes alexpghayes reopened this May 29, 2019
@billdenney
Copy link
Contributor Author

@bbolker, good point. Do you think that this should simply be a doc update, a warning, or something more?

@bbolker
Copy link
Contributor

bbolker commented May 29, 2019

A warning seems like it could be onerous (unless there's an option to disable it, which adds further complexity). I think a strongly worded comment in the documentation would be sufficient ...

@simonpcouch
Copy link
Collaborator

Opted to add a note in the docs warning about supplying the correct objective function. Feel free to drop a note here if you think there should be more discussion and I can reopen.🙂

@github-actions
Copy link

github-actions bot commented Mar 8, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants