Debian Codegraph @

R/bbmle.R

raw
1 #' @templateVar class mle2
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `mle2` object created by a call to [bbmle::mle2()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @examplesIf rlang::is_installed("bbmle")
11 #'
12 #' # load libraries for models and data
13 #' library(bbmle)
14 #'
15 #' # generate data
16 #' x <- 0:10
17 #' y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
18 #' d <- data.frame(x, y)
19 #'
20 #' # fit model
21 #' fit <- mle2(y ~ dpois(lambda = ymean),
22 #' start = list(ymean = mean(y)), data = d
23 #' )
24 #'
25 #' # summarize model fit with tidiers
26 #' tidy(fit)
27 #'
28 #' @export
29 #' @seealso [tidy()], [bbmle::mle2()], [tidy_optim()]
30 #' @aliases mle2_tidiers bbmle_tidiers
31 tidy.mle2 <- function(x, conf.int = FALSE, conf.level = .95, ...) {
32 check_ellipses("exponentiate", "tidy", "mle2", ...)
33
34 co <- bbmle::coef(bbmle::summary(x))
35 ret <- ret <- as_tidy_tibble(
36 co,
37 new_names = c("estimate", "std.error", "statistic", "p.value")
38 )
39
40 if (conf.int) {
41 # can't use broom_confint / broom_confint_terms due to
42 # some sort of S4 object dispatch thing
43
44 ci <- bbmle::confint(x, level = conf.level)
45
46 # confint called on models with a single predictor
47 # often returns a named vector rather than a matrix :(
48
49 if (is.null(dim(ci))) {
50 ci <- matrix(ci, nrow = 1)
51 }
52
53 colnames(ci) <- c("conf.low", "conf.high")
54 ci <- as_tibble(ci)
55
56 ret <- dplyr::bind_cols(ret, ci)
57 }
58
59 as_tibble(ret)
60 }