平均の差の検定

| コメント(0) | トラックバック(0)

母集団の平均が等しいと仮定できる場合

bigclass.csvを例にとり、男女間で身長の平均に差があるかどうかを検定しましょう。まず、データを読み込みます。

> class <- read.csv("bigclass.csv")

男子と女子のデータをclassから抽出し、それぞれclass_male, class_femaleに代入します。

> class_male <- subset(class,性別=="M")

> class_female <- subset(class,性別=="F")

class_maleの内容を表示してみます。

> class_male

名前 年齢 性別 身長 体重

6 TIM 12 M 60 84

7 JAMES 12 M 61 128

8 ROBERT 12 M 51 79

12 JOHN 13 M 65 98

13 JOE 13 M 63 105

14 MICHAEL 13 M 58 95

準備が出来たので、男女間の身長の差の検定を行いましょう。平均の差の検定にはt.testという関数を利用します。母集団の分散が等しいと仮定しているので、var.equal=Tをオプションとして追加します。また、ここでは両側検定を行いましょう。両側検定を行うにはalternative="two.sided"というオプションを追加します。

> t.test(x=class_male$身長,y=class_female$身長,var.equal=T, alternative="two.sided")

Two Sample t-test

data: class_male$身長 and class_female$身長

t = 2.3687, df = 38, p-value = 0.02304

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

0.4390238 5.6013802

sample estimates:

mean of x mean of y

63.90909 60.88889

p-valueに注目しましょう。0.02304なので、有意水準を5%とすると0.023<0.05より、帰無仮説を棄却し、対立仮説が正しいと判断します。つまり、「男女の母集団の平均身長には差がある」と判断されます。

次に、対立仮説を「男子の平均身長が女子よりも高い」とする片側検定を行いましょう。

今回はalternative="greater"というオプションを追加します。

> t.test(x=class_male$身長,y=class_female$身長,var.equal=T, alternative="greater")

Two Sample t-test

data: class_male$身長 and class_female$身長

t = 2.3687, df = 38, p-value = 0.01152

alternative hypothesis: true difference in means is greater than 0

95 percent confidence interval:

0.8705471 Inf

sample estimates:

mean of x mean of y

63.90909 60.88889

p-valueが0.0152なので、5%の有意水準で帰無仮説が棄却され、対立仮説が正しいと判断されます。つまり、「男子の平均身長が女子よりも高い」ということになります。

母集団の分散が等しいと仮定できない場合

peanuts.csv

は生の落花生と、炒った落花生をそれぞれ10匹のネズミに与えたときのタンパク質の摂取量です。生の落花生といった落花生でタンパク質の平均摂取量が異なるかどうか検定します。

> peanuts <- read.csv("peanuts.csv")

> peanuts

生 炒り

1 61 55

2 60 54

3 56 47

4 63 59

5 56 51

6 63 61

7 59 57

8 56 54

9 44 62

10 61 58

仮説検定にはt.testを利用しますが、今回はvar.equal=Fというオプションを追加します。

> t.test(x=peanuts$生,y=peanuts$炒り,var.equal=F, alternative="two.sided")

Welch Two Sample t-test

data: peanuts$生 and peanuts$炒り

t = 0.9185, df = 17.347, p-value = 0.371

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.716617 6.916617

sample estimates:

mean of x mean of y

57.9 55.8

p-valueが0.371なので、有意水準を5%とすると、0.371>0.05より、帰無仮説を棄却できません。つまり、生の落花生と、炒った落花生でも、ネズミが摂取するタンパク質の平均に差はないと判断されます。帰無仮説を「生の落花生の方がタンパク質摂取量の平均値が大きい」とする片側検定は次のようになります。

> t.test(x=peanuts$生,y=peanuts$炒り,var.equal=F, alternative="greater")

Welch Two Sample t-test

data: peanuts$生 and peanuts$炒り

t = 0.9185, df = 17.347, p-value = 0.1855

alternative hypothesis: true difference in means is greater than 0

95 percent confidence interval:

-1.872925 Inf

sample estimates:

mean of x mean of y

57.9 55.8

この結果から、有意水準を5%とするならば、片側検定と同じ結果になることが分かります。

2つの標本に対応がある場合

10人の被験者の体温を電子式体温計と水銀式体温計で測った結果が

temperature.csv

に保存されています。

> temp <- read.csv("temperature.csv")

> temp

電子式 水銀式

1 37.21 37.02

2 37.12 36.88

3 37.04 37.19

4 37.45 37.27

5 36.93 36.83

6 36.96 36.96

7 37.23 36.92

8 37.15 37.23

9 37.22 37.02

10 37.05 36.78

これは対応のあるデータなので、t.testにpaired=Tというオプションを付加します。

> t.test(x=temp$電子式,y=temp$水銀式, paired=T,alternative="two.sided")

Paired t-test

data: temp$電子式 and temp$水銀式

t = 2.5765, df = 9, p-value = 0.02987

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

0.01537252 0.23662748

sample estimates:

mean of the differences

0.126

p-valueから、5%の有意水準で帰無仮説「2つの種類の体温計で測った体温の平均値に差がない」が棄却され、「平均値に差がある」と判断されます。

片側検定も同様に行えます。

> t.test(x=temp$電子式,y=temp$水銀式, paired=T,alternative="greater")

Paired t-test

data: temp$電子式 and temp$水銀式

t = 2.5765, df = 9, p-value = 0.01493

alternative hypothesis: true difference in means is greater than 0

95 percent confidence interval:

0.0363543 Inf

sample estimates:

mean of the differences

0.126

練習問題

restaurant.csv

1. ある地域には2つのレストランがある。ある1年間の営業日の中から無作為に12日を選び、両レストランの売り上げを記録したデータが

である。このデータから両レストランの平均売上に差があるといえるか検定しなさい。

2. 東京電力と東芝の1959年1月から2001年12月までの月間株価収益率が、

stockreturn.csv

に保存されている。このデータをつかって、2つの銘柄の収益率の平均に差があるかどうかを有意水準5%で検定しなさい。


トラックバック(0)

トラックバックURL: http://www.moriyasu.org/cgi/mt/mt-tb.cgi/161

コメントする