Debian Codegraph @

R/speedglm-speedglm.R

raw
1 #' @templateVar class speedglm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `speedglm` object returned from [speedglm::speedglm()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @template param_unused_dots
8 #'
9 #' @evalRd return_tidy(regression = TRUE)
10 #'
11 #' @examplesIf rlang::is_installed("speedglm")
12 #'
13 #' # load libraries for models and data
14 #' library(speedglm)
15 #'
16 #' # generate data
17 #' clotting <- data.frame(
18 #' u = c(5, 10, 15, 20, 30, 40, 60, 80, 100),
19 #' lot1 = c(118, 58, 42, 35, 27, 25, 21, 19, 18)
20 #' )
21 #'
22 #' # fit model
23 #' fit <- speedglm(lot1 ~ log(u), data = clotting, family = Gamma(log))
24 #'
25 #' # summarize model fit with tidiers
26 #' tidy(fit)
27 #' glance(fit)
28 #'
29 #' @aliases speedglm_tidiers
30 #' @export
31 #' @family speedlm tidiers
32 #' @seealso [speedglm::speedglm()]
33 tidy.speedglm <- function(
34 x,
35 conf.int = FALSE,
36 conf.level = 0.95,
37 exponentiate = FALSE,
38 ...
39 ) {
40 ret <- as_tibble(coef(summary(x)), rownames = "term")
41 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
42
43 if (is.factor(ret$p.value)) {
44 ret$p.value <- as.numeric(levels(ret$p.value))[ret$p.value]
45 } else {
46 ret$p.value <- as.numeric(ret$p.value)
47 }
48
49 if (conf.int) {
50 ci <- broom_confint_terms(x, level = conf.level)
51 ret <- dplyr::left_join(ret, ci, by = "term")
52 }
53
54 if (exponentiate) {
55 ret <- exponentiate(ret)
56 }
57
58 as_tibble(ret)
59 }
60
61 #' @templateVar class speedglm
62 #' @template title_desc_glance
63 #'
64 #' @inherit tidy.speedglm params examples
65 #' @template param_unused_dots
66 #'
67 #' @evalRd return_glance(
68 #' "null.deviance",
69 #' "df.null",
70 #' "logLik",
71 #' "AIC",
72 #' "BIC",
73 #' "deviance",
74 #' "df.residual",
75 #' "nobs"
76 #' )
77 #'
78 #' @export
79 #' @family speedlm tidiers
80 #' @seealso [speedglm::speedlm()]
81 glance.speedglm <- function(x, ...) {
82 as_glance_tibble(
83 null.deviance = x$nulldev,
84 df.null = x$nulldf,
85 logLik = as.numeric(stats::logLik(x)),
86 AIC = stats::AIC(x),
87 BIC = stats::BIC(x),
88 deviance = x$deviance,
89 df.residual = x$df,
90 nobs = stats::nobs(x),
91 na_types = "rirrrrii"
92 )
93 }
94
95 #' @export
96 augment.speedglm <- augment.default