Search code examples
rconfidence-interval

How to get one-sided 95% CI for each column in R?


I tried to get the lower one-sided 95% CI as below:

set.seed(123)
dd=data.frame(a = rnorm(100, mean = 24, sd = 5),
              b = rnorm(100, mean = 7, sd = 2),
              c = rnorm(100, mean = 59, sd = 10))

sapply(dd, t.test, alternative = "less")

But the estimate of the result only return the mean for each column. Besides, I could generate two-sided 95% CI for each column as below:

library(Rmisc)
sapply(dd, CI)

Is there a similar way to get upper or lower one-sided 95% CI for each column instead of two-sided?


Solution

  • t.test() will return a confidence interval for the mean appropriate to the specified alternative hypothesis. You can use $conf.int to extract it.

    sapply(dd, \(x) t.test(x, alternative = "less")$conf.int)
    #             a        b        c
    # [1,]     -Inf     -Inf     -Inf
    # [2,] 25.20985 7.106022 61.78182
    
    sapply(dd, \(x) t.test(x, alternative = "greater")$conf.int)
    #             a        b        c
    # [1,] 23.69421 6.463791 58.62748
    # [2,]      Inf      Inf      Inf