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))
挑战杯
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 华大在线讯(通讯员 王馨雅)6月9日,湖北省第十一届“挑战杯”大学生课外学术科技作品竞赛闭幕式在华中农业大学举行。...
- 感恩今天的开启。感恩生命中出现的一切都来的轻松愉快并充满荣耀~还有什么比今天醒来自己还活着更好的呢,新的一天又开始...