Debian Codegraph @

R/lmtest.R

raw
1 #' @templateVar class coeftest
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `coeftest` object returned from [lmtest::coeftest()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @examplesIf rlang::is_installed("lmtest")
11 #'
12 #' # load libraries for models and data
13 #' library(lmtest)
14 #'
15 #' m <- lm(dist ~ speed, data = cars)
16 #'
17 #' coeftest(m)
18 #' tidy(coeftest(m))
19 #' tidy(coeftest(m, conf.int = TRUE))
20 #'
21 #' # a very common workflow is to combine lmtest::coeftest with alternate
22 #' # variance-covariance matrices via the sandwich package. The lmtest
23 #' # tidiers support this workflow too, enabling you to adjust the standard
24 #' # errors of your tidied models on the fly.
25 #' library(sandwich)
26 #'
27 #' # "HC3" (default) robust SEs
28 #' tidy(coeftest(m, vcov = vcovHC))
29 #'
30 #' # "HC2" robust SEs
31 #' tidy(coeftest(m, vcov = vcovHC, type = "HC2"))
32 #'
33 #' # N-W HAC robust SEs
34 #' tidy(coeftest(m, vcov = NeweyWest))
35 #'
36 #' # the columns of the returned tibble for glance.coeftest() will vary
37 #' # depending on whether the coeftest object retains the underlying model.
38 #' # Users can control this with the "save = TRUE" argument of coeftest().
39 #' glance(coeftest(m))
40 #' glance(coeftest(m, save = TRUE))
41 #'
42 #' @export
43 #' @seealso [tidy()], [lmtest::coeftest()]
44 #' @aliases lmtest_tidiers coeftest_tidiers
45 #' @family coeftest tidiers
46 tidy.coeftest <- function(x, conf.int = FALSE, conf.level = .95, ...) {
47 check_ellipses("exponentiate", "tidy", "coeftest", ...)
48
49 co <- as.data.frame(unclass(x))
50 ret <- as_tidy_tibble(
51 co,
52 new_names = c("estimate", "std.error", "statistic", "p.value")[1:ncol(co)]
53 )
54
55 if (conf.int) {
56 ci <- broom_confint_terms(x, level = conf.level)
57
58 # handle one-dimensional case (#1227)
59 if (identical(dim(ci), c(1L, 3L))) {
60 ci[["term"]] <- rownames(x)
61 }
62
63 ret <- dplyr::left_join(ret, ci, by = "term")
64 }
65 ret
66 }
67
68
69 #' @templateVar class coeftest
70 #' @template title_desc_glance
71 #'
72 #' @inherit tidy.coeftest params examples
73 #'
74 #' @evalRd return_glance(
75 #' "r.squared",
76 #' "adj.r.squared",
77 #' "sigma",
78 #' "statistic",
79 #' "p.value",
80 #' "df",
81 #' "logLik",
82 #' "AIC",
83 #' "BIC",
84 #' "deviance",
85 #' "df.residual",
86 #' "nobs"
87 #' )
88 #' @note Because of the way that lmtest::coeftest() retains information about
89 #' the underlying model object, the returned columns for glance.coeftest() will
90 #' vary depending on the arguments. Specifically, four columns are returned
91 #' regardless: "Loglik", "AIC", "BIC", and "nobs". Users can obtain additional
92 #' columns (e.g. "r.squared", "df") by invoking the "save = TRUE" argument as
93 #' part of lmtest::coeftest(). See examples.
94 #'
95 #' As an aside, goodness-of-fit measures such as R-squared are unaffected by the
96 #' presence of heteroskedasticity. For further discussion see, e.g. chapter 8.1
97 #' of Wooldridge (2016).
98 #' @references Wooldridge, Jeffrey M. (2016) \cite{Introductory econometrics: A
99 #' modern approach.} (6th edition). Nelson Education.
100 #'
101 #'
102 #' @export
103 #' @seealso [glance()], [lmtest::coeftest()]
104 #' @family coeftest_tidiers
105 glance.coeftest <- function(x, ...) {
106 # check whether the underlying model object was saved as an attribute of the
107 # coeftest object; i.e. with coeftest(x, save = TRUE). If so, we'll use that
108 # for our glance function.
109 if (!is.null(attr(x, "object"))) {
110 ret <- glance(attr(x, "object"), ...)
111 } else {
112 # If model has not been saved, extract from retained attributes and notify user.
113 ret <- tibble::tibble(
114 logLik = sprintf("%.3f", logLik(x)),
115 AIC = AIC(x),
116 BIC = BIC(x),
117 nobs = nobs(x)
118 )
119 cli::cli_inform(
120 c(
121 "Original model not retained as part of coeftest object.",
122 "i" = "For additional model summary information (r.squared, df, etc.),
123 consider passing {.fn glance.coeftest} an object where the
124 underlying model has been saved, i.e.
125 {.code lmtest::coeftest(..., save = TRUE)}."
126 ),
127 .frequency = "once",
128 .frequency_id = "glance_coeftest_inform"
129 )
130 }
131
132 ret
133 }