統計学入門 I:一般基礎&相関係数
今回の実習課題:
• 課題:5問
► 実習課題に使ったプログラムファイルを全て提出してください
► 重要な出力画面とグラフを1つの word (.docx)にまとめて提出してください。
次の Julia パケージを使います:
► DataFrames 表データの取り扱い
► CSV テキストファイルをDataFrameに読み込む
► Plots グラフの作成
► Statistics 平均値、標本標準偏差、相関係数など
各自のプログラムの最初の所に下記のコードを挿入する。
using DataFrames
using CSV
using Plots
using Statistics
1. 量的データ
Quantitative Data
例:学生15人の身長
学生 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
身長 cm | 178 | 165 | 168 | 152 | 175 | 175 | 165 | 162 | 164 | 170 | 169 | 155 | 153 | 162 | 168 |
• 測定できる数値
• 他の例:時間、長さ、重さ、体積、面積、金額、温度、湿度など
2. 質的データ
Qualitative Data
学生 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
性別 | 男 | 男 | 男 | 女 | 男 | 男 | 女 | 女 | 男 | 男 | 男 | 女 | 女 | 女 | 男 |
• 測定数値ではない。
• あるカテゴリーに属している。
• 他の例:学歴
► 大学卒、高校卒、中学卒など
• 他の例:天気
► 晴、曇り、雨、雪など
3. 多次元データ
1次元データ
上記の例は一人の学生に対し、1つの値、または1つのカテゴリーだけ与える。
そのようなデータは
多次元データ
2次元以上のデータを多次元データと呼ぶ。
例
学生 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
身長 cm | 178 | 165 | 168 | 152 | 175 | 175 | 165 | 162 | 164 | 170 | 169 | 155 | 153 | 162 | 168 |
体重 kg | 63 | 62 | 69 | 41 | 71 | 61 | 62 | 48 | 52 | 55 | 69 | 48 | 44 | 49 | 69 |
性別 | 男 | 男 | 男 | 女 | 男 | 男 | 女 | 女 | 男 | 男 | 男 | 女 | 女 | 女 | 男 |
成績 | 51 | 74 | 87 | 66 | 90 | 74 | 73 | 81 | 81 | 66 | 87 | 72 | 65 | 70 | 82 |
• 一人の学生に対し、2つ以上の観測値が得られる。
4. 時系列データ
Time series data
例


5. 統計代表値: Mean, Median, Mode
•
平均値 mean
• 最もよく知られている代表値。
\[ \bar{x} = \frac{x_1 + x_2 + \cdots + x_N}{N}\]
中央値 Median
例: [1, 2, 2,
例: [1, 2, 2, 3, 4, 7] の中央値は 2.5 です。
最頻値 Mode
例: [1,
練習
Julia の Statistics パケージを使って、平均値、中央値を求める。
julia> using Statistics
julia> A = [1,1 ,2,3 ,4 ]
5 -element Array{Int64,1 }:
1
1
2
3
4
julia> mean(A)
2.2
julia> median(A)
2.0
*実習:平均値、中央値
15名の学生の身長と体重の平均値を中央値を求めるために、下記のコードを完成させる。
身長 = [178 , 165 , 168 , 152 , 175 ,
175 , 165 , 162 , 164 , 170 ,
169 , 155 , 153 , 162 , 168 ]
体重 = [63 , 62 , 69 , 41 , 71 ,
61 , 62 , 48 , 52 , 55 ,
69 , 48 , 44 , 49 , 69 ]
成績 = [51 , 74 , 87 , 66 , 90 ,
74 , 73 , 81 , 81 , 66 ,
87 , 72 , 65 , 70 , 82 ]
println("身長平均値 :" , mean(身長))
println("身長中央値 :" , ❓)
println(❓)
println(❓)
println(❓)
println(❓)
出力:
身長平均値 :165.4
身長中央値 :165.0
体重平均値 :57.53333333333333
体重中央値 :61.0
成績平均値 :74.6
成績中央値 :74.0
6. ばらつきの尺度:標準偏差など

範囲 Range
• 範囲 = 最大値 ー 最小値
平均偏差 mean diviation
\[ d = \frac{|x_1 - \bar{x}| + |x_2 - \bar{x}| ~+ \cdots ~+ |x_n - \bar{x}|}{N} \]
分散 variance (var)
\[ 分散~~\sigma^2 = \frac{(x_1 - \bar{x})^2 + \cdots + (x_N - \bar{x})^2}{N} = \frac{1}{N}\sum_{i=1}^N (x_i - \bar{x})^2\]
標準偏差 Standard deviation (std)
\[ 標準偏差~~\sigma = \sqrt{ \frac{(x_1 - \bar{x})^2 + \cdots + (x_N - \bar{x})^2}{N} } = \sqrt{\frac{1}{N}\sum_{i=1}^N (x_i - \bar{x})^2}\]
*実習:母集団標準偏差
その15人の学生の身長の平均値、標準偏差を求めたら、次のグラフを描いてください。

⋮
scatter(身長, markersize = 10 ,
xlabel = "Student No." ,
ylabel ="height (cm)" , legend = false)
MEAN = mean(身長)
STD = sqrt( sum( (身長 .- MEAN).^2 )/length(身長) )
plot!([0,15 ], [MEAN, MEAN], linewidth = 2 , linecolor = :black)
plot!([0,15 ], [MEAN + STD, MEAN + STD], linestyle = :dash, linecolor = :black, linewidth = 2 )
plot!([0,15 ], [MEAN - STD, MEAN - STD], linestyle = :dash, linecolor = :black, linewidth = 2 )
7. 標本標準偏差 Sample Standard Deviation
• Julia の Statistics Package に std という標準偏差の関数がある。
• それは「標本標準偏差」s のことです。

母集団から n 個の標本を抽出して、その標本平均は:
\[\bar{x} = \frac{1}{n}\sum_{i=1}^n x_i\]
その標本標準偏差は:
\[s = \sqrt{\frac{1}{n-1}\sum_{i=1}^n (x_i - \bar{x})^2}\]
• 母集団標準偏差の計算と違って、(n-1)で割る。
• それは bessel correction と呼ぶ。
• 目的:少ない標本を使って求めた「標本標準偏差 s」を「母集団標準偏差 σ」に近づける。
例:15名の学生から5個の標本を抽出する
N = 15 #母集団の数
println("母集団 Population" )
println("成績の母集団平均\t= " , mean(成績))
println("成績の母集団標準偏差\t= " , sqrt(sum(((成績 .- mean(成績)).^2 )/N)))
n = 5 #標本の数
ランダム = rand(1:15 , n) #1から15までの整数、n作る。
標本 = 成績[ランダム]
println("\n標本:\t" , ランダム)
println("成績の標本平均\t= " , mean(標本))
println("成績の標本標準偏差\t= " , std(標本))
println("補正しない標準偏差\t= " , sqrt(sum(((標本 .- mean(標本)).^2 )/n)))
結果:

8. ヒストグラム Histogram
• 縦軸に度数、横軸に階級をとった統計グラフ
例:15名の学生の成績ヒストグラム
histogram(成績,ylabel = "Frequency" , xlabel = "Grade" , legend = false)

• 山が1つの単純分布。それは 単峰性 unimodal という。
例:15名の学生の体重ヒストグラム
histogram(体重, bins = 40:10 :80 ,
legend=false,
ylabel = "Frequency" ,
xlabel = "Weight (kg)" )

histogram(体重, bins = 40:5 :80 ,
legend=false,
ylabel = "Frequency" ,
xlabel = "Weight (kg)" )

• 山が2つ。それは 双峰性 bimodal という。このような場合は、通常、性質の異なるデータが混在している。
9. DataFrameで分析
*実習:性質の異なるデータの解析
DataFrame を用いて15名の学生の成績、身長、体重を分析する。
身長 = [178 , 165 , 168 , 152 , 175 ,
175 , 165 , 162 , 164 , 170 ,
169 , 155 , 153 , 162 , 168 ]
体重 = [63 , 62 , 69 , 41 , 71 ,
61 , 62 , 48 , 52 , 55 ,
69 , 48 , 44 , 49 , 69 ]
性別 = ["男" , "男" , "男" , "女" , "男" ,
"男" , "女" , "女" , "男" , "男" ,
"男" , "女" , "女" , "女" , "男" ]
成績 = [51 , 74 , 87 , 66 , 90 ,
74 , 73 , 81 , 81 , 66 ,
87 , 72 , 65 , 70 , 82 ]
X = DataFrame(学生身長 = 身長, 学生体重 = 体重, 学生性別 = 性別, 学生成績 = 成績)
show(X)
出力:

下記のコードを追加して、男女ごとで色をつけて、散乱図を作成する。
P1 = scatter(X.学生身長, X.学生体重, legend = false,
xlabel = "height" , ylabel = "weight" )
P2 = scatter(X.学生身長[X.学生性別 .== "男" ], X.学生体重[X.学生性別 .== "男" ])
scatter!(X.学生身長[X.学生性別 .== "女" ], X.学生体重[X.学生性別 .== "女" ])
scatter!(legend = false,
xlabel = "height" , ylabel = "weight" )
plot(P1, P2, layout = (2,1 ), size = (400,300 ))

下記のコードを追加して、男女ごとで色をつけて、ヒストグラムを作成する。
histogram(X.学生体重[X.学生性別 .== "男" ],
bins = 40:5 :80 , label = "male" ,
xlabel = "Weigth (kg)" , ylabel = "Frequency" )
histogram!(X.学生体重[X.学生性別 .== "女" ],
bins = 40:5 :80 , label = "female" )

10. 相関係数 Correlation coefficient
2つの確率変数の間にある線形な関係の強弱を測る指標である
X と Y の共分散 (
\[ \mathrm{cov}(X,Y) = \frac{1}{n}\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})\]
X の標準偏差 (standard deviation)
\[ s_X = \sqrt{ \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})^2 }\]
Y の標準偏差 (standard deviation)
\[ s_Y = \sqrt{ \frac{1}{n} \sum_{i=1}^n (y_i - \bar{y})^2 }\]
X と Y の相関係数 (
\[ \mathrm{cor}(X,Y) = \frac{\mathrm{cov}(X,Y)}{s_X s_Y}\]

図の元: wikipedia
*実習:相関係数
関数
println("身長・身長の相関係数 = " , cor(X.学生身長, X.学生身長))
println("身長・体重の相関係数 = " , cor(X.学生身長, X.学生体重))
println("身長・成績の相関係数 = " , cor(X.学生身長, X.学生成績))
plot(
scatter(X.学生身長, X.学生身長,
title = "Correlation = 1.00" , ylabel = "Height" ),
scatter(X.学生身長, X.学生体重,
title = "Correlation = 0.80" , ylabel = "Weight" ),
scatter(X.学生身長, X.学生成績,
title = "Correlation = 0.13" , ylabel = "Grade" ),
layout = (3,1 ), legend = false,
titlefontsize = 10 , xlabel = "Height" ,
labelfontsize = 8 , size = (300,500 )
)

11. Iris data set
データファイル:iris.txt
• 有名な多変量データセット
• 150行 ✖️ 5列
• wikipedia にの記述もある。詳細は こちら
• ある花 Iris のデータです。
• 花の種類:
► Iris Setosa ヒオウギアヤメ
► Iris versicolor
► Iris virginica
• 同じ場所、同じ日に、同じ人が、同じ時間に同じ測定道具を用いて記録したデータです。

データの中身は下記の通り。数値の単位は cm です。
sepal_length,sepal_width,petal_length,petal_width,class
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
⋮
5.0,3.3,1.4,0.2,setosa
7.0,3.2,4.7,1.4,versicolor
6.4,3.2,4.5,1.5,versicolor
⋮
5.7,2.8,4.1,1.3,versicolor
6.3,3.3,6.0,2.5,virginica
5.8,2.7,5.1,1.9,virginica
⋮
5.9,3.0,5.1,1.8,virginica
*実習:Iris data set
Iris のデータファイルを作業フォルダーに保存してください。
► データファイル:iris.txt
下記のコードを理解して、Iris のデータを描いてください。
A = CSV.read("iris.txt" , DataFrame)
show(A)
IRIS = ["setosa" ,"versicolor" ,"virginica" ]
p1 = scatter(xlabel = "petal width" , ylabel = "petal length" ,
legend=false)
for x in IRIS
scatter!(A.petal_width[A.class .== x],
A.petal_length[A.class .== x])
end
p2 = scatter(xlabel = "sepal width" , ylabel = "sepal length" ,
legend=false)
for x in IRIS
scatter!(A.sepal_width[A.class .== x],
A.sepal_length[A.class .== x])
end
p3 = histogram(A.petal_width[A.class .== "setosa" ], bins=0:0 .1:3 ,
legend = false, xlabel = "petal width" )
histogram!(A.petal_width[A.class .== "versicolor" ], bins=0:0 .1:3 )
histogram!(A.petal_width[A.class .== "virginica" ], bins=0:0 .1:3 )
p4 = histogram(A.petal_length[A.class .== "setosa" ], bins=0:0 .2:8 ,
label = "setosa" , xlabel ="petal length" )
histogram!(A.petal_length[A.class .== "versicolor" ], bins=0:0 .2:8 ,
label = "versicolor" )
histogram!(A.petal_length[A.class .== "virginica" ], bins=0:0 .2:8 ,
label = "virginica" )
plot(p1, p2, p3, p4,layout=(2,2 ))
ターミナルの出力

グラフの出力

12. 簡単な多変量解析 Paired sample
*チャレンジ:Iris の変数のペアを一気に比較する
次のグラフを描いてみる。

A=CSV.read("iris.txt" , DataFrame)
ColName = ["sepal_length" , "sepal_width" , "petal_length" , "petal_width" ]
Species=["setosa" ,"versicolor" ,"virginica" ]
for j in 1:3
c = Species[j]
X = zeros(50 , 16 )
Y = zeros(50 , 16 )
XLABEL = [] #ラベル用の文字列を用意する
YLABEL = []
i = 0
for m in 1:4
for n in 1:4
i += 1
X[:,i] = A[A.class .== c, ColName[n]]
Y[:,i] = A[A.class .== c, ColName[m]]
push!(XLABEL, ColName[n]) #ラベル文字列に新しいラベルを追加
push!(YLABEL, ColName[m])
end
end
if j==1
scatter(X,Y, layout = (4,4 ),
markersize = 3 ,
legend = false,
labelfontsize = 8 ,
xlabel = reshape(XLABEL, 1 ,:),
ylabel = reshape(YLABEL, 1 ,:),
dpi = 300 , #解像度
size = (600,600 ) #グラフのサイズ
)
else
#グラフを追加する
scatter!(X,Y, layout = (4,4 ), markersize = 3 )
end
end