Debian Codegraph @

R/mlogit.R

raw
1 #' Tidying methods for logit models
2 #'
3 #' These methods tidy the coefficients of mnl and nl models generated
4 #' by the functions of the `mlogit` package.
5 #'
6 #' @param x an object returned from [mlogit::mlogit()].
7 #' @template param_confint
8 #' @template param_unused_dots
9 #'
10 #' @evalRd return_tidy(regression = TRUE)
11 #'
12 #' @examplesIf rlang::is_installed("mlogit")
13 #'
14 #' # load libraries for models and data
15 #' library(mlogit)
16 #'
17 #' data("Fishing", package = "mlogit")
18 #' Fish <- dfidx(Fishing, varying = 2:9, shape = "wide", choice = "mode")
19 #'
20 #' # fit model
21 #' m <- mlogit(mode ~ price + catch | income, data = Fish)
22 #'
23 #' # summarize model fit with tidiers
24 #' tidy(m)
25 #' augment(m)
26 #' glance(m)
27 #'
28 #' @aliases mlogit_tidiers
29 #' @export
30 #' @family mlogit tidiers
31 #' @seealso [tidy()], [mlogit::mlogit()]
32 #'
33 tidy.mlogit <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
34 check_ellipses("exponentiate", "tidy", "mlogit", ...)
35
36 # construct parameter table
37 s <- summary(x)
38 ret <- as_tidy_tibble(
39 s$CoefTable,
40 new_names = c("estimate", "std.error", "statistic", "p.value")
41 )
42
43 # calculate confidence interval
44 if (conf.int) {
45 ci <- broom_confint_terms(x, level = conf.level)
46 ret <- dplyr::left_join(ret, ci, by = "term")
47 }
48
49 ret
50 }
51
52 #' @templateVar class mlogit
53 #' @template title_desc_augment
54 #'
55 #' @evalRd return_augment(".fitted", ".probability")
56 #'
57 #' @inherit tidy.mlogit params examples
58 #' @param data Not currently used
59 #'
60 #' @details At the moment this only works on the estimation dataset. Need to set
61 #' it up to predict on another dataset.
62 #'
63 #' @export
64 #' @seealso [augment()]
65 #' @family mlogit tidiers
66 #'
67 #'
68 augment.mlogit <- function(x, data = x$model, ...) {
69 check_ellipses("newdata", "augment", "mlogit", ...)
70
71 # the ID variables are really messed up, so we're going to do some
72 # retrofitting because this ends up being a pretty important element of
73 # what we want to do with the results.
74 idx <- x$model$idx
75
76 reg <- x$model |>
77 as_augment_tibble() |>
78 dplyr::select(-idx) |>
79 # rename the column indicating the chosen alternative
80 dplyr::rename(
81 chosen = 1,
82 .probability = probabilities,
83 .fitted = linpred
84 ) |>
85 # reappend the id columns
86 dplyr::mutate(
87 id = idx[, 1],
88 alternative = idx[, 2],
89 .resid = as.vector(x$residuals)
90 ) |>
91 dplyr::select(id, alternative, chosen, everything())
92
93 reg
94 }
95
96 #' @templateVar class mlogit
97 #' @template title_desc_glance
98 #'
99 #' @inherit tidy.mlogit params examples
100 #'
101 #' @evalRd return_glance(
102 #' "logLik",
103 #' "rho2",
104 #' "rho20",
105 #' "AIC",
106 #' "BIC",
107 #' "nobs"
108 #' )
109 #' @export
110 #' @family mlogit tidiers
111 #' @seealso [glance()], [mlogit::mlogit()]
112 #'
113 #'
114 glance.mlogit <- function(x, ...) {
115 # compute mcfadden r2
116 # model log likelihood
117 llM <- as.numeric(logLik(x))
118 # null model: equal odds for all alternatives
119 n_alts <- length(x$freq)
120 ll0 <- sum(x$freq * log(1 / n_alts))
121 # market shares model: odds equal to chosen proportions
122 llC <- sum(x$freq * log(prop.table(x$freq)))
123
124 res <- as_glance_tibble(
125 logLik = llM,
126 rho2 = 1 - llM / llC,
127 rho20 = 1 - llM / ll0,
128 AIC = stats::AIC(x),
129 BIC = stats::BIC(x),
130 nobs = sum(x$freq),
131 na_types = "rrrrri"
132 )
133
134 res
135 }