Skip to content

Commit e28810f

Browse files
committed
add tests
1 parent 4e957f3 commit e28810f

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

R/format_p_adjust.R

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,22 @@ format_p_adjust <- function(method) {
106106
# tukey adjustment -----
107107

108108
.p_adjust_tukey <- function(params, stat_column, rank_adjust = 1, verbose = TRUE) {
109-
if ("df" %in% colnames(params) && length(stat_column) > 0) {
109+
df_column <- colnames(params)[stats::na.omit(match(c("df", "df_error"), colnames(params)))]
110+
if (length(df_column) && length(stat_column)) {
110111
params$p <- suppressWarnings(stats::ptukey(
111112
sqrt(2) * abs(params[[stat_column]]),
112-
nrow(params) / rank_adjust,
113-
params$df,
113+
nmeans = nrow(params) / rank_adjust,
114+
df = params[[df_column]],
114115
lower.tail = FALSE
115116
))
116117
# for specific contrasts, ptukey might fail, and the tukey-adjustement
117118
# could just be simple p-value calculation
118119
if (all(is.na(params$p))) {
119-
params$p <- 2 * stats::pt(abs(params[[stat_column]]), df = params$df, lower.tail = FALSE)
120+
params$p <- 2 * stats::pt(
121+
abs(params[[stat_column]]),
122+
df = params[[df_column]],
123+
lower.tail = FALSE
124+
)
120125
verbose <- FALSE
121126
}
122127
}
@@ -127,7 +132,8 @@ format_p_adjust <- function(method) {
127132
# scheffe adjustment -----
128133

129134
.p_adjust_scheffe <- function(model, params, stat_column, rank_adjust = 1) {
130-
if ("df" %in% colnames(params) && length(stat_column) > 0) {
135+
df_column <- colnames(params)[stats::na.omit(match(c("df", "df_error"), colnames(params)))]
136+
if (length(df_column) && length(stat_column)) {
131137
# 1st try
132138
scheffe_ranks <- try(qr(model@linfct)$rank, silent = TRUE)
133139

@@ -142,7 +148,7 @@ format_p_adjust <- function(method) {
142148
scheffe_ranks <- scheffe_ranks / rank_adjust
143149
params$p <- stats::pf(params[[stat_column]]^2 / scheffe_ranks,
144150
df1 = scheffe_ranks,
145-
df2 = params$df,
151+
df2 = params[[df_column]],
146152
lower.tail = FALSE
147153
)
148154
}
@@ -165,20 +171,25 @@ format_p_adjust <- function(method) {
165171
if (is.null(ci_level)) {
166172
ci_level <- 0.95
167173
}
174+
# find degrees of freedom column, if available
175+
df_column <- colnames(params)[stats::na.omit(match(c("df", "df_error"), colnames(params)))]
176+
if (length(df_column) == 0) {
177+
return(params)
178+
}
168179
# calculate updated confidence interval level, based on simultaenous
169180
# confidence intervals (https://onlinelibrary.wiley.com/doi/10.1002/jae.2656)
170-
crit <- mvtnorm::qmvt(ci_level, df = params$df[1], tail = "both.tails", corr = vc)$quantile
171-
ci_level <- 1 - 2 * stats::pt(-abs(crit), df = params$df[1])
181+
crit <- mvtnorm::qmvt(ci_level, df = params[[df_column]][1], tail = "both.tails", corr = vc)$quantile
182+
ci_level <- 1 - 2 * stats::pt(-abs(crit), df = params[[df_column]][1])
172183
# update confidence intervals
173184
params$CI_low <- params$Coefficient - crit * params$SE
174185
params$CI_high <- params$Coefficient + crit * params$SE
175186
# udpate p-values
176187
for (i in 1:nrow(params)) {
177188
params$p[i] <- 1 - mvtnorm::pmvt(
178-
lower = rep(-abs(stats::qt(params$p[i] / 2, df = params$df[i])), nrow(vc)),
179-
upper = rep(abs(stats::qt(params$p[i] / 2, df = params$df[i])), nrow(vc)),
189+
lower = rep(-abs(stats::qt(params$p[i] / 2, df = params[[df_column]][i])), nrow(vc)),
190+
upper = rep(abs(stats::qt(params$p[i] / 2, df = params[[df_column]][i])), nrow(vc)),
180191
corr = vc,
181-
df = params$df[i]
192+
df = params[[df_column]][i]
182193
)
183194
}
184195
params

tests/testthat/test-p_adjust.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ test_that("model_parameters, p-adjust", {
99
expect_equal(mp$p, c(0, 0.00912, 0.03318, 0.65851, 0.02137, 0.03318), tolerance = 1e-3)
1010
mp <- model_parameters(model, p_adjust = "bonferroni")
1111
expect_equal(mp$p, c(0, 0.01824, 0.16588, 1, 0.06411, 0.13869), tolerance = 1e-3)
12+
mp <- model_parameters(model, p_adjust = "scheffe")
13+
expect_equal(mp$p, c(0, 0.1425, 0.50499, 0.99981, 0.30911, 0.46396), tolerance = 1e-3)
14+
mp <- model_parameters(model, p_adjust = "tukey")
15+
expect_equal(mp$p, c(0, 0.03225, 0.21714, 0.99748, 0.09875, 0.18822), tolerance = 1e-3)
16+
mp <- model_parameters(model, p_adjust = "sidak")
17+
expect_equal(mp$p, c(0, 0.0181, 0.15483, 0.99841, 0.06242, 0.13092), tolerance = 1e-3)
1218
})
1319

1420

0 commit comments

Comments
 (0)