如何在R中为每列获取单侧95%置信区间?

huangapple go评论60阅读模式
英文:

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

问题

我尝试获取下侧的单边95%置信区间如下所示:

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")

但结果的估计仅返回每列的均值。
此外,我可以为每列生成双边95%置信区间如下所示:

library(Rmisc)
sapply(dd, CI)

是否有一种类似的方法可以获取每列的上侧或下侧单边95%置信区间,而不是双边的?

英文:

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?

答案1

得分: 2

t.test() 将返回与指定的备择假设相适应的均值的置信区间。您可以使用 $conf.int 提取它。

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
英文:

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

huangapple
  • 本文由 发表于 2023年7月18日 14:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定