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)" ,
@@ -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 )))
@@ -93,7 +100,10 @@ format_p_adjust <- function(method) {
93100 by_vars <- model @ misc $ by.vars
94101 if (! is.null(by_vars ) && by_vars %in% colnames(params )) {
95102 correction <- insight :: n_unique(params [[by_vars ]])
103+ } else {
104+ correction <- prod(vapply(model @ model.info $ xlev , length , numeric (1 )))
96105 }
106+
97107 correction
98108 },
99109 error = function (e ) {
@@ -106,22 +116,26 @@ format_p_adjust <- function(method) {
106116# tukey adjustment -----
107117
108118.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 ]
119+ df_column <- colnames(params )[stats :: na.omit(match(
120+ c(" df" , " df_error" ),
121+ colnames(params )
122+ ))][1 ]
110123 if (! is.na(df_column ) && length(stat_column )) {
111124 params $ p <- suppressWarnings(stats :: ptukey(
112125 sqrt(2 ) * abs(params [[stat_column ]]),
113- nmeans = nrow( params ) / rank_adjust ,
126+ nmeans = rank_adjust ,
114127 df = params [[df_column ]],
115128 lower.tail = FALSE
116129 ))
117130 # for specific contrasts, ptukey might fail, and the tukey-adjustement
118131 # could just be simple p-value calculation
119132 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- )
133+ params $ p <- 2 *
134+ stats :: pt(
135+ abs(params [[stat_column ]]),
136+ df = params [[df_column ]],
137+ lower.tail = FALSE
138+ )
125139 verbose <- FALSE
126140 }
127141 }
@@ -132,7 +146,10 @@ format_p_adjust <- function(method) {
132146# scheffe adjustment -----
133147
134148.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 ]
149+ df_column <- colnames(params )[stats :: na.omit(match(
150+ c(" df" , " df_error" ),
151+ colnames(params )
152+ ))][1 ]
136153 if (! is.na(df_column ) && length(stat_column )) {
137154 # 1st try
138155 scheffe_ranks <- try(qr(model @ linfct )$ rank , silent = TRUE )
@@ -146,7 +163,8 @@ format_p_adjust <- function(method) {
146163 scheffe_ranks <- nrow(params )
147164 }
148165 scheffe_ranks <- scheffe_ranks / rank_adjust
149- params $ p <- stats :: pf(params [[stat_column ]]^ 2 / scheffe_ranks ,
166+ params $ p <- stats :: pf(
167+ params [[stat_column ]]^ 2 / scheffe_ranks ,
150168 df1 = scheffe_ranks ,
151169 df2 = params [[df_column ]],
152170 lower.tail = FALSE
@@ -182,7 +200,9 @@ format_p_adjust <- function(method) {
182200 # get correlation matrix, based on the covariance matrix
183201 vc <- .safe(stats :: cov2cor(insight :: get_varcov(model , component = component )))
184202 if (is.null(vc )) {
185- insight :: format_warning(" Could not calculate covariance matrix for `sup-t` adjustment." )
203+ insight :: format_warning(
204+ " Could not calculate covariance matrix for `sup-t` adjustment."
205+ )
186206 return (params )
187207 }
188208 # get confidence interval level, or set default
@@ -197,18 +217,30 @@ format_p_adjust <- function(method) {
197217 }
198218 # calculate updated confidence interval level, based on simultaenous
199219 # 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
220+ crit <- mvtnorm :: qmvt(
221+ ci_level ,
222+ df = params [[df_column ]][1 ],
223+ tail = " both.tails" ,
224+ corr = vc
225+ )$ quantile
201226 # update confidence intervals
202227 params $ CI_low <- params $ Coefficient - crit * params $ SE
203228 params $ CI_high <- params $ Coefficient + crit * params $ SE
204229 # udpate p-values
205230 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- )
231+ params $ p [i ] <- 1 -
232+ mvtnorm :: pmvt(
233+ lower = rep(
234+ - abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])),
235+ nrow(vc )
236+ ),
237+ upper = rep(
238+ abs(stats :: qt(params $ p [i ] / 2 , df = params [[df_column ]][i ])),
239+ nrow(vc )
240+ ),
241+ corr = vc ,
242+ df = params [[df_column ]][i ]
243+ )
212244 }
213245 params
214246}
0 commit comments