前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CRITIC方法R实现

CRITIC方法R实现

原创
作者头像
用户4158596
发布2022-12-31 18:43:01
7680
发布2022-12-31 18:43:01
举报
文章被收录于专栏:scf的note

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]))
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R Markdown
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档