0

今日はかなりの数の質問をしました。

2 つのデータフレームの可変年齢の平均値の信頼区間(95 ではなく 99% レベル)を計算したいと思います。infert_controlinfert_patient

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

infert組み込みの R データセットです。なじみのない人のために説明すると、次のとおりです。ケース 0は対照群の患者、ケース 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

4

4 に答える 4

4

これにはブートストラップを使用できます。

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 
于 2014-01-19T18:33:52.180 に答える
3

非常に多くの答え...

無作為標本の平均値には t 分布がありますが、正規分布ではありませt -> Ndf -> 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

また、分位関数 (eqqt(...)およびqnorm(...)) はデフォルトで下側の裾を返すため、設定しない限り制限は逆になります。lower.tail=F

于 2014-01-19T18:45:57.417 に答える
2

信頼区間は手動で簡単に計算できます。

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も参照してください(ウィキペディアのリンクは申し訳ありませんが、適切な説明があり、式が示されています)

于 2014-01-19T18:19:11.263 に答える
1

...または小さな関数として:

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) 
于 2014-01-19T18:27:02.387 に答える