# Defining hypotheses in tribble
hypotheses <- tribble(
~pair, ~y_var, ~expected_band, ~expected_sign,
"NDI vs CNFDS (function)", "cnfds_d_change", "strong", "positive",
"NDI vs CNFDS (pain)", "cnfds_p_change", "moderate", "positive",
"NDI vs SF-36 Physical Functioning", "sf_pf_change", "strong", "positive", # set "negative" if preferred
"NDI vs SF-36 Bodily Pain", "sf_bp_change", "moderate", "positive", # set "negative" if preferred
"NDI vs SF-36 Mental Health", "sf_mh_change", "moderate", "positive", # set "negative" if preferred
"NDI vs SF-36 PCS", "pcs_change", "strong", "positive", # set "negative" if preferred
"NDI vs NRS (average pain)", "smagns_change", "moderate", "positive"
)
# Classification according to Cohens criteria
band_of <- function(r){
a <- abs(r)
dplyr::case_when(
a < 0.30 ~ "weak",
a >= 0.30 & a <= .50 ~ "moderate",
a > 0.50 ~ "strong"
)
}
# Looping all y_var from hypotheses and building a tibble
corr_res <- map_dfr(hypotheses$y_var, ~{
v <- .x
x <- resp_data$ndi10_change
y <- resp_data[[v]]
ok <- !(is.na(x) | is.na(y))
ct <- suppressWarnings(cor.test(x[ok], y[ok], method = "spearman", exact = FALSE))
tibble(y_var = v, n = sum(ok), rho = unname(ct$estimate), p_value = ct$p.value)
})
# Joining hypothesis and correlation results and checking if hypotheses are true or not
results <- hypotheses |>
left_join(corr_res, by = "y_var") |>
mutate(
observed_band = band_of(rho), # classify observed correlation
observed_sign = if_else(rho >= 0, "positive", "negative"),
band_match = observed_band == expected_band, # direct comparison only
sign_match = observed_sign == expected_sign,
hypothesis_met = band_match & sign_match
) |>
select(pair, y_var, n, rho, observed_sign, observed_band,
expected_sign, expected_band, band_match, sign_match, hypothesis_met)
# Generating a gt-table
results |>
mutate(
rho = round(rho, 2),
"Band match" = if_else(band_match, "✔", "✖"),
`Sign match` = if_else(sign_match, "✔", "✖"),
`Hypothesis met` = if_else(hypothesis_met, "✔", "✖"),
`Observed band` = observed_band,
`Observed sign` = observed_sign,
`Expected band` = expected_band,
`Expected sign` = expected_sign
) |>
select(
pair, n, rho,
`Observed sign`, `Observed band`,
`Expected sign`, `Expected band`,
`Band match`, `Sign match`, `Hypothesis met`
) |>
gt() |>
tab_header(title = "Table 2. Construct Responsiveness of NDI") |>
cols_label(
pair = "Instrument Pair",
n = "N",
rho = "Spearman ρ"
) |>
fmt_number(columns = c(rho), decimals = 2) |>
data_color(
columns = "Observed band",
fn = scales::col_factor(
palette = c(weak = "#f2f0f7", moderate = "#cbc9e2", strong = "#756bb1"),
domain = c("weak","moderate","strong"),
na.color = "white"
)
) |>
cols_align(
align = "center",
columns = c(n, rho, `Observed sign`, `Observed band`,
`Expected sign`, `Expected band`,
`Band match`, `Sign match`, `Hypothesis met`)
) |>
tab_options(
heading.align = "left",
table.font.size = "small",
table.width = px(650),
data_row.padding = px(2)
)