【问题标题】:Calculating Confidence Intervals for two datasets计算两个数据集的置信区间
【发布时间】:2014-02-08 19:44:27
【问题描述】:

我今天提出的问题相当多。

我想计算两个数据框 infert_control 和 @ 的 置信区间(99% 水平,而不是 95)变量年龄的平均值 987654322@哪里:

infert_control = subset(infert$age, infert$case == 0)
infert_patient = subset(infert$age, infert$case == 1) 

infert 是一个内置的 R 数据集,不熟悉的人,这里是:case 0表示对照组患者,case 1实际的。

> infert
    education age parity induced case spontaneous stratum pooled.stratum
1      0-5yrs  26      6       1    1           2       1              3
2      0-5yrs  42      1       1    1           0       2              1
3      0-5yrs  39      6       2    1           0       3              4
4      0-5yrs  34      4       2    1           0       4              2
5     6-11yrs  35      3       1    1           1       5             32
6     6-11yrs  36      4       2    1           1       6             36
7     6-11yrs  23      1       0    1           0       7              6
8     6-11yrs  32      2       0    1           0       8             22
9     6-11yrs  21      1       0    1           1       9              5
10    6-11yrs  28      2       0    1           0      10             19
11    6-11yrs  29      2       1    1           0      11             20
...
239   12+ yrs  38      6       0    0           2      74             63
240   12+ yrs  26      2       1    0           1      75             49
241   12+ yrs  31      1       1    0           0      76             45
242   12+ yrs  31      2       0    0           1      77             53
243   12+ yrs  25      1       0    0           1      78             41
244   12+ yrs  31      1       0    0           1      79             45
245   12+ yrs  34      1       0    0           0      80             47
246   12+ yrs  35      2       2    0           0      81             54
247   12+ yrs  29      1       0    0           1      82             43
248   12+ yrs  23      1       0    0           1      83             40

解决这个问题的正确方法是什么?

我已经计算了 age 列的平均值 infert_controlinfert_patient,加上每个子集的标准差。

【问题讨论】:

  • 这是一个统计问题。但是您可以报告正确的quantiles
  • 也许t.test可以帮助你?
  • @Fernando 因为t.test() 在本练习的第二部分中是必需的,所以我认为我不应该在这部分使用它,这是第一部分。这整件事是一个介绍 R 的任务。
  • 我明白了,也许您可​​以检查 ?t.test 以查看实现细节。
  • @Fernando 我不相信t.test() 与我的问题有关。

标签: r dataset statistics confidence-interval


【解决方案1】:

您可以为此使用引导程序:

library(boot)
set.seed(42)
boot_mean <- boot(infert_control, function(x, i) mean(x[i]), R=1e4)
quantile(boot_mean$t, probs=c(0.005, 0.995))
#      0.5%    99.5% 
#  30.47273 32.58182 

或者如果您不想使用库:

set.seed(42)
R <- 1e4
boot_mean <- colMeans(
                matrix(
                   sample(infert_control, R * length(infert_control), TRUE), 
                   ncol=R))
quantile(boot_mean, probs=c(0.005, 0.995))
#    0.5%    99.5% 
#30.42424 32.55152 

【讨论】:

  • 我很抱歉,但我不熟悉引导库。
  • 好吧,阅读文档或手动操作(参见我的编辑)。
【解决方案2】:

这么多答案...

随机样本的平均值呈 t 分布,不正常,虽然 t -&gt; Ndf -&gt; Inf 相同。

cl <- function(data,p) {
  n  <- length(data)
  cl <- qt(p/2,n-1,lower.tail=F)*sd(data)/sqrt(n)
  m  <- mean(data)
  return(c(lower=m-cl,upper=m+cl))
}
cl.control <- cl(infert_control,0.01)
cl.control
#    lower    upper 
# 30.42493 32.55689 

cl.patient <- cl(infert_patient,0.01)
cl.patient
#    lower    upper 
# 30.00221 33.05803

aggregate(age~case,data=infert,cl,p=0.01)  # much better way...
#   case age.lower age.upper
# 1    0  30.42493  32.55689
# 2    1  30.00221  33.05803

此外,分位数函数(例如 qt(...)qnorm(...))默认返回下尾,因此除非您设置 lower.tail=F,否则您的限制将被反转

【讨论】:

  • 非常感谢@jhoward。看起来这将有助于我完成练习的第二部分。
【解决方案3】:

您可以轻松地手动计算置信区间:

infert_control <- subset(infert$age, infert$case == 0)

# calculate needed values
m <- mean(infert_control)
s <- sd(infert_control)
n <- length(infert_control)

# calculate error for normal distribution (choose you distribution here, e.g. qt for t-distribution)
a <- 0.995 # 99% CI => 0.5% on both sides
error <- qnorm(a)*s/sqrt(n)

# calculate CI
ci_lower <- m-error
ci_upper <- m+error

另见http://en.wikipedia.org/wiki/Confidence_interval(抱歉,维基百科链接,但它有一个很好的解释,并向您展示了公式)

【讨论】:

  • 非常感谢您的帮助。让我研究一下你的答案。
【解决方案4】:

...或作为小功能:

cifun <- function(data, ALPHA){
  c(mean(data) - qnorm(1-ALPHA/2) * sd(data)/sqrt(length(data)),
    mean(data) + qnorm(1-ALPHA/2) * sd(data)/sqrt(length(data)))
}

cifun(infert_control, 0.01) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多