はじめに

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

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

準備

Rのインストールについては,次のページを見てください.

ここでは,標準で使用できるirisデータセットを使います.

data(iris)

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

このデータセットには,setosa, versicolor, virginicaという3種類のアヤメについて,それぞれ50個ずつ,合計150個のデータが含まれています. ランダムに10個のデータを選択して,見てみましょう.

iris[sort(sample(1:150,10)),]

ここでは,setosaのSepal.Lengthだけを扱います.

Speciesの値がsetosaのデータだけを取り出すには,次のようにします.

> 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

コンマを忘れないようにしてください.

Speciesの値がsetosaのデータのSepal.Lengthの値(つまり,1列目の値)だけを取り出して,setosa.Petal.Lengthとします.

install.packages("ggplot2")
library(ggplot2)

ヒストグラム

ヒストグラムを表示するにはhist関数を使います.

plot(x=iris$Petal.Length, y=iris$Petal.Width,
     xlab="Petal.Length", ylab="Petal.Width")

#ref(): File not found: "histgram_1.png" at page "バイオ・データ・マイニング/Rで統計分析する"

区間は適当に決めてくれますが,区間を変更したり,色を付けたりすることもできます.

ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  theme(aspect.ratio=1)

#ref(): File not found: "histgram_2.png" at page "バイオ・データ・マイニング/Rで統計分析する"

頻度の替わりに確率密度を縦軸にすることもできます.

ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  geom_smooth(method="lm")+
  theme(aspect.ratio=1)

#ref(): File not found: "histgram_3.png" at page "バイオ・データ・マイニング/Rで統計分析する"

平均

平均は,値の合計を値の数で割ったものです. \[\mu(X) = \frac{\sum_{i=1}^{n} x_i}{n}\]

平均を求めるには,mean関数を使います.

ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width, color=Species, shape=Species))+
  geom_point()+
  theme(aspect.ratio=1)

平均値からのズレ [math]x_i - \mu(x)[/math] を偏差といいます.

分散

不偏分散は,標本(サンプル・データ)に含まれる値の偏差の平方和を標本数から1を引いた値で割ったものです. なぜ標本数から1を引くのかについては,統計の教科書で勉強してください. \[\mathrm{Var}(X) = \frac{\sum_{i=1}^{n} (x_i - \mu(X))^2}{n-1}\]

不偏分散を求めるには,var関数を使います.

versicolor <- iris[51:100,]
hist(versicolor$Petal.Length, xlab="Petal.Length", main="")

標準偏差

不偏標準偏差は,不偏分散の平方根です. \[\sigma(X) = \sqrt{Var(X)}\]

不偏標準偏差を求めるには,sd関数を使います.

versicolor <- iris[51:100,]
ggplot(data=versicolor, aes(x=Petal.Length))+
  geom_histogram()+
  theme(aspect.ratio=1)

正規分布の確率密度関数

正規分布の確率密度関数は以下の式で与えられます. \[ f(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-\frac{(x-\mu)^2}{2\sigma^2}} \]

Rで正規分布の確率密度関数を求めるにはdnorm関数を用います. パラメーターは平均 mean と標準偏差 sd です.

まずは,平均0,標準偏差1の正規を描いてみましょう.

グラフを描くにはcurve関数を使います.第一引数には変数xを含んだ式を与えます.

ggplot(data=iris, aes(x=Petal.Length, fill=Species))+
  geom_histogram(position="identity", alpha=0.8)+
  theme(aspect.ratio=1)

#ref(): File not found: "density.png" at page "バイオ・データ・マイニング/Rで統計分析する"

ヒストグラムと確率密度関数の重ね書き

最後に,ヒストグラムとデータから推定した正規分布の確率密度関数を重ね書きしてみましょう.

まずは,ヒストグラムを確率密度で表示しておきます.

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(): File not found: "histgram_3.png" at page "バイオ・データ・マイニング/Rで統計分析する"

平均muと標準偏差sigmaを求めておきます.

ggplot(data=iris, aes(x=Species, y=Petal.Length))+
  geom_boxplot()+
  theme(aspect.ratio=1)

平均muと標準偏差sigmaの確率密関数をヒストグラムに重ね書きします.

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(): File not found: "histgram_4.png" at page "バイオ・データ・マイニング/Rで統計分析する"

演習

irisのSepal.Length以外のデータに対して,平均 [math]\mu[/math] と不偏標準偏差 [math]\sigma[/math] を求め,そのヒストグラムと平均 [math]\mu[/math],標準偏差 [math]\sigma[/math] の正規分布の確率密度関数を重ねて描いてみよう.

参考文献

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS