I have a simple set of data (length of stay in hospitals) which is separated by "Low" and "Normal" groups.
The median is the statistic used to represent this data and I'd like to test and then plot (in text format) the result of a test of the difference in median between groups. I can do this with most base statistic tests (example below) using ggplot annotate, but can't find a way to do it with median_test (probably because it's part of the library(coin) package).
t.test in R and plotting as text in ggplot - e.g.:
test1 <- t.test(LengthOfStay ~ Group, data)
text = paste("T: ",test1$statistic ,"\n",
"p-value: ", test1$p.value
)
ggplot() +
annotate("text", x = 0.5, y = 0, size=4, label = text, hjust = 0.5) +
theme_void()
This will produce:
I would like to replicate the above image with median_test (coin package). However, this doesn't seem to work with ggplot and annotate because the results of the test are as an S4 class.
medtest <- median_test(LengthOfStay ~ Group, data)
text = paste("T: ",medtest$statistic ,"\n",
"p-value: ", medtest$p.value
)
Will produce 'Error in medtest$statistic : $ operator not defined for this S4 class'
Does anyone know how I could plot the results of the median_test in text format, please, like with the t.test above?
Thanks in advance.
You can access them with medtest@statistic@teststatistic
and pvalue(medtest)
:
library(coin)
#> Loading required package: survival
ex <- data.frame(
y = c(3, 4, 8, 9, 1, 2, 5, 6, 7),
x = factor(rep(c("no", "yes"), c(4, 5)))
)
mt1 <- median_test(y ~ x, data = ex, distribution = "exact")
paste("Z: ", mt1@statistic@teststatistic ,"\n",
"p-value: ", pvalue(mt1)
)
#> [1] "Z: 0.282842712474619 \n p-value: 1"