このページはまだ書きかけです
*はじめに [#l1e65d6c]
*準備 [#j4c98aa4]
データ
#geshi(rsplus){{
> data(iris)
}}
*主成分分析 [#o8b5e7da]
主成分分析は,''多変量解析''の一手法で,高次元の数値データに対して合成成分を作る手法です.
この合成成分のことを''主成分''といい,次のように表されます.
\[\begin{array}{c@{}c@{}c} z_1&=&a_{1,1} x_{1} + a_{1,2} x_{2} + \dots + a_{1,p} x_{p} \\z_2 &=& \end{array}\]
例として,次のような2次元のデータを考えてみます.
#ref(pca_0.png,nolink,50%)
#ref(pca_1.png,nolink,50%)
#ref(pca_2.png,nolink,50%)
#ref(pca_3.png,nolink,50%)
*主成分分析を実行する [#xcf07f9a]
主成分分析を実行
#geshi(rsplus){{
> iris.pc <- princomp(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=iris, cor=T)
}}
主成分分析の結果
#geshi(rsplus){{
> iris.pc
Call:
princomp(formula = ~Sepal.Length + Sepal.Width + Petal.Length +
Petal.Width, data = iris, cor = T)
Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265
4 variables and 150 observations.
}}
固有ベクトル
#geshi(rsplus){{
> unclass(loadings(iris.pc))
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.5210659 -0.37741762 0.7195664 0.2612863
Sepal.Width -0.2693474 -0.92329566 -0.2443818 -0.1235096
Petal.Length 0.5804131 -0.02449161 -0.1421264 -0.8014492
Petal.Width 0.5648565 -0.06694199 -0.6342727 0.5235971
}}
固有値
#geshi(rsplus){{
> iris.pc$sd^2
Comp.1 Comp.2 Comp.3 Comp.4
2.91849782 0.91403047 0.14675688 0.02071484
}}
因子寄与率(主成分寄与率)
#geshi(rsplus){{
> iris.pc$sd^2/sum(iris.pc$sd^2)
Comp.1 Comp.2 Comp.3 Comp.4
0.729624454 0.228507618 0.036689219 0.005178709
}}
因子負荷量(主成分負荷量)
#geshi(rsplus){{
> t(iris.pc$sd * t(iris.pc$loadings))[, drop=F]
Comp.1 Comp.2 Comp.3 Comp.4
Sepal.Length 0.8901688 -0.36082989 0.27565767 0.03760602
Sepal.Width -0.4601427 -0.88271627 -0.09361987 -0.01777631
Petal.Length 0.9915552 -0.02341519 -0.05444699 -0.11534978
Petal.Width 0.9649790 -0.06399985 -0.24298265 0.07535950
}}
tは行列などを転置する関する,drop=F は行列と表示するオプション.
*主成分分析の結果をグラフに表示する [#abbae3e6]
#geshi(rsplus){{
> biplot(iris.pc)
}}
#ref(pca.png,nolink,50%)
*主成分スコアを求める [#ea60779f]
データの行列と固有ベクトルの内積を求める
#geshi(rsplus){{
> data.matrix(iris[,c(1,2,3,4)]) %*% unclass(loadings(iris.pc)[,c(1,2)])
}}
%*% は行列の積.
これをグラフにプロットする.
#geshi(rsplus){{
> plot(data.matrix(iris[,c(1,2,3,4)]) %*% unclass(loadings(iris.pc)[,c(1,2)]))
}}
#ref(pc.png,nolink,50%)