Debian Codegraph @

R/plm.R

raw
1 #' @templateVar class plm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `plm` objected returned by [plm::plm()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @examplesIf rlang::is_installed("plm")
11 #'
12 #' # load libraries for models and data
13 #' library(plm)
14 #'
15 #' # load data
16 #' data("Produc", package = "plm")
17 #'
18 #' # fit model
19 #' zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
20 #' data = Produc, index = c("state", "year")
21 #' )
22 #'
23 #' # summarize model fit with tidiers
24 #' summary(zz)
25 #'
26 #' tidy(zz)
27 #' tidy(zz, conf.int = TRUE)
28 #' tidy(zz, conf.int = TRUE, conf.level = 0.9)
29 #'
30 #' augment(zz)
31 #' glance(zz)
32 #'
33 #' @aliases plm_tidiers
34 #' @export
35 #' @seealso [tidy()], [plm::plm()], [tidy.lm()]
36 #' @family plm tidiers
37 tidy.plm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
38 check_ellipses("exponentiate", "tidy", "plm", ...)
39
40 s <- summary(x)
41
42 ret <- as_tibble(s$coefficients, rownames = "term")
43 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
44
45 if (conf.int) {
46 ci <- broom_confint_terms(x, level = conf.level)
47 ret <- dplyr::left_join(ret, ci, by = "term")
48 }
49
50 ret
51 }
52
53 # summary(plm) creates an object with class
54 #
55 # "summary.plm" "plm" "panelmodel"
56 #
57 # and we want to avoid these because they *aren't* plm objects
58
59 #' @export
60 tidy.summary.plm <- tidy.default
61
62
63 #' @templateVar class plm
64 #' @template title_desc_augment
65 #'
66 #' @inherit tidy.plm params examples
67 #' @template param_data
68 #'
69 #' @evalRd return_augment()
70 #'
71 #' @export
72 #' @seealso [augment()], [plm::plm()]
73 #' @family plm tidiers
74 augment.plm <- function(x, data = model.frame(x), ...) {
75 # Random effects and fixed effect (within model) have individual intercepts,
76 # thus we cannot take the ususal procedure for augment().
77 # Also, there is currently no predict() method for plm objects.
78 augment_columns(x, data, ...)
79 }
80
81
82 #' @templateVar class plm
83 #' @template title_desc_glance
84 #'
85 #' @inherit tidy.plm params examples
86 #'
87 #' @evalRd return_glance(
88 #' "r.squared",
89 #' "adj.r.squared",
90 #' statistic = "F-statistic",
91 #' "p.value",
92 #' "deviance",
93 #' "df.residual",
94 #' "nobs"
95 #' )
96 #'
97 #' @export
98 #' @seealso [glance()], [plm::plm()]
99 #' @family plm tidiers
100 glance.plm <- function(x, ...) {
101 s <- summary(x)
102 as_glance_tibble(
103 r.squared = unname(s$r.squared["rsq"]),
104 adj.r.squared = unname(s$r.squared["adjrsq"]),
105 statistic = unname(s$fstatistic$statistic),
106 p.value = unname(s$fstatistic$p.value),
107 deviance = unname(stats::deviance(x)),
108 df.residual = stats::df.residual(x),
109 nobs = stats::nobs(x),
110 na_types = "rrrrrii"
111 )
112 }