1414format_p_adjust <- function (method ) {
1515 method <- tolower(method )
1616
17- switch (method ,
17+ switch (
18+ method ,
1819 holm = " Holm (1979)" ,
1920 hochberg = " Hochberg (1988)" ,
2021 hommel = " Hommel (1988)" ,
@@ -44,7 +45,7 @@ format_p_adjust <- function(method) {
4445 # for interaction terms, e.g. for "by" argument in emmeans
4546 # pairwise comparison, we have to adjust the rank resp. the
4647 # number of estimates in a comparison family
47- rank_adjust <- .p_adjust_rank(model , params )
48+ rank_adjust <- .p_adjust_rank(model , params , tolower( p_adjust ) )
4849
4950 # only proceed if valid argument-value
5051 if (tolower(p_adjust ) %in% tolower(all_methods )) {
@@ -68,13 +69,19 @@ format_p_adjust <- function(method) {
6869 } else if (tolower(p_adjust ) == " sidak" ) {
6970 # sidak adjustment
7071 params $ p <- 1 - (1 - params $ p )^ (nrow(params ) / rank_adjust )
71- } else if (tolower(p_adjust ) == " sup-t" ) {
72+ } else if (tolower(p_adjust ) == " sup-t" ) {
7273 # sup-t adjustment
7374 params <- .p_adjust_supt(model , params )
7475 }
7576
76- if (isTRUE(all(old_p_vals == params $ p )) && ! identical(p_adjust , " none" ) && verbose ) {
77- insight :: format_warning(paste0(" Could not apply " , p_adjust , " -adjustment to p-values. Either something went wrong, or the non-adjusted p-values were already very large." )) # nolint
77+ if (
78+ isTRUE(all(old_p_vals == params $ p )) && ! identical(p_adjust , " none" ) && verbose
79+ ) {
80+ insight :: format_warning(paste0(
81+ " Could not apply " ,
82+ p_adjust ,
83+ " -adjustment to p-values. Either something went wrong, or the non-adjusted p-values were already very large."
84+ )) # nolint
7885 }
7986 } else if (verbose ) {
8087 insight :: format_alert(paste0(" `p_adjust` must be one of " , toString(all_methods )))
@@ -86,14 +93,31 @@ format_p_adjust <- function(method) {
8693
8794# calculate rank adjustment -----
8895
89- .p_adjust_rank <- function (model , params ) {
96+ .p_adjust_rank <- function (model , params , adjust = " tukey " ) {
9097 tryCatch(
9198 {
9299 correction <- 1
93100 by_vars <- model @ misc $ by.vars
94- if (! is.null(by_vars ) && by_vars %in% colnames(params )) {
95- correction <- insight :: n_unique(params [[by_vars ]])
101+ if (
102+ ! is.null(by_vars ) && length(by_vars ) > 0 && all(by_vars %in% colnames(params ))
103+ ) {
104+ if (length(by_vars ) == 1 ) {
105+ correction <- insight :: n_unique(params [[by_vars ]])
106+ } else {
107+ # compute unique combinations of multiple by-variables
108+ by_data <- params [by_vars ]
109+ by_groups <- interaction(by_data , drop = TRUE )
110+ correction <- insight :: n_unique(by_groups )
111+ }
112+ } else if (identical(adjust , " tukey" )) {
113+ # correction <- .safe(prod(vapply(model@model.info$xlev, length, numeric(1))))
114+ correction <- .safe(insight :: n_unique(unlist(strsplit(
115+ model @ levels $ contrast ,
116+ " - " ,
117+ fixed = TRUE
118+ ))))
96119 }
120+
97121 correction
98122 },
99123 error = function (e ) {
@@ -106,22 +130,26 @@ format_p_adjust <- function(method) {
106130# tukey adjustment -----
107131
108132.p_adjust_tukey <- function (params , stat_column , rank_adjust = 1 , verbose = TRUE ) {
109- df_column <- colnames(params )[stats :: na.omit(match(c(" df" , " df_error" ), colnames(params )))][1 ]
133+ df_column <- colnames(params )[stats :: na.omit(match(
134+ c(" df" , " df_error" ),
135+ colnames(params )
136+ ))][1 ]
110137 if (! is.na(df_column ) && length(stat_column )) {
111138 params $ p <- suppressWarnings(stats :: ptukey(
112139 sqrt(2 ) * abs(params [[stat_column ]]),
113- nmeans = nrow( params ) / rank_adjust ,
140+ nmeans = rank_adjust ,
114141 df = params [[df_column ]],
115142 lower.tail = FALSE
116143 ))
117144 # for specific contrasts, ptukey might fail, and the tukey-adjustement
118145 # could just be simple p-value calculation
119146 if (all(is.na(params $ p ))) {
120- params $ p <- 2 * stats :: pt(
121- abs(params [[stat_column ]]),
122- df = params [[df_column ]],
123- lower.tail = FALSE
124- )
147+ params $ p <- 2 *
148+ stats :: pt(
149+ abs(params [[stat_column ]]),
150+ df = params [[df_column ]],
151+ lower.tail = FALSE
152+ )
125153 verbose <- FALSE
126154 }
127155 }
@@ -132,7 +160,10 @@ format_p_adjust <- function(method) {
132160# scheffe adjustment -----
133161
134162.p_adjust_scheffe <- function (model , params , stat_column , rank_adjust = 1 ) {
135- df_column <- colnames(params )[stats :: na.omit(match(c(" df" , " df_error" ), colnames(params )))][1 ]
163+ df_column <- colnames(params )[stats :: na.omit(match(
164+ c(" df" , " df_error" ),
165+ colnames(params )
166+ ))][1 ]
136167 if (! is.na(df_column ) && length(stat_column )) {
137168 # 1st try
138169 scheffe_ranks <- try(qr(model @ linfct )$ rank , silent = TRUE )
@@ -146,7 +177,8 @@ format_p_adjust <- function(method) {
146177 scheffe_ranks <- nrow(params )
147178 }
148179 scheffe_ranks <- scheffe_ranks / rank_adjust
149- params $ p <- stats :: pf(params [[stat_column ]]^ 2 / scheffe_ranks ,
180+ params $ p <- stats :: pf(
181+ params [[stat_column ]]^ 2 / scheffe_ranks ,
150182 df1 = scheffe_ranks ,
151183 df2 = params [[df_column ]],
152184 lower.tail = FALSE
@@ -182,7 +214,9 @@ format_p_adjust <- function(method) {
182214 # get correlation matrix, based on the covariance matrix
183215 vc <- .safe(stats :: cov2cor(insight :: get_varcov(model , component = component )))
184216 if (is.null(vc )) {
185- insight :: format_warning(" Could not calculate covariance matrix for `sup-t` adjustment." )
217+ insight :: format_warning(
218+ " Could not calculate covariance matrix for `sup-t` adjustment."
219+ )
186220 return (params )
187221 }
188222 # get confidence interval level, or set default
@@ -197,18 +231,30 @@ format_p_adjust <- function(method) {
197231 }
198232 # calculate updated confidence interval level, based on simultaenous
199233 # confidence intervals (https://onlinelibrary.wiley.com/doi/10.1002/jae.2656)
200- crit <- mvtnorm :: qmvt(ci_level , df = params [[df_column ]][1 ], tail = " both.tails" , corr = vc )$ quantile
234+ crit <- mvtnorm :: qmvt(
235+ ci_level ,
236+ df = params [[df_column ]][1 ],
237+ tail = " both.tails" ,
238+ corr = vc
239+ )$ quantile
201240 # update confidence intervals
202241 params $ CI_low <- params $ Coefficient - crit * params $ SE
203242 params $ CI_high <- params $ Coefficient + crit * params $ SE
204243 # udpate p-values
205244 for (i in 1 : nrow(params )) {
206- params $ p [i ] <- 1 - mvtnorm :: pmvt(
207- lower = rep(- abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])), nrow(vc )),
208- upper = rep(abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])), nrow(vc )),
209- corr = vc ,
210- df = params [[df_column ]][i ]
211- )
245+ params $ p [i ] <- 1 -
246+ mvtnorm :: pmvt(
247+ lower = rep(
248+ - abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])),
249+ nrow(vc )
250+ ),
251+ upper = rep(
252+ abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])),
253+ nrow(vc )
254+ ),
255+ corr = vc ,
256+ df = params [[df_column ]][i ]
257+ )
212258 }
213259 params
214260}
0 commit comments