SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
データサイエンス勉強会
重回帰分析の注意点(多重共線性)
So Mizuno
2018/12/17
自己紹介
PC9821でBasicのゲーム雑誌にはまって以来、その後は、C言語、Fortran、
VB、Javaなどをさわっています。
リクルートGrなどで人材サービス、人事・採用を17年経験したのち、現在は
「HRにもっと科学を」をテーマに、採用施策、キャリア支援といった人事関連
のコンサルティングや、医療・心理研究に分析担当として参加しています。
分析は、主にRとPythonを使用しています。
2/46
注意事項
特別な説明がない限り多重線形回帰分析を略して重回帰分析と言います。
Rや統計用語の説明は、最低限に留めています。
時間の都合上、統計・数学知識についても深追いしません。
配布資料は、自己学習の範囲でご使用ください。
分析プロセス、コーディングルール、欠損値の取り扱いなどには触れていま
せん。
·
·
·
·
·
3/46
アジェンダ
前半
前回のおさらい
R/RStudioについて
重回帰分析の手順
過適合
多重共線性
対応など
·
·
·
·
·
·
·
4/46
アジェンダ
休憩&準備・質問
後半
演習&ディスカッション
良いモデルとは
·
·
·
·
5/46
前回のおさらい
データサイエンス勉強会#1の後半パートで、多重共線性(multicollinearity)につ
いて興味を持つ方が多かった。また、ディスカッションを深める中で、変数選
択などモデリングの流れについてもニーズがあった。
そこで、Extended versionとしてこの勉強会を開催しました。
時間を最大限有効に活用するため、数式の説明は最低限にして、実際のデータ
で現象を見てもらいます。まずは、感覚的に理解をしてもらい、各自の今後の
深掘りに役立てることをテーマに進めます。
重回帰係数を求めながら、注意点や問題点についての学ぶきっかけにしたいと
思っています。複雑な計算は、R言語の力を借りて演習します。
6/46
参考書籍
今回は、この2冊を参考にしています。
本日の勉強会の参考書籍·
7/46
参考書籍
学習にはとても恵まれた環境が到来。
統計・R関係
AIなどへの注目が高まり、手法、プログラミング、応用事例などテーマ別に
多数の書籍が出版されています。
·
8/46
R/RStudioについて
画面説明·
9/46
Rの基礎
データや、計算結果を"オブジェクト"に格納し、目的に沿った関数を使って分
析する。
「HRDataset_v9.csv」というCSVファイルのデータを読み込み、dataオブジェ
クトに格納した。
もっともスタンダードな操作·
指定
方
10/46
dataオブジェクトに格納したデータ構造を以下に示します。
転置しているように見えますが、列(変数)、行(データ)のリスト構造にな
っています。今回は参考のためdataオブジェクトの5列目から10列目までを表
示しています。
読 込 格納
11/46
重回帰分析の手順
変数の選択、外れ値の処理など手順3から6を繰り返す。
1. 仮説を立てる
2. データのチェック
3. 回帰係数を求める
4. モデルの検証
5. 変数選択を行う
6. モデルの評価
7. 予測
12/46
演習データ
attitude*1は、無作為に抽出した30部門に所属している35人の事務員の部署に
対するアンケート回答から得られたデータである。各変数の数値は、各質問項
目に対する好意的な回答の割合(%)を表している。
attitude
rating complaints privileges learning raises critical advance
43 51 30 39 61 92 45
63 64 51 54 63 73 47
71 70 68 69 76 86 48
各変数の意味は、rating:総合評価、complaints:従業員の苦情処理、privileges:特権を許可しない、learning:学習の機会、raises:
能力に基づいた昇給、critical:荷重advance:昇進、となる。
13/46
データの相関係数
目的変数との相関係数、独立変数どうしの相関係数に注目する。
14/46
回帰係数を求める
最小二乗法により、説明(独立)変数の回帰係数を求める。
この式の( )を求めることで、説明変数の値から目的変数を予測することができ
る(ex.店舗の売り上げ、マンションの価格予想、人事評価…など)
回帰係数( )の値は、それぞれの説明変数がどれくらい独立して目的変数に関係
しているかを表すが、そのまま独立変数どうしの大小を比較できないことに注
意。
…まずは、関数ではなくこの式を実装し、回帰係数を求めてみる。
β = ( X YX
T
)
−1
X
T
β
β
15/46
行列表記のイメージ
データ列をそれぞれベクトルとして扱う。 16/46
先ほどの式をRで実装して回帰係数を求めてみる。
コード中の、solve(t(x) %*% x) %*% t(x) %*% yが、 に
相当する箇所。
さっそく、演習データ(attitude)について、総合評価(rating)を目的変数、以外の
変数を説明変数として、作成した関数(reg)から回帰係数を求めてみる。
β = ( X YX
T
)
−1
X
T
17/46
関数(reg)の計算結果
式を実装することで、係数を求めることができた。
実際には、回帰分析の関数”lm( )”が用意されており、通常はこちらを用いる。
係数や変数についての考察は後述します。
18/46
回帰係数を求める2
lm( )関数の構文は以下の通り。
lm(model, data = data)
回帰分析の関数。()内にモデルとデータを指定する。
rating~.,
モデルの指定(ratingを目的変数に指定、他の変数全てで説明(~.で表す) 独立変
数を個別に指定する場合は、"変数名"を"+"でつなぐ。(rating~X1+X2+X3,
data = attitude)
19/46
summary(lm.attitude)で結果を出力
モデルの性能(適合度)を評価する指標が出力される。
20/46
モデルの改善と注意
先の例は、目的変数以外の全てのデータを説明変数とした。回帰分析では、性
能指標を頼りにしてモデルを改善することで、モデルの性能を上げられること
がある。
以降は、今日のメインであるモデルの改善に際して、基本的な注意すべき点に
ついて紹介します。
過剰適合
多重共線性
·
·
21/46
過剰適合
寄与率または決定係数( )は、モデルの精度を表す有用な指標だが、目的変数
と無関係な変数を投入しても値が上昇してしまう問題がある。
検証として、 、 の正規分布に従う乱数列を生成し、先ほど演習
データ(attitude)と統合してから、ランダムに値を抽出し、変数を増やしながら
重回帰分析を10回ずつ行って寄与率の変化を確認する。
X1 X2 X3 X4 X5
50.18746 31.46260 37.62406 45.83645 45.18634
48.15747 49.22054 45.43824 48.08518 52.02882
36.28669 59.68566 41.69677 50.69545 49.68260
R
2
μ = 50 = 10σ
2
22/46
ランダムに作成した変数(var1~var5)を、1つずつ追加して得られた寄与率は以
下のとおり。
23/46
自由度調整済み寄与率は、指標にペナルティを与えて調整している。今回のデ
ータは、データが少ないためか、若干値は上昇した。
24/46
まとめ
参考:
オッカムの剃刀(ケチの理論)
必要が無いなら多くのものを定立してはならない。少数の論理でよい場合は多
数の論理を定立してはならない。— オッカム
統計学や機械学習の分野では、モデルの複雑さとデータへの適合度とのバラン
スを取るために、オッカムの剃刀的な発想を利用する。Wikipedia[オッカムの
剃刀]
変数選択を慎重に行う。
一度モデルができても、仮説に基づき、よりシンプルなモデルにならないか
検証する。
·
25/46
変数選択の指標
AIC(赤池情報量基準)
すべての変数の組み合わせから、最適なモデルの選択が自動的にできる。以
下に「演習データ"attitude"」について、AICをもとに提案されたモデルの回
帰係数と適合指標を示す。
·
26/46
AIC情報量基準は、大変有用なモデル選択の手法だが、解析者は、仮説とモデル
に矛盾がないか責任をもって判断しなければならない。以降では、仮説との矛
盾について考察する。
次は、いよいよ本題に移ります。
27/46
多重共線性
独立変数間の相関係数が高いと、回帰係数の値が不安定になり正しい数値が得
られないことがある。
例えば、極端に大きな値や、符号(±)が仮説とは逆になり、解釈できなくなる現
象がおこる。
確認方法の一つとして、独立変数間の相関係数やVIFという値を目安にする。
(相関係数は、演習データのスライドで紹介)
VIFは、値が10以上であれば多重共線性の可能性あり。
28/46
検証
独立変数"learning"と"advance"に高い相関を示す変数"intraction"を加えたモデ
ルで重回帰分析を行う。
相関係数·
29/46
回帰係数に違和感はないだろうか?
例えば、部署の総合的な満足度について、"learning(学習の機
会)"や"advance(昇進)の満足度の割合が負となるという関係は、社会通念と
矛盾していないだろうか。
回帰係数
早速このデータで重回帰分析を行って、変数ごとの回帰係数を求めてみる。
·
30/46
多重共線性では、こうした矛盾(符号が入れ替わる)や、わずかなデータの変化で
も回帰係数が不安程になることが知られている。
VIF指標を確認してみる。
非常に高い値(VIF>10)を示しており、多重共線性がこのモデルの計算過程を一
つずつ追って数値を観察してみる。
31/46
転置行列 の計算イメージ
.
.
がm×n 行列のとき はn×m 行列となる。
( X)X
T
X X
T
32/46
計算過程の値をみる。
❶行列の積
(5行30列) × (30行5列)の積の解は、5行5列の行列
( X)X
T
X
T
X
33/46
逆行列について少しだけ説明
例えば、2行2列の行列A
の逆行列は、
となります。
この式の、 の値が、0や限りなく0に近い値となると計算がで
きなくなるということ。
A =
( )
a11
a21
a12
a22
=
( )
A
−1
1
−a11 a22 a12 a21
a22
−a21
−a12
a11
−a11 a22 a12 a21
34/46
試しに、
のとき、以下の値はいくつになるか確認してみよう。
すこしイメージできただろか。
今回の例は、計算しやすいように2次元の行列としたが、高次の行列でも行列式
が0であれば計算はできない。また、高次の場合は2次元の行列のような公式が
使えないため、計算がより複雑になる。
A =
( )
6
3
4
2
1
−a11 a22 a12 a21
35/46
❷逆行列を求める
非常に小さな値が出力された。
普通にみればほぼ0という値だが、そのまま計算されてしまう。
( X)X
T
−1
逆行列を求める計算途中の値も確認する(行列式の逆数の値)·
36/46
❸行列の積
5列30行の行列が出力される。
β = ( XX
T
)
−1
X
T
⋮ ⋮ ⋮ ⋮ ⋮ ⋮
37/46
と の積
最終的に、回帰係数の値が求められた。
値もRの”lm( )"関数で求めた値と一致している。
今回の検証データでは、❷逆行列の時点では非常に小さな値であった。また、
新たに追加したinteractionという変数は、他の変数と一次従属の関係があるこ
とで多重共線性が起こったと言える。
また、他のケースとしては、変数の数に対してサンプル数が少ない場合も回帰
係数が不安定になるため注意が必要。
( XX
T
)
−1
X
T
Y
38/46
吉田(1987)は、多重共線性について以下のように説明している。
現実のデータの場合に、正確に となることは、むしろ希だが、0に
近い、小さい値となることは起こりうる。
p個の説明変数のうち極めて相互に相関の高い変量が含まれていたり、あるいは
ある変量が他の変量群と因果関係にあり、そのため近似的に一次式で表現でき
たりするような場合である。
| X| = 0X
T
線形代数 転置行列 逆潮列 余因子 行列式 一次従属 一次独立
39/46
十分なデータ数を準備したうえでの対応としては、独立変数間で相関の高い変
数を除くほか、正則化することで抑えられる場合がある(詳しく解説しません
が、概要は以下を参考にしてください)
正規化では、最小二乗法にモデルの複雑さを表す式を加えます。この場合、複
雑なモデルであればあるほどペナルティが与えられます。またその正則化項
は、以下のようになり、q=1の時はラッソ回帰、q=2の時はリッジ回帰と言いま
す。(M:変数の数、w:重み付け(係数)、λ:正則化パラメータ)
変数の数Mを増やせば増やすほど、重みも増やせば増やすほど上記の式も大き
くなり、それがペナルティとして考慮されるのがわかると思います。
λ |
∑
j=1
M
wj |
q
40/46
モデルは以下の目的関数が最小になるように学習します。この式を最小にする
ためにはペナルティ項を小さくすることが必要になるので、モデルの複雑さを
抑えることにつながります。
引用元:
今回は、概要のみにとどめますがより詳しい情報は以下を参照ください。
東京大学GCIデータサイエンティスト育成講座
参考: RでL1 / L2正則化を実践する- 六本木で働くデータサイエンティストのブログ
( − f ( ) + λ |
∑
i=1
n
yi xi )
2
∑
j=1
M
wj |
q
41/46
その他
残差分析(残差診断図)
外れ値(予測値から大きく外れる値)を除くことで、モデルの当てはまりの良
さが、改善することがある。
42/46
後半の準備
これからの作業
Rのコードを配信します。ファイル内に、後半で使用する重回帰分析などのコ
ードが入っています。
グループ内で以下のことに取り組んでみよう!
(1)仮説を立てる
(2)仮説をもとに、最適な変数を選択してモデリング
(3)精度の確認( R^2、VIFなど)
最終的に、どんなモデルが良さそうか共有しましょう!
43/46
後半パート
URL: http://ur2.link/OyKj
Rコード:
* 基本統計量
summary(attitude)
※もしエラーなら、
install.packages("car") を一度だけ実行。
散布図
cor(attitude)
相関
pairs(attitude)
回帰分析(説明変数は、"+" でつないで指定する)
lm <- summary(lm(rating~ 変数名+ 変数名, data = attitude))
AIC
aic.lm <- step(lm)
VIC
library(car) ※carパッケージを使用
vif(lm)
·
·
·
·
·
44/46
より良いモデルに向けて
過剰適合や多重共線性に注意しつつ、シンプルなモデリングを心がけましょ
う。今後の分析の一助になれば幸いです。
ご静聴ありがとうございました。
enjoy!!
45/46
引用
スライド内に記載できなかったもの
[1] Chatterjee, S. and Price, B. (1977) Regression Analysis by Example. New York: Wiley. (Section 3.7, p.68ff of 2nd ed.(1991).)
46/46

Contenu connexe

En vedette

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

En vedette (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

データサイエンス勉強会20181217