zl程序教程

您现在的位置是:首页 >  后端

当前栏目

CRITIC方法R实现

方法 实现 critic
2023-06-13 09:16:26 时间

title: "CRITIC方法R实现"

author: "scf"

date: '2022-12-31'

output: html_document


knitr::opts_chunk$set(echo = TRUE)

R Markdown

# Load necessary libraries
library(readxl)
library(tidyverse)
library(dplyr)
# Import data
data <- read_excel("银行数据.xlsx")
label_need <- data %>% names()# Select the columns we need
label_need <- label_need[c(2:ncol(data))]
data1 <- data[,label_need]  # Convert to a matrix
data2 <- data1  # Make a copy of the matrix
# Get the number of rows and columns in data2
m <- nrow(data2)
n <- ncol(data2)
index_all <- 1:n  # Create a vector of column indices
index <- 3  # Index of negative criteria

# Normalize negative criteria
for (j in index) {
  d_max <- max(data1[,j])
  d_min <- min(data1[,j])
  data2[,j] <- (d_max - data1[,j]) / (d_max - d_min)
}

# Normalize positive criteria
index <- setdiff(index_all, index)  # Index of positive criteria
  for (j in index) {
  d_max <- max(data1[,j])
  d_min <- min(data1[,j])
  data2[,j] <- (data1[,j] - d_min) / (d_max - d_min)
}
# Calculate contrast and contradiction
the <- apply(data2, 2, sd)  # Contrast
data3 <- data2  # Make a copy of the data
#data3 <- t(data3)  # Transpose the matrix
r <- cor(data3, method = "pearson")  # Pearson correlation coefficient
f <- rowSums(1 - r)  # Sum of 1 - r
# Calculate weights
c <- the * f
w <- c / sum(c)  # Normalize weights
for(k in 1:length(label_need)){
  print(paste(label_need[k],"指标的CRITIC权重分别为:",w[k]))
}
s <- as.matrix(data2) %*% w  # Weighted sum
Score <- 100 * s / max(s)  # Calculate scores
# Print scores
for (i in 1:length(Score)) {
  print(paste(data[i, "银行"], "银行百分制评分为:", Score[i]))
}