baseball <- read.csv("baseball2016.csv",na.strings="*")
今日はパッケージpsychを使いますので,持ってな人はインストールしておいてね!
install.packages(psych)
因子分析は次のように説明されます。
「性格」「学力」「女子力」のように,あるいは「夏休みの楽しみ方」のような構成概念は目に見えません。しかし,もしそこにそういうものがあるのであれば,複数の項目に共通した反応が出てくるはず。たとえば,学力が高い人は,いろんな問題を出してもどんどん正解していくし,学力が低い人はいろんな問題にどんどん間違えていく,という傾向があるはずです。
こういう複数の項目,反応の背後に「なにか」があれば,そこは反応パターンに共通した傾向=相関関係が出てくるはずです。因子分析法は,相関関係から「背後にこういうまとまりがあるんじゃないか」というのを見つけ出してくる手法です。目に見えないものを見えるようにというとなんだか魔法のような,怪しげな術があるのかと思うかもしれませんが,今回のテーマはまさにそういうことなのです。
目に見えないもの=心とまでは言いませんが,ここで言わんとすることを専門的に言うと「潜在変数」とか「合成変数」ということになります。
例えば「身長」と「体重」と「年齢」を組み合わせた変数でプロ野球選手を表現してみたいと思います。プロ野球選手の特徴を際立たせるのが目的ですから(みんな同じになっても仕方がないので),この三つの変数に重みをつけて,なにかその特徴を表現する変数を作ることをイメージをしてみてください。
数学的にはこうなります。 \[ P = w_1 * height + w_2 * weight + w_3 * age\]
ここで,作られる変数\(P\)の分散を最大にするように,三つの変数\(w_1,w_2,w_3\)の重みを決めます。ここで\(P\)を主成分と呼び,この方法が主成分分析(PCA,Principle Component Analysis)と言われるものです。数学的に解を得るために\(\sum w_i^2 = 1\)という上限の制約をかけます。
こうして作られた合成変数\(P\)はプロ野球選手の特徴を表現したスコアです。これを「選手力」と呼べばほら,目に見えなかったものが見えるようになったでしょ?
今日のお話はそういう感じのものです。
今日のテーマは因子分析で,主成分分析ではありません。 この二つのどこが違うかというと,
の2点が挙げられます。
主成分分析は標準化してないスコアでも計算できます。因子分析は普通,変数間関係を標準化した相関行列から分析を始めます。
主成分分析は変数のスコアをそのまま使いますが,因子分析は知能テストや性格検査を背景に生まれてきたこともあって,得られた変数の数字には誤差が含まれているのではないかという過程をおきます。この辺の詳しいところは,心理教育測定法で説明しますね。
とりあえず因子分析をやってみましょう。psychパッケージを使いますので,まだの方はインストールしてください。
使う変数を一部に限定します。量的変数だけにしておきます。
# ここで使う変数は次の通りです。
fa.dat <- subset(baseball, select=c("year","age","height",
"weight","games","Hit","HR"))
因子分析を始める場合,まず説明する潜在変数=因子の数をいくつにするか,決めなければなりません。因子数の決め方は幾つかあります。
1-3は古典な方法,4-6はより客観的で現代的な方法です。おすすめは5と6です。
実際にやってみましょう。
# psychパッケージを使います
# install.packages("psych")
library(psych)
fa.parallel(fa.dat,SMC=TRUE)
## Parallel analysis suggests that the number of factors = 4 and the number of components = 3
VSS(fa.dat)
##
## Very Simple Structure
## Call: vss(x = x, n = n, rotate = rotate, diagonal = diagonal, fm = fm,
## n.obs = n.obs, plot = plot, title = title, use = use, cor = cor)
## VSS complexity 1 achieves a maximimum of 0.75 with 4 factors
## VSS complexity 2 achieves a maximimum of 0.89 with 5 factors
##
## The Velicer MAP achieves a minimum of NA with 1 factors
## BIC achieves a minimum of NA with 3 factors
## Sample Size adjusted BIC achieves a minimum of NA with 3 factors
##
## Statistics by number of factors
## vss1 vss2 map dof chisq prob sqresid fit RMSEA BIC SABIC
## 1 0.42 0.00 0.12 14 1.8e+02 5.2e-30 6.20 0.42 0.29 106.0 150
## 2 0.53 0.65 0.13 8 1.2e+02 2.4e-22 3.75 0.65 0.32 81.0 106
## 3 0.73 0.85 0.15 3 1.7e+01 6.8e-04 1.44 0.87 0.19 2.2 12
## 4 0.75 0.89 0.25 -1 2.7e-01 NA 0.80 0.93 NA NA NA
## 5 0.75 0.89 0.43 -4 1.0e-08 NA 0.72 0.93 NA NA NA
## 6 0.74 0.85 1.00 -6 0.0e+00 NA 0.70 0.93 NA NA NA
## 7 0.62 0.85 NA -7 0.0e+00 NA 0.13 0.99 NA NA NA
## complex eChisq SRMR eCRMS eBIC
## 1 1.0 2.4e+02 2.0e-01 0.244 169.4
## 2 1.3 1.3e+02 1.5e-01 0.238 90.1
## 3 1.2 7.4e+00 3.5e-02 0.093 -7.5
## 4 1.3 1.4e-01 4.8e-03 NA NA
## 5 1.4 2.3e-09 6.2e-07 NA NA
## 6 1.4 6.7e-18 3.3e-11 NA NA
## 7 1.4 2.2e-26 1.9e-15 NA NA
結果として,平行分析では4因子,MAP法では1因子,適合度基準(BIC)では3因子が良いということでした。ここでは仮に3因子だとしましょう。
fa.result <- fa(fa.dat,nfactors =3,fm="ML")
## Loading required namespace: GPArotation
print(fa.result,sort=T)
## Factor Analysis using method = ml
## Call: fa(r = fa.dat, nfactors = 3, fm = "ML")
## Standardized loadings (pattern matrix) based upon correlation matrix
## item ML2 ML1 ML3 h2 u2 com
## weight 4 0.94 0.01 0.07 0.91 0.090 1.0
## height 3 0.70 -0.16 -0.01 0.56 0.438 1.1
## year 1 -0.11 0.97 0.04 1.00 0.005 1.0
## age 2 0.36 0.64 -0.15 0.47 0.528 1.7
## HR 7 0.21 -0.03 0.77 0.71 0.290 1.2
## Hit 6 -0.12 0.03 0.74 0.52 0.478 1.1
## games 5 -0.10 0.14 0.40 0.16 0.836 1.4
##
## ML2 ML1 ML3
## SS loadings 1.60 1.40 1.33
## Proportion Var 0.23 0.20 0.19
## Cumulative Var 0.23 0.43 0.62
## Proportion Explained 0.37 0.32 0.31
## Cumulative Proportion 0.37 0.69 1.00
##
## With factor correlations of
## ML2 ML1 ML3
## ML2 1.0 -0.20 0.20
## ML1 -0.2 1.00 -0.14
## ML3 0.2 -0.14 1.00
##
## Mean item complexity = 1.2
## Test of the hypothesis that 3 factors are sufficient.
##
## The degrees of freedom for the null model are 21 and the objective function was 2.25 with Chi Square of 312.2
## The degrees of freedom for the model are 3 and the objective function was 0.12
##
## The root mean square of the residuals (RMSR) is 0.04
## The df corrected root mean square of the residuals is 0.11
##
## The harmonic number of observations is 143 with the empirical chi square 9.85 with prob < 0.02
## The total number of observations was 143 with MLE Chi Square = 15.95 with prob < 0.0012
##
## Tucker Lewis Index of factoring reliability = 0.684
## RMSEA index = 0.178 and the 90 % confidence intervals are 0.096 0.262
## BIC = 1.06
## Fit based upon off diagonal values = 0.98
## Measures of factor score adequacy
## ML2 ML1 ML3
## Correlation of scores with factors 0.96 1.00 0.88
## Multiple R square of scores with factors 0.92 0.99 0.78
## Minimum correlation of possible factor scores 0.84 0.99 0.55
因子分析の結果の見方は次の通りです。
ちょっとわかりにくいところもあるので,因子負荷量が小さなところを表示しないようにしてみます。
print(fa.result,sort=T,cut=0.3)
## Factor Analysis using method = ml
## Call: fa(r = fa.dat, nfactors = 3, fm = "ML")
## Standardized loadings (pattern matrix) based upon correlation matrix
## item ML2 ML1 ML3 h2 u2 com
## weight 4 0.94 0.91 0.090 1.0
## height 3 0.70 0.56 0.438 1.1
## year 1 0.97 1.00 0.005 1.0
## age 2 0.36 0.64 0.47 0.528 1.7
## HR 7 0.77 0.71 0.290 1.2
## Hit 6 0.74 0.52 0.478 1.1
## games 5 0.40 0.16 0.836 1.4
##
## ML2 ML1 ML3
## SS loadings 1.60 1.40 1.33
## Proportion Var 0.23 0.20 0.19
## Cumulative Var 0.23 0.43 0.62
## Proportion Explained 0.37 0.32 0.31
## Cumulative Proportion 0.37 0.69 1.00
##
## With factor correlations of
## ML2 ML1 ML3
## ML2 1.0 -0.20 0.20
## ML1 -0.2 1.00 -0.14
## ML3 0.2 -0.14 1.00
##
## Mean item complexity = 1.2
## Test of the hypothesis that 3 factors are sufficient.
##
## The degrees of freedom for the null model are 21 and the objective function was 2.25 with Chi Square of 312.2
## The degrees of freedom for the model are 3 and the objective function was 0.12
##
## The root mean square of the residuals (RMSR) is 0.04
## The df corrected root mean square of the residuals is 0.11
##
## The harmonic number of observations is 143 with the empirical chi square 9.85 with prob < 0.02
## The total number of observations was 143 with MLE Chi Square = 15.95 with prob < 0.0012
##
## Tucker Lewis Index of factoring reliability = 0.684
## RMSEA index = 0.178 and the 90 % confidence intervals are 0.096 0.262
## BIC = 1.06
## Fit based upon off diagonal values = 0.98
## Measures of factor score adequacy
## ML2 ML1 ML3
## Correlation of scores with factors 0.96 1.00 0.88
## Multiple R square of scores with factors 0.92 0.99 0.78
## Minimum correlation of possible factor scores 0.84 0.99 0.55
これを見て因子の意味を考え,因子に命名することが一般的です。
因子分析の留意点を幾つか挙げておきます。
data(bfi)
bfi.data <- bfi[1:25] # 数量データの箇所だけにする
#このbfi.dataが分析対象です。