R的基础安装包提供了PCA为 princomp()
principal() 含多种可选的方差旋转方法的主成分分析 fa() 可用主轴、最小残差、加权最小平方或最大似然法估计的因子分析 fa.parallel() 含平行分析的碎石图 factor.plot() 绘制因子分析或主成分分析的结果 fa.diagram() 绘制因子分析或主成分的载荷矩阵 scree() 因子分析和主成分分析的碎石图
setwd("E:\\Rwork")
library(psych)
data<-read.csv("2012MLB.csv", header=T, sep=",")
fa.parallel(data[,-1], fa = "pc", n.iter = 100,
show.legend = FALSE, main = "screen plot with parallel analysis")
pc <- principal(data[,-1], nfactors = 2)
plot(pc$values,type = "b")
> pc
Principal Components Analysis
Call: principal(r = data[, -1], nfactors = 2)
Standardized loadings (pattern matrix) based upon correlation matrix
RC1 RC2 h2 u2 com
G -0.08 -0.61 0.374 0.626 1.0
R 0.86 0.44 0.931 0.069 1.5
H 0.98 -0.12 0.966 0.034 1.0
H1B 0.81 -0.49 0.899 0.101 1.7
H2B 0.67 -0.05 0.449 0.551 1.0
H3B 0.20 -0.52 0.312 0.688 1.3
HR 0.23 0.90 0.856 0.144 1.1
RBI 0.85 0.46 0.942 0.058 1.5
BB -0.07 0.26 0.072 0.928 1.2
SO -0.63 0.48 0.624 0.376 1.9
SB -0.05 -0.36 0.131 0.869 1.0
AVG 0.98 -0.09 0.976 0.024 1.0
OBP 0.90 0.09 0.826 0.174 1.0
RC1 RC2
SS loadings 5.81 2.55
Proportion Var 0.45 0.20
Cumulative Var 0.45 0.64
Proportion Explained 0.69 0.31
Cumulative Proportion 0.69 1.00
Mean item complexity = 1.3
Test of the hypothesis that 2 components are sufficient.
The root mean square of the residuals (RMSR) is 0.09
with the empirical chi square 37.9 with prob < 0.94
Fit based upon off diagonal values = 0.96
data1 <-data[,c(3:5,9,13,14)]
fa.parallel(data1, fa = "pc", n.iter = 100,
show.legend = FALSE, main = "screen plot with parallel analysis")
pc <- principal(data1, nfactors = 1)
Principal Components Analysis
Call: principal(r = data1, nfactors = 1)
Standardized loadings (pattern matrix) based upon correlation matrix
PC1 h2 u2 com
R 0.89 0.79 0.206 1
H 0.96 0.92 0.076 1
H1B 0.79 0.62 0.378 1
RBI 0.88 0.78 0.218 1
AVG 0.97 0.95 0.054 1
OBP 0.92 0.85 0.152 1
PC1
SS loadings 4.92
Proportion Var 0.82
Mean item complexity = 1
Test of the hypothesis that 1 component is sufficient.
The root mean square of the residuals (RMSR) is 0.12
with the empirical chi square 13.66 with prob < 0.13
Fit based upon off diagonal values = 0.98