CRITIC方法R实现

2022-12-31 18:43:01 浏览数 (2)


title: "CRITIC方法R实现"

author: "scf"

date: '2022-12-31'

output: html_document


代码语言:{r setup, include=FALSE ,message=FALSE , warning=FALSE}复制
knitr::opts_chunk$set(echo = TRUE)

R Markdown

代码语言:{r warning=FALSE ,message=FALSE}复制
# Load necessary libraries
library(readxl)
library(tidyverse)
library(dplyr)
代码语言:text复制
# 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
代码语言:text复制
# 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)
}
代码语言:text复制
# 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
代码语言:text复制
# 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
代码语言:text复制
# Print scores
for (i in 1:length(Score)) {
  print(paste(data[i, "银行"], "银行百分制评分为:", Score[i]))
}

0 人点赞