|
24 | 24 | # we have to print a different error message. |
25 | 25 | emmeans_options <- c("scheffe", "mvt", "dunnettx") |
26 | 26 |
|
27 | | - all_methods <- c(tolower(stats::p.adjust.methods), emmeans_options, "tukey", "sidak", "esarey", "sup-t") |
| 27 | + all_methods <- c( |
| 28 | + tolower(stats::p.adjust.methods), |
| 29 | + emmeans_options, |
| 30 | + "tukey", |
| 31 | + "sidak", |
| 32 | + "esarey", |
| 33 | + "sup-t" |
| 34 | + ) |
28 | 35 | insight::validate_argument(p_adjust, all_methods) |
29 | 36 |
|
30 | 37 | # emmeans methods? Then tell user |
31 | 38 | if (p_adjust %in% emmeans_options) { |
32 | 39 | insight::format_error(paste0( |
33 | | - "`p_adjust = \"", p_adjust, "\"` is only available when `backend = \"emmeans\"." |
| 40 | + "`p_adjust = \"", |
| 41 | + p_adjust, |
| 42 | + "\"` is only available when `backend = \"emmeans\"." |
34 | 43 | )) |
35 | 44 | } |
36 | 45 |
|
|
96 | 105 | # columns. |
97 | 106 | coef_column <- intersect(c(.valid_coefficient_names(), "estimate"), colnames(params))[1] |
98 | 107 | if (is.na(coef_column)) { |
99 | | - insight::format_alert("Could not find coefficient column to apply `sup-t` adjustment.") |
| 108 | + insight::format_alert( |
| 109 | + "Could not find coefficient column to apply `sup-t` adjustment." |
| 110 | + ) |
100 | 111 | return(params) |
101 | 112 | } |
102 | 113 | se_column <- intersect(c("SE", "std.error"), colnames(params))[1] |
103 | 114 | if (is.na(se_column)) { |
104 | | - insight::format_alert("Could not extract standard errors to apply `sup-t` adjustment.") |
| 115 | + insight::format_alert( |
| 116 | + "Could not extract standard errors to apply `sup-t` adjustment." |
| 117 | + ) |
105 | 118 | return(params) |
106 | 119 | } |
107 | 120 | p_column <- intersect(c("p", "p.value"), colnames(params))[1] |
|
112 | 125 | ci_low_column <- intersect(c("CI_low", "conf.low"), colnames(params))[1] |
113 | 126 | ci_high_column <- intersect(c("CI_high", "conf.high"), colnames(params))[1] |
114 | 127 | if (is.na(ci_low_column) || is.na(ci_high_column)) { |
115 | | - insight::format_alert("Could not extract confidence intervals to apply `sup-t` adjustment.") |
| 128 | + insight::format_alert( |
| 129 | + "Could not extract confidence intervals to apply `sup-t` adjustment." |
| 130 | + ) |
116 | 131 | return(params) |
117 | 132 | } |
118 | 133 | df_column <- intersect(c("df", "df_error"), colnames(params))[1] |
|
122 | 137 | } |
123 | 138 | # calculate updated confidence interval level, based on simultaenous |
124 | 139 | # confidence intervals (https://onlinelibrary.wiley.com/doi/10.1002/jae.2656) |
125 | | - crit <- mvtnorm::qmvt(ci_level, df = params[[df_column]][1], tail = "both.tails", corr = vc)$quantile |
| 140 | + crit <- mvtnorm::qmvt( |
| 141 | + ci_level, |
| 142 | + df = params[[df_column]][1], |
| 143 | + tail = "both.tails", |
| 144 | + corr = vc |
| 145 | + )$quantile |
126 | 146 | # update confidence intervals |
127 | 147 | params[[ci_low_column]] <- params[[coef_column]] - crit * params[[se_column]] |
128 | 148 | params[[ci_high_column]] <- params[[coef_column]] + crit * params[[se_column]] |
129 | 149 | # update p-values |
130 | 150 | for (i in 1:nrow(params)) { |
131 | | - params[[p_column]][i] <- 1 - mvtnorm::pmvt( |
132 | | - lower = rep(-abs(stats::qt(params[[p_column]][i] / 2, df = params[[df_column]][i])), nrow(vc)), |
133 | | - upper = rep(abs(stats::qt(params[[p_column]][i] / 2, df = params[[df_column]][i])), nrow(vc)), |
134 | | - corr = vc, |
135 | | - df = params[[df_column]][i] |
136 | | - ) |
| 151 | + params[[p_column]][i] <- 1 - |
| 152 | + mvtnorm::pmvt( |
| 153 | + lower = rep( |
| 154 | + -abs(stats::qt(params[[p_column]][i] / 2, df = params[[df_column]][i])), |
| 155 | + nrow(vc) |
| 156 | + ), |
| 157 | + upper = rep( |
| 158 | + abs(stats::qt(params[[p_column]][i] / 2, df = params[[df_column]][i])), |
| 159 | + nrow(vc) |
| 160 | + ), |
| 161 | + corr = vc, |
| 162 | + df = params[[df_column]][i] |
| 163 | + ) |
137 | 164 | } |
138 | 165 | # clean up - remove temporary column |
139 | 166 | params[[".sup_df"]] <- NULL |
|
145 | 172 | .p_adjust_esarey <- function(x) { |
146 | 173 | # only for slopes |
147 | 174 | if (!inherits(x, c("estimate_slopes", "marginaleffects_slopes"))) { |
148 | | - insight::format_error("The `esarey` p-value adjustment is only available for Johnson-Neyman intervals, i.e. when calling `estimate_slopes()` with an interaction term of two numeric predictors.") # nolint |
| 175 | + insight::format_error( |
| 176 | + "The `esarey` p-value adjustment is only available for Johnson-Neyman intervals, i.e. when calling `estimate_slopes()` with an interaction term of two numeric predictors." |
| 177 | + ) # nolint |
149 | 178 | } |
150 | 179 | # get names of interaction terms |
151 | 180 | pred <- attributes(x)$trend |
152 | 181 | mod <- attributes(x)$by |
153 | 182 |
|
154 | 183 | # check for valid values - all must be numeric |
155 | 184 | if (!all(vapply(attributes(x)$datagrid[c(pred, mod)], is.numeric, logical(1)))) { |
156 | | - insight::format_error("The `esarey` p-value adjustment is only available for Johnson-Neyman intervals, i.e. when calling `estimate_slopes()` with an interaction term of two numeric predictors.") # nolint |
| 185 | + insight::format_error( |
| 186 | + "The `esarey` p-value adjustment is only available for Johnson-Neyman intervals, i.e. when calling `estimate_slopes()` with an interaction term of two numeric predictors." |
| 187 | + ) # nolint |
157 | 188 | } |
158 | 189 |
|
159 | 190 | int <- paste0(pred, ":", mod) |
|
186 | 217 | # produces a sequence of marginal effects |
187 | 218 | marginal_effects <- beta_pred + beta_int * range_sequence |
188 | 219 | # SEs of those marginal effects |
189 | | - me_ses <- sqrt(vcov_pred + (range_sequence^2) * vcov_int + 2 * range_sequence * vcov_pred_int) |
| 220 | + me_ses <- sqrt( |
| 221 | + vcov_pred + (range_sequence^2) * vcov_int + 2 * range_sequence * vcov_pred_int |
| 222 | + ) |
190 | 223 |
|
191 | 224 | # t-values across range of marginal effects |
192 | 225 | statistic <- marginal_effects / me_ses |
193 | 226 | # degrees of freedom |
194 | 227 | dof <- insight::get_df(model, type = "wald") |
195 | 228 | # Get the minimum p values used in the adjustment |
196 | | - pvalues <- 2 * pmin(stats::pt(statistic, df = dof), (1 - stats::pt(statistic, df = dof))) |
| 229 | + pvalues <- 2 * |
| 230 | + pmin(stats::pt(statistic, df = dof), (1 - stats::pt(statistic, df = dof))) |
197 | 231 | # Multipliers |
198 | 232 | multipliers <- seq_along(marginal_effects) / length(marginal_effects) |
199 | 233 | # Order the pvals |
|
0 commit comments