Debian Codegraph @

R/biglm.R

raw
1 #' @templateVar class biglm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `biglm` object created by a call to [biglm::biglm()] or
5 #' [biglm::bigglm()].
6 #' @template param_confint
7 #' @template param_exponentiate
8 #' @template param_unused_dots
9 #'
10 #' @evalRd return_tidy(regression = TRUE)
11 #'
12 #' @examplesIf rlang::is_installed("biglm")
13 #'
14 #' # load modeling library
15 #' library(biglm)
16 #'
17 #' # fit model -- linear regression
18 #' bfit <- biglm(mpg ~ wt + disp, mtcars)
19 #'
20 #' # summarize model fit with tidiers
21 #' tidy(bfit)
22 #' tidy(bfit, conf.int = TRUE)
23 #' tidy(bfit, conf.int = TRUE, conf.level = .9)
24 #'
25 #' glance(bfit)
26 #'
27 #' # fit model -- logistic regression
28 #' bgfit <- bigglm(am ~ mpg, mtcars, family = binomial())
29 #'
30 #' # summarize model fit with tidiers
31 #' tidy(bgfit)
32 #' tidy(bgfit, exponentiate = TRUE)
33 #' tidy(bgfit, conf.int = TRUE)
34 #' tidy(bgfit, conf.int = TRUE, conf.level = .9)
35 #' tidy(bgfit, conf.int = TRUE, conf.level = .9, exponentiate = TRUE)
36 #'
37 #' glance(bgfit)
38 #'
39 #' @export
40 #' @family biglm tidiers
41 #' @seealso [tidy()], [biglm::biglm()], [biglm::bigglm()]
42 tidy.biglm <- function(
43 x,
44 conf.int = FALSE,
45 conf.level = .95,
46 exponentiate = FALSE,
47 ...
48 ) {
49 # TODO: separate in biglm and bigglm tidiers
50
51 ret <- as_tibble(summary(x)$mat, rownames = "term")
52 colnames(ret) <- c(
53 "term",
54 "estimate",
55 "conf.low",
56 "conf.high",
57 "std.error",
58 "p.value"
59 )
60
61 # remove the 95% confidence interval and replace:
62 # it isn't exactly 95% (uses 2 rather than 1.96), and doesn't allow
63 # specification of confidence level in any case
64 ret <- dplyr::select(ret, -conf.low, -conf.high)
65
66 if (conf.int) {
67 ci <- broom_confint_terms(x, level = conf.level)
68 ret <- dplyr::left_join(ret, ci, by = "term")
69 }
70
71 if (exponentiate) {
72 ret <- exponentiate(ret)
73 }
74
75 ret
76 }
77
78
79 #' @templateVar class biglm
80 #' @template title_desc_glance
81 #'
82 #' @inherit tidy.biglm params examples
83 #' @template param_unused_dots
84 #'
85 #' @evalRd return_glance("r.squared",
86 #' "AIC",
87 #' "deviance",
88 #' "df.residual",
89 #' "nobs")
90 #'
91 #' @export
92 #' @family biglm tidiers
93 #' @seealso [glance()], [biglm::biglm()], [biglm::bigglm()]
94 glance.biglm <- function(x, ...) {
95 s <- summary(x)
96 as_glance_tibble(
97 r.squared = s$rsq,
98 AIC = stats::AIC(x),
99 deviance = stats::deviance(x),
100 df.residual = x$df.resid,
101 nobs = stats::nobs(x),
102 na_types = "rrrii"
103 )
104 }