Skip to content

Commit

Permalink
Report standard errors when Hessian present in tidy_optim() (#530)
Browse files Browse the repository at this point in the history
Fixes #529
  • Loading branch information
billdenney authored and alexpghayes committed Nov 24, 2018
1 parent 4caeb56 commit c5d85b4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -38,7 +38,8 @@ Authors@R: c(
person("Ben", "Schneider", email = "benjamin.julius.schneider@gmail.com", role = "ctb"),
person("Patrick", "Kennedy", email = "pkqstr@protonmail.com", role = "ctb"),
person("Brian", "Fannin", email = "captain@pirategrunt.com", role = "ctb"),
person("Jason", "Muhlenkamp", email = "jason.muhlenkamp@gmail.com", role = "ctb"))
person("Jason", "Muhlenkamp", email = "jason.muhlenkamp@gmail.com", role = "ctb"),
person("Bill", "Denney", email = "wdenney@humanpredictions.com", role = "ctb", comment = c(ORCID = "0000-0002-5759-428X")))
Maintainer: David Robinson <admiral.david@gmail.com>
Description: Summarizes key information about statistical objects in tidy
tibbles. This makes it easy to report results, create plots and consistently
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -29,6 +29,7 @@ in the augment method for the chi sq test, .residuals column was renamed to .res
- tidy.kmeans now defaults to using variable names in output columns
- Bug fix for tidy.ridgelm returning inconsistent columns (#532)
- Correct output for `tidy.mlm(, quick=TRUE)`, add tests (#539 by @MatthieuStigler)
- `tidy_optim()` provides the standard error if the Hessian is present. (#529 by @billdenney)


## Deprecations
Expand Down
9 changes: 6 additions & 3 deletions R/list-optim-tidiers.R
Expand Up @@ -4,7 +4,7 @@
#' @param x A list returned from [stats::optim()].
#' @template param_unused_dots
#'
#' @evalRd return_tidy("parameter", "value")
#' @evalRd return_tidy("parameter", "value", "std.error", .post="\\code{std.error} is only provided as a column if the Hessian is calculated.")
#'
#' @examples
#'
Expand All @@ -21,10 +21,13 @@ tidy_optim <- function(x, ...) {
if (is.null(names(x$par))) {
names(x$par) <- paste0("parameter", seq_along(x$par))
}
tibble(parameter = names(x$par), value = unname(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
}


#' @templateVar class optim
#' @template title_desc_tidy_list
#'
Expand Down
1 change: 1 addition & 0 deletions man/broom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tidy_optim.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-list-optim.R
Expand Up @@ -20,3 +20,19 @@ test_that("optim tidiers works", {
gl <- glance(o)
check_glance_outputs(gl)
})

test_that("optim std.error inclusion works", {
func <- function(x) {
(x[1] - 2)^2 + (x[2] - 3)^2 + (x[3] - 8)^2
}

o <- optim(c(1, 1, 1), func, hessian = TRUE)

check_arguments(tidy_optim)
check_arguments(glance_optim)

td <- tidy(o)
check_tidy_output(td)
check_dims(td, 3, 3)
expect_true("std.error" %in% names(td))
})

0 comments on commit c5d85b4

Please sign in to comment.