简介
PCA是一种属降维方法,将众多具有一定相关性的变量重组为一组新的相互无关的综合变量,可以用作降维或评价(建议不用,因为新综合变量很难解释)。此方法在实际运用中有一个难点,就是选择主成分后,需要注意主成分实际含义的解释(难点)。
PCA步骤
1、原始数据标准化处理
2、计算样本相关系数矩阵
3、计算相关系数矩阵的特征值以及相应的特征向量
4、选择主成分并写出表达式
根据贡献率选出主成分
5、计算主成分得分:样本数据*主成分特征向量
6、进一步分析
实现代码
% Read data from a file (e.g. excel) and place it in a matrix.
A=xlsread('Coporation_evaluation.xlsx', 'B2:I16');
% Transfer orginal data to standard data
a=size(A,1); % Get the row number of A
b=size(A,2); % Get the column number of A
for i=1:b
SA(:,i)=(A(:,i)-mean(A(:,i)))/std(A(:,i)); % Matrix normalization
end
% Calculate correlation matrix of A.
CM=corrcoef(SA);
% Calculate eigenvectors and eigenvalues of correlation matrix.
[V, D]=eig(CM);
% Get the eigenvalue sequence according to descending and the corrosponding
% attribution rates and accumulation rates.
for j=1:b
DS(j,1)=D(b+1-j, b+1-j);
end
for i=1:b
DS(i,2)=DS(i,1)/sum(DS(:,1)); % 贡献率
DS(i,3)=sum(DS(1:i,1))/sum(DS(:,1)); % 累计贡献率
end
% Calculate the numvber of principal components.
T=0.9; % set the threshold value for evaluating information preservation level.
for K=1:b
if DS(K,3)>=T
Com_num=K;
break;
end
end
% Get the eigenvectors of the Com_num principal components
for j=1:Com_num
PV(:,j)=V(:,b+1-j); % 与之前倒序排列的特征值相匹配,故需要倒序
end
% Calculate the new socres of the orginal items
new_score=SA*PV;
for i=1:a
total_score(i,2)=sum(new_score(i,:));
total_score(i,1)=i;
end
new_score_s=sortrows(total_score,-2);
% Displays result reports
disp('特征值及贡献率:')
DS
disp('阀值T对应的主成分数与特征向量:')
Com_num
PV
disp('主成分分数:')
new_score
disp('主成分分数排序:')
new_score_s
易知第9家综合实力最强,第12家综合实力最弱