Debian Codegraph @

R/nnet.R

raw
1 #' Tidying methods for multinomial logistic regression models
2 #'
3 #' These methods tidy the coefficients of multinomial logistic regression
4 #' models generated by `multinom` of the `nnet` package.
5 #'
6 #' @param x A `multinom` object returned from [nnet::multinom()].
7 #' @template param_confint
8 #' @template param_exponentiate
9 #' @template param_unused_dots
10 #'
11 #' @evalRd return_tidy("y.value", regression = TRUE)
12 #'
13 #' @examplesIf rlang::is_installed(c("nnet", "MASS"))
14 #'
15 #' # load libraries for models and data
16 #' library(nnet)
17 #' library(MASS)
18 #'
19 #' example(birthwt)
20 #'
21 #' bwt.mu <- multinom(low ~ ., bwt)
22 #'
23 #' tidy(bwt.mu)
24 #' glance(bwt.mu)
25 #'
26 #' # or, for output from a multinomial logistic regression
27 #' fit.gear <- multinom(gear ~ mpg + factor(am), data = mtcars)
28 #' tidy(fit.gear)
29 #' glance(fit.gear)
30 #'
31 #' @aliases multinom_tidiers nnet_tidiers
32 #' @export
33 #' @family multinom tidiers
34 #' @seealso [tidy()], [nnet::multinom()]
35 tidy.multinom <- function(
36 x,
37 conf.int = FALSE,
38 conf.level = .95,
39 exponentiate = FALSE,
40 ...
41 ) {
42 # when the response is a matrix, x$lev is null
43 if (is.null(x$lev)) {
44 n_lev <- ncol(x$residuals)
45 } else {
46 n_lev <- length(x$lev)
47 }
48
49 # when the dependent variable has only two levels, there is only one set of
50 # coefficients and coef returns a vector instead of a matrix. row.names is
51 # used to fetch y.level column in tidy output.
52 if (n_lev > 2) {
53 col_names <- colnames(coef(x))
54 row_names <- row.names(coef(x))
55 } else {
56 col_names <- names(coef(x))
57 row_names <- 1
58 }
59
60 s <- summary(x)
61
62 co <- coef(s)
63 coef <- matrix(
64 co,
65 byrow = FALSE,
66 nrow = n_lev - 1,
67 dimnames = list(
68 row_names,
69 col_names
70 )
71 )
72
73 se <- s$standard.errors
74 se <- matrix(
75 se,
76 byrow = FALSE,
77 nrow = n_lev - 1,
78 dimnames = list(
79 row_names,
80 col_names
81 )
82 )
83
84 multinomRowToDf <- function(r, coef, se, col_names) {
85 unrowname(data.frame(
86 y.level = rep(r, length(col_names)),
87 term = colnames(coef),
88 estimate = coef[r, ],
89 std.error = se[r, ],
90 stringsAsFactors = FALSE
91 ))
92 }
93
94 ret <- lapply(rownames(coef), multinomRowToDf, coef, se, col_names)
95 ret <- do.call("rbind", ret)
96
97 ret$statistic <- ret$estimate / ret$std.error
98 ret$p.value <- stats::pnorm(abs(ret$statistic), 0, 1, lower.tail = FALSE) * 2
99
100 if (conf.int) {
101 ci <- apply(
102 stats::confint(x, level = conf.level),
103 2,
104 function(a) unlist(as.data.frame(a))
105 )
106 ci <- as.data.frame(ci)
107 names(ci) <- c("conf.low", "conf.high")
108 ret <- cbind(ret, ci)
109 }
110
111 if (exponentiate) {
112 ret <- exponentiate(ret)
113 }
114
115 as_tibble(ret)
116 }
117
118 #' @templateVar class multinom
119 #' @template title_desc_glance
120 #'
121 #' @inherit tidy.multinom params examples
122 #'
123 #' @evalRd return_glance("edf", "deviance", "AIC", "nobs")
124 #' @export
125 #' @family multinom tidiers
126 #' @seealso [glance()], [nnet::multinom()]
127 glance.multinom <- function(x, ...) {
128 as_glance_tibble(
129 edf = x$edf,
130 deviance = x$deviance,
131 AIC = x$AIC,
132 nobs = stats::nobs(x),
133 na_types = "irri"
134 )
135 }