Rで統計分析する

| Topic path: Top / バイオ・データ・マイニング / Rで統計分析する

*はじめに [#w5ae99c9]

ここでは、Rを使って平均、分散、標準偏差を求めます。

『Rによるバイオインフォマティクスデータ解析』の3.3節「基本統計関数」に少しだけ出てきます。

#html{{
<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="https://rcm-fe.amazon-adsystem.com/e/cm?ref=tf_til&t=tohgorohmatsu-22&m=amazon&o=9&p=8&l=as1&IS2=1&detail=1&asins=4320057082&linkId=f6db5311dcfd9a82f5dee20859d39574&bc1=ffffff&lt1=_blank&fc1=444b4c&lc1=444b4c&bg1=ffffff&f=ifr"></iframe>
}}


*準備 [#q96ce9c2]

Rのインストールについては、次のページを見てください。
-[[MacでRを使う>機械学習/MacでRを使う]]
-[[WindowsでRを使う>機械学習/WindowsでRを使う]]

ここでは、標準で使用できる''irisデータセット''を使います。
#geshi(rsplus){{
data(iris)
}}

このデータセットは、アヤメの種類(Species)をがく片の長さ(Sepal.Length)、幅(Sepal.Width)、花びらの長さ(Petal.Length)、幅(Petal.Width)によって分類する問題です。
長さと幅は連続値、種類はsetosa, versicolor, virginicaのいずれかをとる離散値です。

このデータセットには、setosa, versicolor, virginicaという3種類のアヤメについて、それぞれ50個ずつ、合計150個のデータが含まれています。
ランダムに10個のデータを選択して、見てみましょう。
#geshi(rsplus){{
iris[sort(sample(1:150,10)),]
}}
#geshi(rsplus){{
> iris[sort(sample(1:150,10)),]
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
4            4.6         3.1          1.5         0.2     setosa
22           5.1         3.7          1.5         0.4     setosa
65           5.6         2.9          3.6         1.3 versicolor
97           5.7         2.9          4.2         1.3 versicolor
100          5.7         2.8          4.1         1.3 versicolor
108          7.3         2.9          6.3         1.8  virginica
116          6.4         3.2          5.3         2.3  virginica
122          5.6         2.8          4.9         2.0  virginica
136          7.7         3.0          6.1         2.3  virginica
146          6.7         3.0          5.2         2.3  virginica
}}

論文に使用するグラフはカッコイイ方がいいので、グラフ作成ライブラリーのggplot2でもグラフを作成します。
#geshi(rsplus){{
install.packages("ggplot2")
library(ggplot2)
}}




*記述統計 [#ea97832a]

標本を要約し、標本の情報をわかりやすく記述することを''記述統計''といいます。

ここでは、散布図、ヒストグラム、ボックスプロット(箱ひげ図)、棒グラフを作成します。


**散布図 [#xdbe2fbc]

横軸と縦軸にそれぞれ別の量をとり、測定値を点として表したグラフを散布図といいます。
2つの値を同時に測定し、[math]n[/math] 個の測定値の組を [math](x_1, x_2), (x_2, y_2),\dots, (x_n, y_n)[/math] としたとき、それぞれを [math]x[/math] 座標、[math]y[/math] 座標とする点として表したものです。

散布図を作成するには、''plot関数''を使います。
#geshi(rsplus){{
plot(x=iris$Petal.Length, y=iris$Petal.Width,
     xlab="Petal.Length", ylab="Petal.Width")
}}
#ref(scatter.png,nolink)


ggplot2で散布図を作成するには、''ggplot関数''と''geom_point関数''を使います。
#geshi(rsplus){{
ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_scatter_bw.png,nolink)

近似直線を追加するには、''geom_smooth関数''を加えます。
#geshi(rsplus){{
ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  geom_smooth(method="lm")+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_scatter_bw_lm.png,nolink)

種(Species)ごとに色と形を変えるには、''aes関数''の''color''と''shape''にカテゴリーを表す列を指定します。
#geshi(rsplus){{
ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width, color=Species, shape=Species))+
  geom_point()+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_scatter.png,nolink)





**ヒストグラム [#wa8f8e55]

測定値が存在する範囲をいくつかの区間に分け、各区間とその区間に属する測定値の個数との関係性を度数分布と言います。
度数分布を表すグラフをヒストグラムといい、底辺の長さが各区間の幅に比例し、その面積がその区間の度数に比例する長方形を近接して並べたものです。

ヒストグラムを作成するには、''hist関数''を使います。
#geshi(rsplus){{
versicolor <- iris[51:100,]
hist(versicolor$Petal.Length, xlab="Petal.Length", main="")
}}
#ref(histogram.png,nolink)


ggplot2でヒストグラムを作成するには、''ggplot関数''と''geom_histogram関数''を使います。
#geshi(rsplus){{
versicolor <- iris[51:100,]
ggplot(data=versicolor, aes(x=Petal.Length))+
  geom_histogram()+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_histogram_bw.png,nolink)

種(Species)ごとに色を変えるには、''aes関数''の''fill''にカテゴリーを表す列を指定し、''geom_histogram関数''の''position''に''identity''と指定します。(''alpha''には透明度を指定します。)
#geshi(rsplus){{
ggplot(data=iris, aes(x=Petal.Length, fill=Species))+
  geom_histogram(position="identity", alpha=0.8)+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_histogram.png,nolink)




**ボックスプロット(箱ひげ図) [#a5452015]

ヒストグラムの他に、測定値の分布やばらつき具合を表すグラフとして''ボックスプロット''(''箱ひげ図'')があります。
長方形の箱とその両端から伸びるひげで標本の統計量を表します。

箱の両端は、''第一四分位数''(最小値から全体の1/4のところにある測定値、25 パーセンタイル)と''第三四分位数''(最小値から全体の3/4のところにある測定値、75 パーセンタイル)を表し、箱の中の線は''中央値''(標本の大きさが奇数のときは全体の中央にある測定値、偶数のときは中央の2つの測定値の平均値、メディアン、50% パーセンタイル)を表します。

ひげの表し方には2種類あり、一つはひげの両端が最小値と最大値を表します。
もう一つは、ひげの両端が''箱の両端から第三四分位数と第一四分位数の差の1.5倍の範囲内での最小値と最大値''を表します。
後者の場合、ひげの両端よりも外側にある測定値を''特異値''(または''外れ値'')として丸印で表すこともあります。

#geshi(rsplus){{
setosa <- iris[1:50,]
versicolor <- iris[51:100,]
verginica <- iris[101:150,]

boxplot(setosa$Petal.Length, versicolor$Petal.Length, virginica$Petal.Length,
        names=c("Setosa", "Versicolor", "Virginica"), ylab="Petal.Length")
}}
#ref(boxplot.png,nolink)


ggplot2でボックスプロットを作成するには、''ggplot関数''と''geom_boxplot関数''を使います。
#geshi(rsplus){{
ggplot(data=iris, aes(x=Species, y=Petal.Length))+
  geom_boxplot()+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_boxplot_bw.png,nolink)

種(Species)ごとに色を変えるには、''aes関数''の''fill''にカテゴリーを表す列を指定し、データをプロットするには''geom_jitter関数''を使います。(''size''には点の大きさ、''alpha''には透明度を指定します。)
#geshi(rsplus){{
ggplot(data=iris, aes(x=Species, y=Petal.Length, fill=Species))+
  geom_boxplot()+
  geom_jitter(size=0.5, alpha=0.8)+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_boxplot.png,nolink)


**棒グラフ [#k3c837d7]

棒グラフを作成するには、''barplot関数''を使用します。
まず''aggregate関数''を使って平均を集計し、それを棒グラフにします。
#geshi(rsplus){{
aggr <- aggregate(iris$Petal.Length, by=list(iris$Species), FUN="mean")
barplot(aggr$x, names=aggr$Group.1)
}}
#ref(barplot.png,nolink)


ggplot2で棒グラフを作成するには、''ggplot関数''と''geom_bar関数''を使います。
ここでは、エラーバーを表示するために、''stat_summary関数''を加えます。
#geshi(rsplus){{
ggplot(data=iris, aes(x=Species, y=Petal.Length))+
  geom_bar(stat="summary", fun.y="mean")+
  stat_summary(fun.data="mean_se", geom="errorbar", width=0.5)+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_bar_bw.png,nolink)

ボックスプロットと同様に、種(Species)ごとに色を変えるには、''aes関数''の''fill''にカテゴリーを表す列を指定し、データをプロットするには''geom_jitter関数''を加えます。(''size''には点の大きさ、''alpha''には透明度を指定します。)
#geshi(rsplus){{
ggplot(data=iris, aes(x=Species, y=Petal.Length))+
  geom_bar(stat="summary", fun.y="mean")+
  stat_summary(fun.data="mean_se", geom="errorbar", width=0.5)+
  theme(aspect.ratio=1)
}}
#ref(ggplot2_bar.png,nolink)






*推定統計 [#r4e375a0]

標本から母集団の情報を推定することを推定統計といいます。

**平均 [#ee0baa35]

''母平均''は、母集団の値の合計を母集団の大きさ [math]N[/math] で割ったものです。
\[ \mu(X) = \frac{1}{N} \sum_{i=1}^N x_i \]

''標本平均''は、標本の値の合計を標本の大きさ [math]n[/math] で割ったものです。
\[\overline{X} = \frac{1}{n} \sum_{i=1}^{n} x_i \]

標本平均を求めるには、''mean関数''を使います。
#geshi(rsplus){{
setosa <- iris[1:50,]
versicolor <- iris[51:100,]
virginica <- iris[101:150,]

mean(setosa$Petal.Length)
mean(versicolor$Petal.Length)
mean(virginica$Petal.Length)
}}
#geshi(rsplus){{
> mean(setosa$Petal.Length)
[1] 1.462
> mean(versicolor$Petal.Length)
[1] 4.26
> mean(virginica$Petal.Length)
[1] 5.552
}}

標本平均の期待値は母平均に等しいため、標本平均を求めることで母平均を推定できます。



**分散 [#ded26716]

測定値の標本平均からのズレを''偏差''といいます。

''分散''は、値の偏差の二乗の総和を集団の大きさ [math]N[/math] で割ったものです。
\[\sigma(X)^2 = \frac{1}{N} \sum_{i=1}^{n} (x_i - \mu(X))^2 \]


''不偏分散''(標本不偏分散)は、標本に含まれる値の偏差の二乗の総和を標本数から1を引いた数で割ったものです。
\[ s(X)^2 = \frac{1}{n-1} \sum_{i=1}^{n} (x_i - \overline{X})^2 \]

不偏分散を求めるには、''var関数''を使います。
#geshi(rsplus){{
var(setosa$Petal.Length)
var(versicolor$Petal.Length)
var(virginica$Petal.Length)
}}
#geshi(rsplus){{
> var(setosa$Petal.Length)
[1] 0.03015918
> var(versicolor$Petal.Length)
[1] 0.2208163
> var(virginica$Petal.Length)
[1] 0.3045878
}}
不偏分散の期待値は母集団の分散に等しいため、不偏分散を求めることで母集団の分散を推定できます。




**標準偏差 [#t6948fa7]

''標準偏差''は、分散の平方根です。
\[ \sigma(X) = \sqrt{\sigma(X)^2} = \sqrt{\frac{1}{N} \sum_{i=1}^{n} (x_i - \mu(X))^2} \]

''不偏標準偏差''(標本不偏標準偏差)は、不偏分散の平方根です。
\[ s(X) = \sqrt{s(X)^2} = \sqrt{\frac{1}{n-1} \sum_{i=1}^{n} (x_i - \overline{X})} \]

不偏標準偏差を求めるには、''sd関数''を使います。
#geshi(rsplus){{
sd(setosa$Petal.Length)
sd(versicolor$Petal.Length)
sd(virginica$Petal.Length)
}}
#geshi(rsplus){{
> sd(setosa$Petal.Length)
[1] 0.173664
> sd(versicolor$Petal.Length)
[1] 0.469911
> sd(virginica$Petal.Length)
[1] 0.5518947
}}

不偏標準偏差の期待値は母集団の標準偏差に等しいため、不偏標準偏差を求めることで母集団の標準偏差を推定できます。


**不確かさ [#u9f3ffa9]

「平均 [math]\mu(X)[/math]、標準偏差 [math]\sigma(X)[/math] の母集団から無作為に抽出した大きさ [math]n[/math] の標本の平均 [math]\overline{X}[/math] は、平均 [math]\mu(X)[/math]、標準偏差 [math]\frac{\sigma(X)}{\sqrt{n}}[/math] の正規分布に従う」という''中心極限定理''より、[math]n[/math] 個の測定値を平均すると標準偏差が [math]\frac{1}{\sqrt{n}}[/math] になることがわかります。

不偏標準偏差 [math]s(X)[/math] を標本の大きさの平方根 [math]\sqrt{n}[/math] で割ったものを、''標準不確かさ''(''標準誤差'')といいます。
\[ u(X) = \frac{s(X)}{\sqrt{n}} \]

標準不確かさに''包含係数'' [math]k[/math] をかけたものを''拡張不確かさ''といいます。
\[ U(X) =  k \times u(X) \]

測定値が正規分布に従っていて、測定値が11個以上あるとき、測定値の95%が包含係数 [math]k=2[/math] の [math]\overline{X} \pm U(X)[/math] の範囲に含まれます。
この95%のことを''信頼水準''といいます。

1993年にISOを含む7つの国際機関が策定した「計測における不確かさの表現ガイド」に従うと、測定値は標準不確かさまたは拡張不確かさを用いて、次のように表します。
-[math]x = \overline{X}[/math]、標準不確かさは [math]u(X)[/math]
-[math]x = (\overline{X} \pm U(X))[/math]、ただし記号 [math]\pm[/math] に続く値は包含係数 [math]k=2[/math] から決定された拡張不確かさである

標準不確かさを求めるには、標本標準偏差を求める''sd関数''と平方根を求める''sqrt関数''を使います。
#geshi(rsplus){{
sd(setosa$Petal.Length) / sqrt(50)
sd(versicolor$Petal.Length) / sqrt(50)
sd(virginica$Petal.Length) / sqrt(50)
}}
#geshi(rsplus){{
> sd(setosa$Petal.Length) / sqrt(50)
[1] 0.0245598
> sd(versicolor$Petal.Length) / sqrt(50)
[1] 0.06645545
> sd(virginica$Petal.Length) / sqrt(50)
[1] 0.0780497
}}


これを用いて、SetosaのPetal.Lengthの測定値を「長さ [math]l = 1.462[/math] cm、標準不確かさは [math]0.025[/math] cm」または「長さ [math]l = (1.462 \pm 0.049)[/math] cm、ただし記号 [math]\pm[/math] に続く値は包含係数 [math]k=2[/math] から決定された拡張不確かさである」と表します。


**95%信頼区間 [#m62d681f]

計測における不確かさの表し方が分野によってバラバラなのは良くないので「計測における不確かさの表現ガイド」が作成されましたが、 95%信頼区間もまだよく使われています。

''95%信頼区間''は、母集団の平均が95%の確率で含まれると推定される区間のことです。

標本の平均と母集団の平均の偏差を標準不確かさで割ったものを''t値''(''t統計量'')といいます。
\[ t(X) = \frac{\overline{X} - \mu(X)}{u(X)} \]

t値は、自由度 [math]n - 1[/math] の''t分布''(自由度が大きいほど正規分布に近づく分布)に従うことがわかっています。
そこで、この分布において上側の面積の割合が2.5%になるt値 [math]t_{n-1,2.5\%}[/math] を求め、これに標準不確かさ [math]u(X)[/math] を掛けると95%信頼区間の幅の半分が求まります。
つまり、母平均の95%信頼区間は次のように表されます。
\[ \overline{X} - t_{n-1,2.5\%} \times u(X) \le \mu \le \overline{X} + t_{n-1,2.5\%} \times u(X) \]

計測の不確かさを表すのに95%信頼区間を用いるとき、測定値は次のように表します。
-[math]\overline{X}[/math](95%信頼区間 [math]\overline{X} - t_{n-1,2.5\%} \times u(X)[/math]–[math]\overline{X} + t_{n-1,2.5\%} \times u(X)[/math])

95%信頼区間を求めるには、''t.test関数''を使います。
#geshi(rsplus){{
t.test(setosa$Petal.Length)
}}
#geshi(rsplus){{
> t.test(setosa$Petal.Length)

	One Sample t-test

data:  setosa$Petal.Length
t = 59.528, df = 49, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 1.412645 1.511355
sample estimates:
mean of x 
    1.462 

}}
95%信頼区間 (''95 percent confidence interval'') のところを見ると、SetosaのPetal.Lengthの母平均の95%信頼区間は [math][1.412645, 1.511355][/math] です。

そこで、SetosaのPetal.Lengthの測定値を「長さは [math]1.462[/math] cm(95%信頼区間 [math]1.412[/math]–[math]1.511[/math] cm)だった」と表します。






*検定 [#s53a2538]

統計的検定では、「AとBの間には差がある」という仮説に対して、「AとBの間には差がない」という反対の仮説を立て、後者の仮説が成り立たないことを示すことによって、前者の仮説が成り立つことを示します。

否定される仮説を''帰無仮説''、帰無仮説を否定することを''棄却する''といいます。
帰無仮説が棄却されることによって支持される、帰無仮説の反対の仮説を''対立仮説''といいます。


**平均の差の検定(t検定) [#zf6ee43e]

Petal.Lengthについて、Versicolorの平均は4.906 cm、Virginicaの平均は5.552 cmです。

この二つの平均にの差には、意味があるのでしょうか。
(Virginicaの方がVersicolorよりも大きいと言えるでしょうか。)

これを、''t検定''という方法によって調べることができます。

''t検定''では、対立仮説を「二つの平均の差には意味がある」、帰無仮説を「二つの平均の差には意味がない」として、帰無仮説を棄却することによって対立仮説を支持します。

t検定を行うには、''t.test関数''を使います。
#geshi(rsplus){{
t.test(versicolor$Petal.Length, virginica$Petal.Length)
}}
#geshi(rsplus){{
> t.test(versicolor$Petal.Length, virginica$Petal.Length)

	Welch Two Sample t-test

data:  versicolor$Petal.Length and virginica$Petal.Length
t = -12.604, df = 95.57, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.49549 -1.08851
sample estimates:
mean of x mean of y 
    4.260     5.552 

}}
これは、''二つの群の分散が同じであることを仮定しない''、''Welchのt検定''といいます。

''二つの群の分散が同じであることを仮定する''検定は''Studentのt検定''といい、オプション ''var.equal=T'' を付けます。
#geshi(rsplus){{
t.test(versicolor$Petal.Length, virginica$Petal.Length, var.equal=T)
}}
#geshi(rsplus){{
> t.test(versicolor$Petal.Length, virginica$Petal.Length, var.equal=T)

	Two Sample t-test

data:  versicolor$Petal.Length and virginica$Petal.Length
t = -12.604, df = 98, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.495426 -1.088574
sample estimates:
mean of x mean of y 
    4.260     5.552 

}}

まず、''p値''(''p-value'')を見ます。

p値が、0.1よりも小さいときは「10%有意水準で統計的有意な差がある」、0.05よりも小さいときは「5%有意水準で統計的有意な差がある」、0.01よりも小さいときは「1%有意水準で統計的有意な差がある」といいます。

つぎに、平均の差の95%信頼区間(95 percent confidence interval)を見ます。

この区間がゼロを含むときは、5%有意水準で有意な差があるとはいえません。
「5%有意水準で有意な差がない」ではないことに注意しましょう。

ここで、p値が 1.612e-06 で、0.01よりも小さいため、「VersicolorのPetal.Lengthの平均とVirginicaのPetal.Lengthの平均には1%有意水準で統計的有意な差がある」といえます。

次に、Versicolorを半分ずつに分けて、そのPetal.Lengthの平均を調べてみましょう。
#geshi(rsplus){{
versicolor1 <- versicolor[1:25,]
versicolor2 <- versicolor[26:50,]

mean(versicolor1$Petal.Length)
mean(versicolor2$Petal.Length)
}}
#geshi(rsplus){{
> mean(versicolor1$Petal.Length)
[1] 4.312
> mean(versicolor2$Petal.Length)
[1] 4.208
}}
前半グループの平均は 4.312 cm、後半グループの平均は 4.208 cmで、その差が 0.104 cmありますが、これは意味のある差でしょうか?

t検定を行うと、次のようになります。
#geshi(rsplus){{
t.test(versicolor1$Petal.Length, versicolor2$Petal.Length, var.equal=T)
}}
#geshi(rsplus){{
> t.test(versicolor1$Petal.Length, versicolor2$Petal.Length, var.equal=T)

	Two Sample t-test

data:  versicolor1$Petal.Length and versicolor2$Petal.Length
t = 0.77934, df = 48, p-value = 0.4396
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.1643124  0.3723124
sample estimates:
mean of x mean of y 
    4.312     4.208 

}}
p値が 0.4396 あり、平均の差の95%信頼区間にも0が含まれています。
したがって、統計的に有意な差があるとはいえません。



**比率の差の検定 [#ibaf0da2]

VersicolorとVirginicaについて、Petal.Lengthの値がその平均よりも大きいものがいくつあるか、調べてみましょう。
#geshi(rsplus){{
sum(versicolor$Petal.Length > mean(versicolor$Petal.Length))
sum(virginica$Petal.Length > mean(virginica$Petal.Length))
}}
#geshi(rsplus){{
> sum(versicolor$Petal.Length > mean(versicolor$Petal.Length))
[1] 27
> sum(virginica$Petal.Length > mean(virginica$Petal.Length))
[1] 25
}}
Versicolorでは50個中27個(54%)、Virginicaでは50個中25個(50%)ありました。

この二つの比率の差には、意味があるのでしょうか。
(Versicolorの方がVirginicaよりも比率が高いと言えるでしょうか。)

これを、''2標本の比率検定''という方法によって調べることができます。

''2標本の比率検定''では、、対立仮説を「二つの比率の差には意味がある」、帰無仮説を「二つの比率の差には意味がない」として、帰無仮説を棄却することによって対立仮説を支持します。

2標本の比率検定を行うには、''prop.test関数''を使います。
#geshi(rsplus){{
> prop.test(c(27, 25), c(50, 50))

	2-sample test for equality of proportions with continuity correction

data:  c(27, 25) out of c(50, 50)
X-squared = 0.040064, df = 1, p-value = 0.8414
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.1756826  0.2556826
sample estimates:
prop 1 prop 2 
  0.54   0.50 

}}
一つ目の引数は条件を満たす測定値の数のベクトル、二つ目の引数は測定値全体の数のベクトルです。

結果の見方はt検定と同じです。

ここではp値が0.841と0.05よりも大きく、(比率の差の)95%信頼区間がゼロを含みますので、5%有意水準で有意な差があるとはいえません。



**独立性の検定 [#idb1aed6]

今度は、Setosaについて、Sepal.LengthとPetal.Lengthについて、それぞれの値が平均より大きいものがいくつあるか調べてみましょう。
#geshi(rsplus){{
sum(setosa$Petal.Length > mean(setosa$Petal.Length) &
      setosa$Sepal.Length > mean(setosa$Sepal.Length))
sum(setosa$Petal.Length > mean(setosa$Petal.Length) &
      setosa$Sepal.Length <= mean(setosa$Sepal.Length))
sum(setosa$Petal.Length <= mean(setosa$Petal.Length) &
      setosa$Sepal.Length > mean(setosa$Sepal.Length))
sum(setosa$Petal.Length <= mean(setosa$Petal.Length) &
      setosa$Sepal.Length <= mean(setosa$Sepal.Length))
}}
#geshi(rsplus){{
> sum(setosa$Petal.Length > mean(setosa$Petal.Length) &
+       setosa$Sepal.Length > mean(setosa$Sepal.Length))
[1] 15
> sum(setosa$Petal.Length > mean(setosa$Petal.Length) &
+       setosa$Sepal.Length <= mean(setosa$Sepal.Length))
[1] 11
> sum(setosa$Petal.Length <= mean(setosa$Petal.Length) &
+       setosa$Sepal.Length > mean(setosa$Sepal.Length))
[1] 7
> sum(setosa$Petal.Length <= mean(setosa$Petal.Length) &
+       setosa$Sepal.Length <= mean(setosa$Sepal.Length))
[1] 17
}}

これを表にまとめると、次の表のようになります。
このような表を''分割表''といいます。
|CENTER:~Setosa|CENTER:~Petal.Lengthが平均より大きい|CENTER:~Petal.Lengthが平均以下|CENTER:~計|
|CENTER:~Sepal.Lengthが平均より大きい|RIGHT:15|RIGHT:7|RIGHT:22|
|CENTER:~Sepal.Lengthが平均以下|RIGHT:11|RIGHT:17|RIGHT:28|
|CENTER:~計|RIGHT:26|RIGHT:24|RIGHT:50|

そこで、分割表を表す行列を作っておきます。
#geshi(rsplus){{
> m <- matrix(c(15, 11, 7, 17), nrow=2)
> m
     [,1] [,2]
[1,]   15    7
[2,]   11   17
}}

この二つの事象は、独立に生じているでしょうか。
(Sepal.Lengthが大きいこととPetal.Lengthが平均より大きいことは無関係でしょうか。)

これを''カイ二乗検定''または''Fisherの正確検定''という方法によって調べることができます。

''カイ二乗検定''と''Fisherの正確検定''では、対立仮説を「二つの事象は独立でない(関係がある)」、帰無仮説を「二つの事象は独立である(無関係である)」として、帰無仮説を棄却することによって対立仮説を支持します。

基本的には、カイ二乗検定を行いますが、分割表が次のようなときは、Fisherの正確検定を行います。
-度数が0のセルが存在する、または
-期待度数が5未満のセルが20%以上存在する

期待度数というのは、それぞれの事象が生じる確率から求められる度数です。
Petal.Lengthが平均より大きい確率は [math]26/50 = 0.52[/math]、Sepal.Lenghが平均より大きいものは22個ありますから、Petal.Lengthが平均より大きく、かつ、Sepal.Lengthが平均より大きいものの期待度数は [math]22 \times 0.52 = 11.44[/math]  となります。

上の分割表は、この条件に当てはまらないので、カイ二乗検定を用います。

カイ二乗検定を行うには、''chisq.test関数''を使います。
#geshi(rsplus){{
> chisq.test(m)

	Pearson's Chi-squared test with Yates' continuity correction

data:  m
X-squared = 3.045, df = 1, p-value = 0.08099

}}
分割表を表す行列を作って引数とします。

ここでは、p値が0.081と0.05より大きいので、5%有意水準で独立でない(関係がある)とは言えません。
(帰無仮説が棄却できなかったときは、対立仮説についてはなんとも言えません。「5%有意水準で独立である(関係がない)」とは言えないことに注意しましょう。)


Fisherの正確検定を行うには、''fisher.test関数''を使います。
#geshi(rsplus){{
> fisher.test(m)

	Fisher's Exact Test for Count Data

data:  m
p-value = 0.05176
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
  0.8902322 12.7546580
sample estimates:
odds ratio 
  3.229179 

}}

ここでも、p値が0.051と0.05より大きいので、5%有意水準で独立でない(関係がある)とは言えません。


*演習 [#t115f943]

Sepal.LengthとSepal.Widthの関係を表す散布図、VersicolorのSepal.Lengthの分布を表すヒストグラム、SetosaとVersicolorとVirginicaのSepal.Lengthの分布を表すボックスプロットを作成しよう。

Setosa, Versicolor, VirginicaのSepal.Lengthについて、それぞれの標本平均 [math]\overline{X}[/math] と標本標準偏差 [math]s(X)[/math] を求めてみよう。

また、VersicolorのSepal.Lengthの平均とVirginicaのSepal.Lengthの平均の間に統計的有意な差があるかどうかを調べてみよう。




*参考文献 [#zf4d0948]
-樋口千洋: ''Rによるバイオインフォマティクスデータ解析 第2版''-Bioconductorを用いたゲノムスケールのデータマイニング-, 共立出版 (2011)

#html{{
<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="https://rcm-fe.amazon-adsystem.com/e/cm?ref=tf_til&t=tohgorohmatsu-22&m=amazon&o=9&p=8&l=as1&IS2=1&detail=1&asins=4320057082&linkId=f6db5311dcfd9a82f5dee20859d39574&bc1=ffffff&lt1=_blank&fc1=444b4c&lc1=444b4c&bg1=ffffff&f=ifr"></iframe>
}}
トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS