比率の差の検定 prop.test *機能 (1) 複数の母比率が等しいかどうか検定する。~ (2) 母比率がある値であるかどうか検定する。(一標本とは限らない) *使用法 prop.test(x, n, p = NULL, alternative = c("two.sided", "less", "greater"), conf.level = 0.95, correct = TRUE) *引数 x 注目している特性を持つものの数(成功数)を表すベクトルか, 特性を持っているもの持っていないものの数を表す2列の行列 n 各群の標本の大きさを表すベクトル(x が行列のときには無視される) p 母比率と等しいかどうか比べられる特定の値(成功比率) 一標本のときのデフォルトは 0.5 複数標本の場合に p を与えるときには, p の要素数は群の数と同じでなくてはならない(群毎に指定するということ) alternative 対立仮説の種類 "two.sided", "less", "greater" のいずれか 最初の1文字だけでもよい 両側検定なら "two.sided"(デフォルト) 片側検定なら "less" または "greater" 指定できるのは,一標本の場合と,二標本で p を指定しない場合のみ 三標本以上の場合はデフォルトの両側検定のみ conf.level 信頼率 デフォルトは 0.95 指定できるのは,一標本の場合と二標本の場合のみ correct イエーツの補正をしないときには FALSE を指定する デフォルトは TRUE *戻り値 **関数への入力 $ null.value : 引数で指定された p の値 $ alternarive: 対立仮説の種類 **関数からの出力 $ statistic : カイ二乗統計量 $ parameter : カイ二乗分布の自由度 $ p.value : P 値 $ estimate : 標本比率 $ conf.int : 一標本の場合,二標本で p が指定されない比率の差の検定 $ method : 検定の種別 $ data.name : データの記述 *例1 一標本の場合(母比率の検定) 成功確率 0.5 で,50 施行中 35 回成功した場合の検定。 > prop.test(35, 50, p=0.5) # 正規近似によるので,お勧めではない。常に binom.test を使うべし。 # p=0.5 はデフォルトなので指定しなくても同じ 1-sample proportions test with continuity correction data: 35 out of 50, null probability 0.5 X-squared = 7.22, df = 1, p-value = 0.00721 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.5521660 0.8171438 sample estimates: p 0.7 > binom.test(35, 50, p=0.5) # 同じことを binom.test でやった場合(こちらが正確) Exact binomial test data: 35 and 50 number of successes = 35, number of trials = 50, p-value = 0.0066 alternative hypothesis: true probability of success is not equal to 0.5 95 percent confidence interval: 0.5539177 0.8213822 sample estimates: probability of success 0.7 *例2 複数標本で母比率を指定したとき 成功数の期待値を p,失敗数の期待値を 1-p から求め,後は普通のカイ二乗検定と同じように検定を進めるものである。 > prop.test(c(35,30,32), c(50,60,65), p=c(0.5, 0.4, 0.6)) 3-sample test for given proportions without continuity correction data: c(35, 30, 32) out of c(50, 60, 65), null probabilities c(0.5, 0.4, 0.6) X-squared = 13.641, df = 3, p-value = 0.003437 alternative hypothesis: two.sided null values: prop 1 prop 2 prop 3 0.5 0.4 0.6 sample estimates: prop 1 prop 2 prop 3 0.7000000 0.5000000 0.4923077 *例3 二標本の場合(比率の差の検定) イエーツの補正がデフォルト,片側検定も行える,比率の差の信頼区間も求められる 以下のような2×2分割表にまとめられるデータの検定を行う 群A 群B 合計 成功 35 30 65 失敗 15 30 45 合計 50 60 110 > prop.test(c(35,30), c(50,60)) # 各群の成功数と標本サイズを入力するとき 2-sample test for equality of proportions with continuity correction data: c(35, 30) out of c(50, 60) X-squared = 3.7234, df = 1, p-value = 0.05366 alternative hypothesis: two.sided 95 percent confidence interval: 0.002389771 0.397610229 sample estimates: prop 1 prop 2 0.7 0.5 > prop.test(matrix(c(35, 30, 15, 30), 2,2)) # 成功数の列と失敗数の列からなる行列を入力するとき 2-sample test for equality of proportions with continuity correction data: matrix(c(35, 30, 15, 30), 2, 2) X-squared = 3.7234, df = 1, p-value = 0.05366 # 当然ながら上と同じ結果になる alternative hypothesis: two.sided 95 percent confidence interval: 0.002389771 0.397610229 sample estimates: prop 1 prop 2 0.7 0.5 > chisq.test(matrix(c(35,15,30,30),2,2)) # 同じデータを chisq.test で検定しても(普通は)同じ結果になる Pearson's Chi-squared test with Yates' continuity correction data: matrix(c(35, 15, 30, 30), 2, 2) X-squared = 3.7234, df = 1, p-value = 0.05366 *例4 複数の群の場合の比率の差の検定 > prop.test(c(35,30,32), c(50,60,65)) 3-sample test for equality of proportions without continuity correction # correct=FALSE を指定しても無視される data: c(35, 30, 32) out of c(50, 60, 65) X-squared = 6.0235, df = 2, p-value = 0.0492 alternative hypothesis: two.sided # 常に両側検定になる sample estimates: prop 1 prop 2 prop 3 0.7000000 0.5000000 0.4923077