Debian Codegraph @

R/multcomp.R

raw
1 #' @templateVar class glht
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `glht` object returned by [multcomp::glht()].
5 #' @template param_confint
6 #' @param ... Additional arguments passed to [summary()] and
7 #' [tidy.summary.glht()].
8 #'
9 #' @evalRd return_tidy("contrast", "null.value", "estimate")
10 #'
11 #' @examplesIf rlang::is_installed(c("multcomp", "ggplot2"))
12 #'
13 #' # load libraries for models and data
14 #' library(multcomp)
15 #' library(ggplot2)
16 #'
17 #' amod <- aov(breaks ~ wool + tension, data = warpbreaks)
18 #' wht <- glht(amod, linfct = mcp(tension = "Tukey"))
19 #'
20 #' tidy(wht)
21 #'
22 #' ggplot(wht, aes(lhs, estimate)) +
23 #' geom_point()
24 #'
25 #' CI <- confint(wht)
26 #'
27 #' tidy(CI)
28 #'
29 #' ggplot(CI, aes(lhs, estimate, ymin = lwr, ymax = upr)) +
30 #' geom_pointrange()
31 #'
32 #' tidy(summary(wht))
33 #' ggplot(mapping = aes(lhs, estimate)) +
34 #' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) +
35 #' geom_point(aes(size = p), data = summary(wht)) +
36 #' scale_size(trans = "reverse")
37 #'
38 #' cld <- cld(wht)
39 #' tidy(cld)
40 #'
41 #' @aliases multcomp_tidiers
42 #' @export
43 #' @family multcomp tidiers
44 #' @seealso [tidy()], [multcomp::glht()]
45 #'
46 tidy.glht <- function(x, conf.int = FALSE, conf.level = .95, ...) {
47 check_ellipses("exponentiate", "tidy", "glht", ...)
48
49 glht_summary <- summary(x, ...)
50
51 tidy_glht_summary <- tidy.summary.glht(glht_summary, ...)
52
53 if (conf.int) {
54 tidy_glht_confint <- tidy.confint.glht(confint(x, level = conf.level, ...))
55
56 by_cols <- c("contrast", "estimate")
57 if ("term" %in% colnames(tidy_glht_summary)) {
58 by_cols <- c("term", by_cols)
59 }
60
61 tidy_glht_summary <- dplyr::select(tidy_glht_summary, -std.error) |>
62 dplyr::left_join(tidy_glht_confint, by = by_cols) |>
63 dplyr::select(
64 dplyr::contains("term"),
65 contrast,
66 null.value,
67 estimate,
68 conf.low,
69 conf.high,
70 dplyr::everything()
71 )
72
73 return(tidy_glht_summary)
74 }
75
76 tidy_glht_summary
77 }
78
79 #' @templateVar class confint.glht
80 #' @template title_desc_tidy
81 #'
82 #' @param x A `confint.glht` object created by calling
83 #' [multcomp::confint.glht()] on a `glht` object created with
84 #' [multcomp::glht()].
85 #' @template param_unused_dots
86 #'
87 #' @inherit tidy.glht examples
88 #' @evalRd return_tidy("contrast", "estimate", "conf.low", "conf.high")
89 #'
90 #' @export
91 #' @family multcomp tidiers
92 #' @seealso [tidy()], [multcomp::confint.glht()], [multcomp::glht()]
93 tidy.confint.glht <- function(x, ...) {
94 lhs_rhs <- glht_lhr_rhs_tibble(x)
95
96 coef <- as_tibble(x$confint)
97
98 colnames(coef) <- c("estimate", "conf.low", "conf.high")
99 coef$estimate <- as.vector(coef$estimate) # Remove attributes
100
101 bind_cols(glht_term_column(x), lhs_rhs[, "contrast", drop = FALSE], coef)
102 }
103
104 #' @templateVar class summary.glht
105 #' @template title_desc_tidy
106 #'
107 #' @param x A `summary.glht` object created by calling
108 #' [multcomp::summary.glht()] on a `glht` object created with
109 #' [multcomp::glht()].
110 #' @template param_unused_dots
111 #'
112 #' @inherit tidy.glht examples
113 #' @evalRd return_tidy(
114 #' "contrast",
115 #' "null.value",
116 #' "estimate",
117 #' "std.error",
118 #' "statistic",
119 #' "p.value"
120 #' )
121 #'
122 #' @export
123 #' @family multcomp tidiers
124 #' @seealso [tidy()], [multcomp::summary.glht()], [multcomp::glht()]
125 tidy.summary.glht <- function(x, ...) {
126 lhs_rhs <- glht_lhr_rhs_tibble(x)
127
128 coef <- as_tibble(
129 x$test[c("coefficients", "sigma", "tstat", "pvalues")]
130 )
131
132 if (x$test$type != "none") {
133 pvalue_colname <- "adj.p.value"
134 } else {
135 pvalue_colname <- "p.value"
136 }
137
138 names(coef) <- c("estimate", "std.error", "statistic", pvalue_colname)
139
140 bind_cols(glht_term_column(x), lhs_rhs, coef)
141 }
142
143
144 glht_lhr_rhs_tibble <- function(x) {
145 tibble(
146 contrast = stringr::str_replace(rownames(x$linfct), "^.+: ", ""),
147 null.value = x$rhs
148 )
149 }
150
151 glht_term_column <- function(x) {
152 if (!is.null(x$focus) && length(x$focus) == 1) {
153 tibble(term = rep(x$focus, nrow(x$linfct)))
154 } else if (!is.null(x$focus) && length(x$focus) > 1) {
155 term <- stringr::str_extract(rownames(x$linfct), "(^.+): .")
156 term <- stringr::str_replace(term, ": .$", "")
157 tibble(term = term)
158 }
159 }
160
161
162 #' @templateVar class cld
163 #' @template title_desc_tidy
164 #'
165 #' @param x A `cld` object created by calling [multcomp::cld()] on a
166 #' `glht`, `confint.glht()` or `summary.glht()` object.
167 #' @template param_unused_dots
168 #'
169 #' @inherit tidy.glht examples
170 #' @evalRd return_tidy("contrast", "letters")
171 #'
172 #' @export
173 #' @family multcomp tidiers
174 #' @seealso [tidy()], [multcomp::cld()], [multcomp::summary.glht()],
175 #' [multcomp::confint.glht()], [multcomp::glht()]
176 tidy.cld <- function(x, ...) {
177 vec <- x$mcletters$Letters
178 tidy_cld <- tibble(names(vec), vec)
179 colnames(tidy_cld) <- c(x$xname, "letters")
180 tidy_cld
181 }