Search code examples
rdifferencemedianconfidence-interval

I need to report difference in median (or Median difference) and its 95% confidence interval


I have 2 unequal groups (pp and Control) and I reported median and 95% confidence interval for waiting.time variable for each group but now I want to report "Difference in median" and its 95% confidence interval. I found something similar but in sas (Link here) but I need to do it in R.

Here is a data sample

library(tableone)
tab<-print(a<-CreateTableOne(vars = "waiting.time",  strata="group", 
                             data=data,  addOverall = T, ), nonnormal = "waiting.time");tab

Here is what I am looking for: enter image description here

Any advice will be greatly appreciated


Solution

  • To bootstrap the difference in medians and then compute quantiles can be done as follows.

    1.

    First read in the data.

    data <- read.csv("~/Transferências/stackoverflow.data.csv")
    str(data)
    #'data.frame':  420 obs. of  3 variables:
    # $ X           : int  1 2 3 4 5 6 7 8 9 10 ...
    # $ group       : chr  "Control" "Control" "PP" "Control" ...
    # $ waiting.time: num  NA NA NA 23.9 NA ...
    

    The data has NA's and argument na.rm = TRUE will be used.

    2.

    Now bootstrap the statistic, function boot_diff_median.

    library(boot)
    
    boot_diff_median <- function(data, i){
      diff(tapply(data$waiting.time[i], data$group[i], FUN = median, na.rm = TRUE))
    }
    
    set.seed(2021)
    R <- 1e4
    
    b <- boot(data, statistic = boot_diff_median, R = R)
    

    3.

    Compute quantiles. I have also included the bootstrapped value, mean(b$t).

    mean(b$t)
    #[1] -0.4330915
    
    t(quantile(b$t, probs = c(0.025, 0.5, 0.975), na.rm = TRUE))
    #          2.5%  50%    97.5%
    #[1,] -11.35525 -0.5 8.620875