R语言学习记录:主成分分析的R实现
時間: 2018-08-09
教程:知乎:Learn R | 數據降維之主成分分析(上)、Learn R | 數據降維之主成分分析(下) 作者:Jason
數據來源:《應用多元統計分析》 王學民 編著 P228-P230 習題7.6、7.7
主成分分析
1.主成分分析
使用psych包對數據進行主成分分析。
其中,principal函數進行主成分分析,fa.parallel函數生成碎石圖。
principal(r, nfactors = 1, residuals = FALSE, rotate = “varimax”,
n.obs = NA, covar = FALSE, scores = TRUE,missing = FALSE, impute =
“median”, oblique.scores=TRUE, method = “regression”,…)
r:指定輸入的數據,如果輸入的是原始數據,R將自動計算其相關系數矩陣 nfactors:指定主成分個數
residuals:是否顯示主成分模型的殘差,默認不顯示 rotate:指定模型旋轉的方法,默認為最大方差法
n.obs:如果輸入的數據是相關系數矩陣,則必須指定觀測樣本量
covar:邏輯參數,如果輸入數據為原始數據或方陣(如協方差陣),R將其轉為相關系數矩陣 scores:是否計算主成分得分
missing:缺失值處理方式,如果scores為TRUE,且missing也為TRUE,缺失值將被中位數或均值替代
impute:指定缺失值的替代方式,默認為中位數替代; method:指定主成分得分的計算方法,默認使用回歸方法計算。
(摘自教程)
如:
導入數據:
生成碎石圖:
> library(psych) > fa.parallel(data1, fa = 'pc', n.iter = 100, show.legend = FALSE) Parallel analysis suggests that the number of factors = NA and the number of components = 1
從圖中可以看出,保留兩個主成分即可。
主成分分析:
> data1_pca <- principal(data1, nfactors = 2, rotate = "none") > data1_pca Principal Components Analysis Call: principal(r = data1, nfactors = 2, rotate = "none") Standardized loadings (pattern matrix) based upon correlation matrixPC1 PC2 h2 u2 com 殺人罪 0.61 -0.70 0.86 0.14 2.0 強奸罪 0.88 -0.19 0.80 0.20 1.1 搶劫罪 0.81 0.05 0.65 0.35 1.0 傷害罪 0.80 -0.38 0.79 0.21 1.4 夜盜罪 0.89 0.23 0.85 0.15 1.1 盜竊罪 0.72 0.45 0.73 0.27 1.7 汽車犯罪 0.60 0.56 0.67 0.33 2.0PC1 PC2 SS loadings 4.11 1.24 Proportion Var 0.59 0.18 Cumulative Var 0.59 0.76 Proportion Explained 0.77 0.23 Cumulative Proportion 0.77 1.00Mean item complexity = 1.5 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 16 with prob < 0.036 Fit based upon off diagonal values = 0.97principal函數中,第一個參數是數據,第二個參數說明要保留兩個主成分,第三個參數為旋轉方法,為”none”,即不進行主成分旋轉。
輸出結果中,第一部分中PC1和PC2為兩個主成分在各觀測變量上的載荷,即觀測變量與主成分的相關系數。h2是主成分公因子方差,即主成分對每個變量的方差解釋度;u2是成分唯一性,即方差無法被主成分解釋的比例,u2+h2=1。第二部分是對于兩個主成分的說明,SS loadings行是兩主成分的特征值,Proportion Var是方差比例,即各主成分對數據的解釋程度,Cumulative Var是累計方差比例。
主成分旋轉:
> data1_pca_xz <- principal(data1, nfactors = 2, rotate = "varimax") > data1_pca_xz Principal Components Analysis Call: principal(r = data1, nfactors = 2, rotate = "varimax") Standardized loadings (pattern matrix) based upon correlation matrixRC1 RC2 h2 u2 com 殺人罪 -0.05 0.93 0.86 0.14 1.0 強奸罪 0.50 0.74 0.80 0.20 1.8 搶劫罪 0.61 0.52 0.65 0.35 2.0 傷害罪 0.32 0.83 0.79 0.21 1.3 夜盜罪 0.80 0.46 0.85 0.15 1.6 盜竊罪 0.83 0.18 0.73 0.27 1.1 汽車犯罪 0.82 0.01 0.67 0.33 1.0RC1 RC2 SS loadings 2.74 2.62 Proportion Var 0.39 0.37 Cumulative Var 0.39 0.76 Proportion Explained 0.51 0.49 Cumulative Proportion 0.51 1.00Mean item complexity = 1.4 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 16 with prob < 0.036 Fit based upon off diagonal values = 0.97> head(data1_pca_xz$weights)RC1 RC2 殺人罪 -0.285 0.510 強奸罪 0.048 0.257 搶劫罪 0.167 0.108 傷害罪 -0.073 0.358 夜盜罪 0.283 0.018 盜竊罪 0.377 -0.139rotate = varimax表示旋轉方式為正交因子旋轉。
由輸出結果可以看出,兩個主成分的方差比例不變,但在各觀測值上的載荷發生了改變,一般情況下,進行因子旋轉是為了更好的解釋主成分。
data1_pca_xz$weights的內容為主成分與原始變量之間之間線性回歸方程的系數。
principal函數中主成分得分默認不進行計算,在函數中將scores參數的值改為TRUE即可計算主成分得分。計算主成分得分:
> data1_pca_xz1 <- principal(data1, nfactors = 2, rotate = "varimax", scores = TRUE) > options(digits = 2) # 顯示兩位小數 > head(data1_pca_xz1$scores)RC1 RC2 Alabama -1.32 1.34 Alaska 0.96 0.72 Arizona 1.60 0.48 Arkansas -1.21 0.51 California 1.61 1.37 Colorado 1.46 0.26生成主成分分析圖:
> autoplot(prcomp(data1, scale = TRUE), label = TRUE)2.練習
練習內容:《應用多元統計分析》 王學民 編著 P230 習題7.7
數據內容為紐約股票交易所的五只股票(阿萊德化學、杜邦、聯合碳化物、埃克森和德士古)從1975年1月到1976年12月期間的周回報率。
阿萊德化學、杜邦和聯合碳化物屬于化工類股票,埃克森和德士古屬于石油類股票。
根據碎石圖可以看出,該數據選取兩個主成分較為合適。
由輸出結果可知,第一主成分在5個觀測變量上的載荷均較大,第二主成分在杜邦、埃克森和德士古這三個變量上載荷較大,在阿萊德化學和聯合碳化物上載荷相對較小,但相差不大。兩主成分解釋了該數據的73%,但該結果并不便于解釋兩主成分。因此,考慮進行主成分旋轉。
> data2_pca2 <- principal(data2, nfactors = 2, rotate = "varimax") > data2_pca2 Principal Components Analysis Call: principal(r = data2, nfactors = 2, rotate = "varimax") Standardized loadings (pattern matrix) based upon correlation matrixRC1 RC2 h2 u2 com 阿萊德化學 0.75 0.32 0.66 0.34 1.4 杜邦 0.89 0.12 0.81 0.19 1.0 聯合碳化物 0.77 0.31 0.69 0.31 1.3 埃克森 0.26 0.81 0.73 0.27 1.2 德士古 0.23 0.85 0.78 0.22 1.1RC1 RC2 SS loadings 2.06 1.61 Proportion Var 0.41 0.32 Cumulative Var 0.41 0.73 Proportion Explained 0.56 0.44 Cumulative Proportion 0.56 1.00Mean item complexity = 1.2 Test of the hypothesis that 2 components are sufficient.The root mean square of the residuals (RMSR) is 0.11 with the empirical chi square 24 with prob < 8.8e-07 Fit based upon off diagonal values = 0.95 > autoplot(prcomp(data2, scale = TRUE), label = TRUE)
由輸出結果可知,第一主成分在阿萊德化學、杜邦和聯合碳化物這三個變量上載荷較大較大,因此,第一主成分可以解釋為化工類股票的回報率。第二主成分埃克森和德士古這兩個變量上載荷較大,因此,第二主成分可以解釋為石油類股票的回報率。
(主成分分析圖的解釋略)
總結
以上是生活随笔為你收集整理的R语言学习记录:主成分分析的R实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rest_framework学习之解析器
- 下一篇: 数仓实时数据同步 debezium