挑战杯

setwd("F:/Users/liang/OneDrive/文档/同步文档/层次分析法各矩阵/新建文件夹/新建文件夹")
weight <- function (judgeMatrix, round=5) {#权重
 print(judgeMatrix)
 n = ncol(judgeMatrix)
 cumProd <- vector(length=n)
 cumProd <- apply(judgeMatrix, 1, prod) ##求每行连乘积
 weight <- cumProd^(1/n) ##开n次方(特征向量)
 weight <- weight/sum(weight) ##求权重
 #round(weight, round)
}
CRtest <- function (judgeMatrix, round=3){
 RI <- c(0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51) #随机一致性指标
 Wi <- weight(judgeMatrix) ##计算权重
 #print(Wi)
 n <- length(Wi)
 if(n > 11){
 cat("判断矩阵过大,请少于11个指标 \n")
 }
 status='fail'
 if (n > 2) {
 W <- matrix(Wi, ncol = 1)
 judgeW <- judgeMatrix %*% W
 JudgeW <- as.vector(judgeW)
 la_max <- sum(JudgeW/Wi)/n
 print("最大特征根")
 print(la_max)
 CI = (la_max - n)/(n - 1)
 #print(CI,n)
 CR = CI/RI[n]
 #cat("\n CI=", round(CI, round), "\n")
 #cat("CR=", round(CR, round), "\n")
 if (CR <= 0.1) {
 #cat("通过一致性检验 \n")
 status='Success'
 #cat("\n Wi: ", round(Wi, round), "\n")
 }
 else {
 status="fail"
 #print(" 请调整判断矩阵,使CR<0.1")
 #Wi = NULL
 }
 }
 else if (n <= 2) {
 names(Wi)<-colnames(judgeMatrix)
 return(Wi)
 }
 #consequence <- c(Wi,CI,CR,status)
 consequence <- c(round(Wi, round),round(CI, round), round(CR, round),status)
 names(consequence) <-c(colnames(judgeMatrix),"CI", "CR",'STATUS')
 consequence
}
calcuTable<-function(inputfile){#转置
 dataframe=read.csv(inputfile,header=F)
 JMatrix<-data.matrix(dataframe)
 #print(JMatrix)
 t(CRtest(JMatrix))
}
Coe<-function(table){
 x=NULL
 nums=0
 if ((ncol(table)-3)<=2){
 nums=ncol(table)}
 else{
 nums=ncol(table)-3
 }
 for(i in 1:nums){
 col_i=as.vector(table[,i])
 #print(table[,i])
 x<-c(x,sd(col_i)/mean(as.numeric(table[,i])))
 }
 names(x)=colnames(table)[1:nums]
 x
}
files<-dir()
for (file in files){
 print(file)
 print(calcuTable(file))
 print("------------------------")
}

# weight_table=NULL

#inputname<-paste0("一级指标",num,".csv")

#weight_table=rbind(weight_table,calcuTable(inputname))

#print(Coe(weight_table))
setwd("F:/Users/liang/OneDrive/文档/同步文档/层次分析法各矩阵/新建文件夹/新建文件夹")

weight <- function (judgeMatrix, round=5) #权重

 print(judgeMatrix)

 n = ncol(judgeMatrix)

 cumProd <- vector(length=n)

 cumProd <- apply(judgeMatrix, 1, prod) ##求每行连乘积

 weight <- cumProd^(1/n) ##开n次方(特征向量)

 weight <- weight/sum(weight) ##求权重

 #round(weight, round)

}

CRtest <- function (judgeMatrix, round=3){

 RI <- c(0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51) #随机一致性指标

 Wi <- weight(judgeMatrix) ##计算权重

 #print(Wi)

 n <- length(Wi)

 if(n > 11){

 cat("判断矩阵过大,请少于11个指标 \n")

 }

 status='fail'

 if (n > 2) {

 W <- matrix(Wi, ncol = 1)

 judgeW <- judgeMatrix %*% W

 JudgeW <- as.vector(judgeW)

 la_max <- sum(JudgeW/Wi)/n

 print("最大特征根")

 print(la_max)

 CI = (la_max - n)/(n - 1)

 #print(CI,n)

 CR = CI/RI[n]

 #cat("\n CI=", round(CI, round), "\n")

 #cat("CR=", round(CR, round), "\n")

 if (CR <= 0.1) {

 #cat("通过一致性检验 \n")

 status='Success'

 #cat("\n Wi: ", round(Wi, round), "\n")

 }

 else {

 status="fail"

 #print(" 请调整判断矩阵,使CR<0.1")

 #Wi = NULL

 }

 }

 else if (n <= 2) {

 names(Wi)<-colnames(judgeMatrix)

 return(Wi)

 }

 #consequence <- c(Wi,CI,CR,status)

 consequence <- c(round(Wi, round),round(CI, round), round(CR, round),status)

 names(consequence) <-c(colnames(judgeMatrix),"CI", "CR",'STATUS')

 consequence

}

calcuTable<-function(inputfile){#转置

 dataframe=read.csv(inputfile,header=F)

 JMatrix<-data.matrix(dataframe)

 #print(JMatrix)

 t(CRtest(JMatrix))

}

Coe<-function(table){

 x=NULL

 nums=0

 if ((ncol(table)-3)<=2){

 nums=ncol(table)}

 else{

 nums=ncol(table)-3

 }

 for(i in 1:nums){

 col_i=as.vector(table[,i])

 #print(table[,i])

 x<-c(x,sd(col_i)/mean(as.numeric(table[,i])))

 }

 names(x)=colnames(table)[1:nums]

 x

}

files<-dir()

for (file in files){

 print(file)

 print(calcuTable(file))

 print("------------------------")

}

# weight_table=NULL

#inputname<-paste0("一级指标",num,".csv")

#weight_table=rbind(weight_table,calcuTable(inputname))

#print(Coe(weight_table))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容