Skip to content

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

@billdenney

Description

@billdenney

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions