#----------------------------------------------------------------------------
# rdt() example
#----------------------------------------------------------------------------
library(rankdifferencetest)
# Use example data from Kornbrot (1990)
data <- kornbrot_table1
# Create long-format data for demonstration purposes
data_long <- reshape(
data = kornbrot_table1,
direction = "long",
varying = c("placebo", "drug"),
v.names = c("time"),
idvar = "subject",
times = c("placebo", "drug"),
timevar = "treatment",
new.row.names = seq_len(prod(length(c("placebo", "drug")), nrow(kornbrot_table1)))
)
# Subject and treatment must be factors. The ordering of the treatment factor
# will determine the difference (placebo - drug).
data_long$subject <- factor(data_long$subject)
data_long$treatment <- factor(data_long$treatment, levels = c("placebo", "drug"))
# Recreate analysis and results from section 7.1 in Kornbrot (1990)
## The p-value shown in Kornbrot (1990) was continuity corrected. rdt() does
## not apply a continuity correction, so the p-value here will be slightly
## lower. It does match the uncorrected p-value shown in footnote on page 246.
rdt(
data = data,
formula = placebo ~ drug,
alternative = "two.sided",
distribution = "asymptotic"
)$p.value/2
rdt(
data = data_long,
formula = time ~ treatment | subject,
alternative = "two.sided",
distribution = "asymptotic"
)$p.value/2
# The same outcome is seen after transforming time to rate.
## The rate transformation inverts the rank ordering.
data$placebo_rate <- 60 / data$placebo
data$drug_rate <- 60 / data$drug
data_long$rate <- 60 / data_long$time
rdt(
data = data,
formula = placebo_rate ~ drug_rate,
alternative = "two.sided",
distribution = "asymptotic"
)$p.value/2
rdt(
data = data_long,
formula = rate ~ treatment | subject,
alternative = "two.sided",
distribution = "asymptotic"
)$p.value/2
# In contrast to the rank difference test, the Wilcoxon signed-rank test
# produces differing results. See table 1 and table 2 in Kornbrot (1990).
wilcox.test(
x = data$placebo,
y = data$drug,
paired = TRUE,
exact = TRUE,
alternative = "two.sided"
)$p.value/2
wilcox.test(
x = data$placebo_rate,
y = data$drug_rate,
paired = TRUE,
exact = TRUE,
alternative = "two.sided"
)$p.value/2
Run the code above in your browser using DataLab