Debian Codegraph @

R/mass-negbin.R

raw
1 #' @templateVar class negbin
2 #' @template title_desc_glance
3 #'
4 #' @param x A `negbin` object returned by [MASS::glm.nb()].
5 #' @template param_unused_dots
6 #'
7 #' @evalRd return_glance(
8 #' "null.deviance",
9 #' "df.null",
10 #' "logLik",
11 #' "AIC",
12 #' "BIC",
13 #' "deviance",
14 #' "df.residual",
15 #' "nobs"
16 #' )
17 #'
18 #' @examplesIf rlang::is_installed("MASS")
19 #'
20 #' # load libraries for models and data
21 #' library(MASS)
22 #'
23 #' # fit model
24 #' r <- glm.nb(Days ~ Sex / (Age + Eth * Lrn), data = quine)
25 #'
26 #' # summarize model fit with tidiers
27 #' tidy(r)
28 #' glance(r)
29 #'
30 #' @aliases glm.nb_tidiers
31 #' @family glm.nb tidiers
32 #' @seealso [glance()], [MASS::glm.nb()]
33 #' @export
34 glance.negbin <- function(x, ...) {
35 s <- summary(x)
36
37 ret <- tibble(
38 null.deviance = s$null.deviance,
39 df.null = s$df.null,
40 logLik = stats::logLik(x),
41 AIC = stats::AIC(x),
42 BIC = stats::BIC(x),
43 deviance = s$deviance,
44 df.residual = s$df.residual,
45 nobs = stats::nobs(x)
46 )
47
48 ret
49 }
50
51
52 #' @templateVar class negbin
53 #' @template title_desc_tidy
54 #' @template param_exponentiate
55 #'
56 #' @inherit glance.negbin examples
57 #'
58 #' @param x A `glm.nb` object returned by [MASS::glm.nb()].
59 #' @template param_confint
60 #' @param ... For `tidy()`, additional arguments passed to [summary()].
61 #' Otherwise ignored.
62 #'
63 #' @family glm.nb tidiers
64 #' @seealso [MASS::glm.nb()]
65 #' @export
66 tidy.negbin <- function(
67 x,
68 conf.int = FALSE,
69 conf.level = 0.95,
70 exponentiate = FALSE,
71 ...
72 ) {
73 s <- summary(x, ...)
74
75 ret <- tibble(
76 term = row.names(s$coefficients),
77 estimate = s$coefficients[, 1],
78 std.error = s$coefficients[, 2],
79 statistic = s$coefficients[, 3],
80 p.value = s$coefficients[, 4]
81 )
82
83 if (conf.int) {
84 ci <- broom_confint_terms(x, level = conf.level)
85 ret <- dplyr::left_join(ret, ci, by = "term")
86 }
87
88 if (exponentiate) {
89 ret <- exponentiate(ret)
90 }
91
92 ret
93 }