1. 程式人生 > >R語言學習記錄:因子分析的R實現

R語言學習記錄:因子分析的R實現

時間: 2018-08-09(學習時間)、2018-08-12(記錄時間)
教程:知乎:Learn R | 資料降維之主成分分析(上)Learn R | 資料降維之因子分析(下) 作者:Jason
資料來源:《應用多元統計分析》 王學民 編著 P261-P262 習題8.5、8.6

因子分析

1.因子分析

使用psych包對資料進行因子分析。
其中,fa函式進行主成分分析,fa.parallel函式生成碎石圖。

fa(r, nfactors=, n.obs=, rotate=, scores=, fm=) r:相關係數矩陣或原始資料矩陣,
nfactors:設定主提取的因子數(預設為1) n.obs:觀測數(輸入相關係數矩陣時需要填寫)
rotate:設定旋轉的方法(預設互變異數最小法) scores:設定是否需要計算因子得分(預設不需要)
fm:設定因子化方法(預設極小殘差法)

提取公因子的方法(fm),方法包括: ml:最大似然法 pa:主軸迭代法 wls:加權最小二乘法 gls:廣義加權最小二乘法
minres:最小殘差法
(摘自教程

如:

 # 匯入資料
> library(openxlsx)
> data1 <- read.xlsx("E:\\Learning_R\\因子分析\\exec8.5.xlsx",rows = 1:13, cols = 1:5 )
> head(data1)
  人口 教育 傭人 服務  房價
1 5700 12.8 2500  270 25000
2 1000 10.9  600   10 10000
3 3400
8.8 1000 10 9000 4 3800 13.6 1700 140 25000 5 4000 12.8 1600 140 25000 6 8200 8.3 2600 60 12000 > data1_cor <- cor(data1) > head(cor(data1),3) 人口 教育 傭人 服務 房價 人口 1.00000000 0.00975059 0.9724483 0.4388708 0.02241157 教育 0.00975059 1.00000000 0.1542838 0.6914082 0.86307009 傭人 0.97244826 0.15428378 1.0000000 0.5147184 0.12192599 # 生成碎石圖
> library(psych) > fa.parallel(data1_cor, n.obs = 112, fa = "both", n.iter = 100) Parallel analysis suggests that the number of factors = 2 and the number of components = 2

這裡寫圖片描述
從圖中和函式給出的資訊可以看出,保留兩個主成分即可。

# 因子分析
> fa_model1 <- fa(data1_cor, nfactors = 2, rotate = "none", fm = "ml")
> fa_model1
Factor Analysis using method =  ml
Call: fa(r = data1_cor, nfactors = 2, rotate = "none", fm = "ml")
Standardized loadings (pattern matrix) based upon correlation matrix
       ML2  ML1   h2    u2 com
人口 -0.03 1.00 1.00 0.005 1.0
教育  0.90 0.04 0.81 0.193 1.0
傭人  0.09 0.98 0.96 0.036 1.0
服務  0.78 0.46 0.81 0.185 1.6
房價  0.96 0.05 0.93 0.074 1.0

                       ML2  ML1
SS loadings           2.34 2.16
Proportion Var        0.47 0.43
Cumulative Var        0.47 0.90
Proportion Explained  0.52 0.48
Cumulative Proportion 0.52 1.00

Mean item complexity =  1.1
Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  10  and the objective function was  6.38
The degrees of freedom for the model are 1  and the objective function was  0.31 

The root mean square of the residuals (RMSR) is  0.01 
The df corrected root mean square of the residuals is  0.05 

Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                   ML2  ML1
Correlation of (regression) scores with factors   0.98 1.00
Multiple R square of scores with factors          0.95 1.00
Minimum correlation of possible factor scores     0.91 0.99

fa函式中,第一個引數是資料,第二個引數說明要保留兩個主成分,第三個引數為旋轉方法,為none,即不進行主成分旋轉,第四個引數表示提取公因子的方法為最大似然法。
輸出結果中與主成分輸出結果基本一致,可以看出該資料選取兩個主成分解釋了原始5個變數的90%的方差。

# 因子旋轉:正交旋轉法
> fa_model2 <- fa(data1_cor, nfactors = 2, rotate = "varimax", fm = "ml")
> fa_model2
Factor Analysis using method =  ml
Call: fa(r = data1_cor, nfactors = 2, rotate = "varimax", fm = "ml")
Standardized loadings (pattern matrix) based upon correlation matrix
      ML2  ML1   h2    u2 com
人口 0.02 1.00 1.00 0.005 1.0
教育 0.90 0.00 0.81 0.193 1.0
傭人 0.14 0.97 0.96 0.036 1.0
服務 0.80 0.42 0.81 0.185 1.5
房價 0.96 0.00 0.93 0.074 1.0

                       ML2  ML1
SS loadings           2.39 2.12
Proportion Var        0.48 0.42
Cumulative Var        0.48 0.90
Proportion Explained  0.53 0.47
Cumulative Proportion 0.53 1.00

Mean item complexity =  1.1
Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  10  and the objective function was  6.38
The degrees of freedom for the model are 1  and the objective function was  0.31 

The root mean square of the residuals (RMSR) is  0.01 
The df corrected root mean square of the residuals is  0.05 

Fit based upon off diagonal values = 1
Measures of factor score adequacy             
                                                   ML2  ML1
Correlation of (regression) scores with factors   0.98 1.00
Multiple R square of scores with factors          0.95 1.00
Minimum correlation of possible factor scores     0.91 0.99
> fa_model2$weights
            ML2          ML1
人口 -0.2023403  0.887108659
教育  0.2178564 -0.009363652
傭人  0.1241204  0.113241682
服務  0.1974628  0.001662584
房價  0.6101433 -0.025881048

rotate = varimax表示旋轉方式為正交因子旋轉。
由輸出結果可以看出,兩個因子的方差比例不變,但在各觀測值上的載荷發生了改變,
data1_pca_xz$weights的內容為因子與原始變數之間之間線性迴歸方程的係數。

主成分分析中,主成分可以表示為原始變數的線性組合,原始變數也可以表示為因子的線性組合;而在因子分析中,原始變數是因子的線性組合,因子卻不能表示為主成分的線性組合。

使用factor.plot函式對旋轉結果進行視覺化:

> factor.plot(fa_model2)

這裡寫圖片描述

> fa.diagram(fa_model2, simple = FALSE)

這裡寫圖片描述
由上述結果和分析可知,該資料可以取兩個因子,f1因子是與地區的房價水平、教育水平和各種服務行業的人數有主要關係的地區經濟發達因子,而f2因子是與地區的總人口數和僱傭人口數有主要關係的地區人口規模因子。

2.練習

練習內容:《應用多元統計分析》 王學民 編著 P260-P261 習題8.6
資料內容:某公司老闆對48名應聘者進行命面試,並給出他們在15個方面所得的分數,這15個方面分別為:申請書的形式、外貌、專業能力、討人喜歡、自信心、精明、誠實、推銷能力、經驗、積極性、抱負、理解能力、潛力、交際能力和適應性。
程式碼及結果:

> library(openxlsx)
> data2 <- read.xlsx("E:\\Learning_R\\因子分析\\exec8.6.xlsx",rows = 1:49, cols = 1:15 )
> head(data2)
  申請 外貌 專業 討喜 自信 精明 誠實 推銷 經驗 積極 抱負 理解 潛力
1    6    7    2    5    8    7    8    8    3    8    9    7    5
2    9   10    5    8   10    9    9   10    5    9    9    8    8
3    7    8    3    6    9    8    9    7    4    9    9    8    6
4    5    6    8    5    6    5    9    2    8    4    5    8    7
5    6    8    8    8    4    4    9    2    8    5    5    8    8
6    7    7    7    6    8    7   10    5    9    6    5    8    6
  交際 適應
1    7   10
2    8   10
3    8   10
4    6    5
5    7    7
6    6    6
> data2_cor <- cor(data2)
> library(psych)
> fa.parallel(data2_cor, n.obs = 112, fa = "both", n.iter = 100)
Parallel analysis suggests that the number of factors =  4  and the number of components =  3 

這裡寫圖片描述
由函式資訊可知選取4個因子即可,但根據碎石圖中曲線斜率來看,選取5個因子更為合適。

> fa_model4 <- fa(data2_cor, nfactors = 5, rotate = "varimax", fm = "pa")
> fa_model4
Factor Analysis using method =  pa
Call: fa(r = data2_cor, nfactors = 5, rotate = "varimax", fm = "pa")
Standardized loadings (pattern matrix) based upon correlation matrix
      PA1   PA2   PA3   PA5   PA4   h2       u2 com
申請 0.13  0.72  0.09  0.07 -0.10 0.55  0.44528 1.2
外貌 0.32  0.15  0.23  0.93  0.07 1.05 -0.05444 1.4
專業 0.06  0.13 -0.01  0.04  0.68 0.48  0.51909 1.1
討喜 0.22  0.24  0.83  0.10 -0.06 0.82  0.18441 1.4
自信 0.90 -0.09  0.16  0.14 -0.07 0.87  0.12895 1.1
精明 0.85  0.12  0.29  0.02  0.05 0.82  0.18211 1.3
誠實 0.23 -0.22  0.75  0.14 -0.01 0.68  0.32043 1.5
推銷 0.89  0.23  0.07  0.15 -0.06 0.88  0.12234 1.2
經驗 0.09  0.79 -0.04 -0.01  0.21 0.68  0.32457 1.2
積極 0.77  0.37  0.19  0.01 -0.02 0.77  0.23364 1.6
抱負 0.88  0.18  0.07  0.24 -0.07 0.88  0.12430 1.3
理解 0.78  0.27  0.34  0.13  0.17 0.84  0.15532 1.8
潛力 0.72  0.34  0.43  0.11  0.27 0.92  0.08076 2.6
交際 0.44  0.39  0.56  0.00 -0.58 1.00  0.00027 3.6
適應 0.35  0.78  0.06  0.13  0.09 0.76  0.24244 1.5

                       PA1  PA2  PA3  PA5  PA4
SS loadings           5.38 2.47 2.09 1.05 0.99
Proportion Var        0.36 0.16 0.14 0.07 0.07
Cumulative Var        0.36 0.52 0.66 0.73 0.80
Proportion Explained  0.45 0.21 0.17 0.09 0.08
Cumulative Proportion 0.45 0.65 0.83 0.92 1.00

Mean item complexity =  1.6
Test of the hypothesis that 5 factors are sufficient.

The degrees of freedom for the null model are  105  and the objective function was  15.75
The degrees of freedom for the model are 40  and the objective function was  1.9 

The root mean square of the residuals (RMSR) is  0.02 
The df corrected root mean square of the residuals is  0.04 

Fit based upon off diagonal values = 1

為了便於解釋,我們對因子進行了因子旋轉,從結果可以看出,自信心、精明、推銷能力、積極性、抱負、理解能力和潛力在第一個因子上載荷較大,我們可以將第一因子定義為進取能幹;申請書的形式、經驗和適應性在第二個因子上載荷較大,我們可以將第二因子定義為經驗;討人喜歡、誠實、潛力和交際能力在第三個因子上載荷較大,我們可以將第三因子定義為內在品質;專業能力和交際能力在第四個因子上載荷較大,我們可以將第四因子定義為外貌;外貌在在第五個因子上載荷較大,我們可以將第五因子定義為外貌。

> factor.plot(fa_model4)

這裡寫圖片描述

> fa.diagram(fa_model3, simple = FALSE)

這裡寫圖片描述

因子分析圖的解釋略。

> fa_model3$weights
             PA1          PA2
人口 -0.66534374  1.247355565
教育 -0.03266863  0.152682328
傭人  0.64113113 -0.253338929
服務  0.09545333  0.004111142
房價  0.87248431 -0.139003564

原始變數與因子的線性表示式略。