Debian Codegraph @

R/lm-beta.R

raw
1 #' @templateVar class lm.beta
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `lm.beta` object created by [lm.beta::lm.beta].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @details If the linear model is an `mlm` object (multiple linear model),
11 #' there is an additional column `response`.
12 #'
13 #' If you have missing values in your model data, you may need to refit
14 #' the model with `na.action = na.exclude`.
15 #'
16 #' @examplesIf rlang::is_installed("lm.beta")
17 #'
18 #' # load libraries for models and data
19 #' library(lm.beta)
20 #'
21 #' # fit models
22 #' mod <- stats::lm(speed ~ ., data = cars)
23 #' std <- lm.beta(mod)
24 #'
25 #' # summarize model fit with tidiers
26 #' tidy(std, conf.int = TRUE)
27 #'
28 #' # generate data
29 #' ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
30 #' trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
31 #' group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
32 #' weight <- c(ctl, trt)
33 #'
34 #' # fit models
35 #' mod2 <- lm(weight ~ group)
36 #' std2 <- lm.beta(mod2)
37 #'
38 #' # summarize model fit with tidiers
39 #' tidy(std2, conf.int = TRUE)
40 #'
41 #' @export
42 #' @family lm tidiers
43 tidy.lm.beta <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
44 check_ellipses("exponentiate", "tidy", "lm.beta", ...)
45
46 ret <- as_tibble(summary(x)$coefficients, rownames = "term")
47 colnames(ret) <- c(
48 "term",
49 "estimate",
50 "std_estimate",
51 "std.error",
52 "statistic",
53 "p.value"
54 )
55
56 if (conf.int) {
57 ci <- broom_confint_terms(x, level = conf.level)
58 ret <- dplyr::left_join(ret, ci, by = "term")
59 }
60
61 ret
62 }