-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathestimate_contrasts.R
176 lines (169 loc) · 6.66 KB
/
estimate_contrasts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#' Estimate Marginal Contrasts
#'
#' Run a contrast analysis by estimating the differences between each level of a
#' factor. See also other related functions such as [estimate_means()]
#' and [estimate_slopes()].
#'
#' @param contrast A character vector indicating the name of the variable(s)
#' for which to compute the contrasts.
#' @param p_adjust The p-values adjustment method for frequentist multiple
#' comparisons. Can be one of `"none"` (default), `"hochberg"`, `"hommel"`,
#' `"bonferroni"`, `"BH"`, `"BY"`, `"fdr"`, `"tukey"`, `"sidak"`, `"esarey"` or
#' `"holm"`. The `"esarey"` option is specifically for the case of Johnson-Neyman
#' intervals, i.e. when calling `estimate_slopes()` with two numeric predictors
#' in an interaction term. Details for the other options can be found in the
#' p-value adjustment section of the `emmeans::test` documentation or
#' `?stats::p.adjust`.
#' @param comparison Specify the type of contrasts or tests that should be
#' carried out.
#' * When `backend = "emmeans"`, can be one of `"pairwise"`, `"poly"`,
#' `"consec"`, `"eff"`, `"del.eff"`, `"mean_chg"`, `"trt.vs.ctrl"`,
#' `"dunnett"`, `"wtcon"` and some more. See also `method` argument in
#' [emmeans::contrast] and the `?emmeans::emmc-functions`.
#' * For `backend = "marginaleffects"`, can be a numeric value, vector, or
#' matrix, a string equation specifying the hypothesis to test, a string
#' naming the comparison method, a formula, or a function. Strings, string
#' equations and formula are probably the most common options and described
#' below. For other options and detailed descriptions of those options, see
#' also [marginaleffects::comparisons] and
#' [this website](https://marginaleffects.com/bonus/hypothesis.html).
#' * String: One of `"pairwise"`, `"reference"`, `"sequential"`, `"meandev"`
#' `"meanotherdev"`, `"poly"`, `"helmert"`, or `"trt_vs_ctrl"`.
#' * String equation: To identify parameters from the output, either specify
#' the term name, or `"b1"`, `"b2"` etc. to indicate rows, e.g.:`"hp = drat"`,
#' `"b1 = b2"`, or `"b1 + b2 + b3 = 0"`.
#' * Formula: A formula like `comparison ~ pairs | group`, where the left-hand
#' side indicates the type of comparison (`difference` or `ratio`), the
#' right-hand side determines the pairs of estimates to compare (`reference`,
#' `sequential`, `meandev`, etc., see string-options). Optionally, comparisons
#' can be carried out within subsets by indicating the grouping variable
#' after a vertical bar ( `|`).
#' @inheritParams estimate_means
#'
#' @inherit estimate_slopes details
#'
#' @examplesIf all(insight::check_if_installed(c("lme4", "marginaleffects", "rstanarm"), quietly = TRUE))
#' \dontrun{
#' # Basic usage
#' model <- lm(Sepal.Width ~ Species, data = iris)
#' estimate_contrasts(model)
#'
#' # Dealing with interactions
#' model <- lm(Sepal.Width ~ Species * Petal.Width, data = iris)
#'
#' # By default: selects first factor
#' estimate_contrasts(model)
#'
#' # Can also run contrasts between points of numeric, stratified by "Species"
#' estimate_contrasts(model, contrast = "Petal.Width", by = "Species")
#'
#' # Or both
#' estimate_contrasts(model, contrast = c("Species", "Petal.Width"), length = 2)
#'
#' # Or with custom specifications
#' estimate_contrasts(model, contrast = c("Species", "Petal.Width=c(1, 2)"))
#'
#' # Or modulate it
#' estimate_contrasts(model, by = "Petal.Width", length = 4)
#'
#' # Standardized differences
#' estimated <- estimate_contrasts(lm(Sepal.Width ~ Species, data = iris))
#' standardize(estimated)
#'
#' # Other models (mixed, Bayesian, ...)
#' data <- iris
#' data$Petal.Length_factor <- ifelse(data$Petal.Length < 4.2, "A", "B")
#'
#' model <- lme4::lmer(Sepal.Width ~ Species + (1 | Petal.Length_factor), data = data)
#' estimate_contrasts(model)
#'
#' data <- mtcars
#' data$cyl <- as.factor(data$cyl)
#' data$am <- as.factor(data$am)
#'
#' model <- rstanarm::stan_glm(mpg ~ cyl * wt, data = data, refresh = 0)
#' estimate_contrasts(model)
#' estimate_contrasts(model, by = "wt", length = 4)
#'
#' model <- rstanarm::stan_glm(
#' Sepal.Width ~ Species + Petal.Width + Petal.Length,
#' data = iris,
#' refresh = 0
#' )
#' estimate_contrasts(model, by = "Petal.Length = [sd]", test = "bf")
#' }
#'
#' @return A data frame of estimated contrasts.
#' @export
estimate_contrasts <- function(model, ...) {
UseMethod("estimate_contrasts")
}
#' @rdname estimate_contrasts
#' @export
estimate_contrasts.default <- function(model,
contrast = NULL,
by = NULL,
predict = NULL,
ci = 0.95,
comparison = "pairwise",
estimate = "average",
p_adjust = "none",
transform = NULL,
backend = getOption("modelbased_backend", "marginaleffects"),
verbose = TRUE,
...) {
if (backend == "emmeans") {
# Emmeans ------------------------------------------------------------------
estimated <- get_emcontrasts(model,
contrast = contrast,
by = by,
predict = predict,
comparison = comparison,
adjust = p_adjust,
verbose = verbose,
...
)
out <- .format_emmeans_contrasts(model, estimated, ci, p_adjust, ...)
} else {
# Marginalmeans ------------------------------------------------------------
estimated <- get_marginalcontrasts(model,
contrast = contrast,
by = by,
predict = predict,
comparison = comparison,
p_adjust = p_adjust,
ci = ci,
estimate = estimate,
transform = transform,
verbose = verbose,
...
)
out <- format(estimated, model, p_adjust, comparison, ...)
}
# restore attributes later
info <- attributes(estimated)
# Table formatting
attr(out, "table_title") <- c(ifelse(
estimate == "specific",
"Model-based Contrasts Analysis",
"Marginal Contrasts Analysis"
), "blue")
attr(out, "table_footer") <- .table_footer(
out,
by = info$contrast,
type = "contrasts",
model = model,
info = info
)
# Add attributes
attr(out, "model") <- model
attr(out, "response") <- insight::find_response(model)
attr(out, "ci") <- ci
attr(out, "p_adjust") <- p_adjust
attr(out, "backend") <- backend
# add attributes from workhorse function
attributes(out) <- utils::modifyList(attributes(out), info[.info_elements()])
# Output
class(out) <- c("estimate_contrasts", "see_estimate_contrasts", class(out))
out
}