R语言AHP实例分析
层次分析法应用示例
某单位拟从3名干部中选拔一名领导,选拔标准有政策水平、工作作风、业务知识、口才、写作能力和健康状况六项,下面使用AHP进行综合评估。对于选拔的六个标准,我们认为健康水平、业务知识、写作能力和政策水平同等重要,健康状况相对于口才处于稍微重要与明显重要之间,即标度为4,工作作风相对于健康状况处于同等重要和稍微重要之间,标度为2等等。对此我们可以构造判断矩阵:
options(digits = 3)
A<-c(1,1,1,4,1,1/2,1,1,2,4,1,1/2,1,1/2,1,5,3,1/2,
1/4,1/4,1/5,1,1/3,1/3,1,1,1/3,3,1,1,2,2,2,3,1,1)
names<-c("健康情况","业务知识","写作能力","口才","政策水平","工作作风")
judgeMatrix<-matrix(A,nrow = 6,byrow = TRUE,dimnames = list(names,names))
judgeMatrix
判断矩阵输出为:
同理我们可以得到这3名干部关于选拔标准的六个判断矩阵,将其设定为A1,A2,A3,A4,A5,A6
#健康状况
name<-c("甲","乙","丙")
a1<-c(1,1/4,1/2,4,1,3,2,1/3,1)
A1<-matrix(a1,nrow = 3,byrow = TRUE,dimnames = list(name,name))
#业务知识
a2<-c(1,1/4,1/4,4,1,1/2,4,2,1)
A2<-matrix(a2,nrow = 3,byrow = TRUE,dimnames = list(name,name))
#写作能力
a3<-c(1,3,1/3,1/3,1,1/6,3,6,1)
A3<-matrix(a3,nrow = 3,byrow = TRUE,dimnames = list(name,name))
#口才
a4<-c(1,1/3,5,3,1,7,1/5,1/7,1)
A4<-matrix(a4,nrow = 3,byrow = TRUE,dimnames = list(name,name))
#政策水平
a5<-c(1,1,7,1,1,7,1/7,1/7,1)
A5<-matrix(a5,nrow = 3,byrow = TRUE,dimnames = list(name,name))
#工作作风
a6<-c(1,7,9,1/7,1,2,1/9,1/2,1)
A6<-matrix(a6,nrow = 3,byrow = TRUE,dimnames = list(name,name))
A1;A2;A3;A4;A5;A6
输出结果为:
利用自编函数weight对七个判断矩阵进行权重的计算
输入:judgeMatrix 判断矩阵;round 结果约分位数
输出:权重
weight <- function (judgeMatrix, round=3) {
n = ncol(judgeMatrix)
cumProd <- vector(length=n)
cumProd <- apply(judgeMatrix, 1, prod) ##求每行连乘积
weight <- cumProd^(1/n) ##开n次方(特征向量)
weight <- weight/sum(weight) ##求权重
round(weight, round)}
W<-weight(judgeMatrix);W
w1<-weight(A1);w1
w2<-weight(A2);w2
w3<-weight(A3);w3
w4<-weight(A4);w4
w5<-weight(A5);w5
w6<-weight(A6);w6
输出结果为:
填充好权重矩阵之后,需要对判断矩阵后进行一致性检验
若计算得到一致性比例CR<0.1,则通过检验,权重矩阵有效
CRtest为自编一致性检验函数
注:CRtest调用了weight函数
输入:judgeMatrix
输出:CI, CR
CRtest <- function (judgeMatrix, round=3){
RI <- c(0, 0, 0.52, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51) #随机一致性指标
Wi <- weight(judgeMatrix) ##计算权重
n <- length(Wi)
if(n > 11){
cat("判断矩阵过大,请少于11个指标 \n")
}
if (n > 2) {
W <- matrix(Wi,ncol = 1)
judgeW <- judgeMatrix %*% W
JudgeW <- as.vector(judgeW)
la_max <- sum(JudgeW/Wi)/n
CI = (la_max - n)/(n - 1)
CR = CI/RI[n]
cat("\n 判断矩阵为:",judgeMatrix,"\n")
cat("\n CI=", round(CI, round), "\n")
cat("\n CR=", round(CR, round), "\n")
if (CR <= 0.1) {
cat(" 通过一致性检验 \n")
cat("\n Wi: ", round(Wi, round), "\n")
}
else {
cat(" 请调整判断矩阵,使CR<0.1 \n")
Wi = NULL
}
}
else if (n <= 2) {
return(Wi)
consequence <- c(round(CI, round), round(CR, round))
names(consequence) <- c("CI", "CR")
consequence }
}
对七个判断矩阵进行一致性检验
相应的权重矩阵为W,w1,w2,w3,w4,w5,w6
结果表明均通过一致性检验
CRtest(judgeMatrix)
CRtest(A1)
CRtest(A2)
CRtest(A3)
CRtest(A4)
CRtest(A5)
CRtest(A6)
输出结果为:
计算方案层得分向量
由结果可知:三位干部的评分分别是0.377,0.325,0.299,干部甲评分最高。
w_matrix<-cbind(w1,w2,w3,w4,w5,w6)
point<-w_matrix %*% W;point