entropy<-apply(try1[,2:25],1,function(x)entropy::entropy(unlist(x),method="ML"))
时间: 2024-03-18 10:44:16 浏览: 23
这是一个R语言的代码,使用了`apply`函数和`entropy`包中的`entropy`函数。`try1`是一个数据框,`try1[,2:25]`表示选取数据框中的第2列到第25列作为输入数据。`apply`函数的作用是对输入的数据进行操作,其中`1`表示对每行进行操作,`function(x)`表示对每一行执行的操作是一个函数,`x`表示每一行的数据。`entropy::entropy`函数的作用是计算每一行数据的熵,其中`unlist(x)`将每一行的数据转化为一个向量,`method="ML"`表示使用最大似然法来估计概率分布。因此,这段代码的功能是计算`try1`数据框中每个样本的熵值,并将结果保存在`entropy`向量中。
相关问题
#------(一)方法1:基于指标体系1的结果---- #--------1.数据导入------------- library(xlsx) d1.1 <- read.xlsx('data.xlsx', '2022', encoding = "UTF-8") #读取数据 head(d1.1,10) colnames(d1.1) d1 <- d1.1[,5:ncol(d1.1)] d1 <- abs(d1) #---------2.归一化处理--------------- Rescale = function(x, type=1) { # type=1正向指标, type=2负向指标 rng = range(x, na.rm = TRUE) if (type == 1) { (x - rng[1]) / (rng[2] - rng[1]) } else { (rng[2] - x) / (rng[2] - rng[1]) } } #---------3.熵值法步骤---------- #定义熵值函数 Entropy = function(x) { entropy=array(data = NA, dim = ncol(x),dimnames = NULL) j=1 while (j<=ncol(x)) { value=0 i=1 while (i<=nrow(x)) { if (x[i,j]==0) { (value=value) } else { (value=value+x[i,j]log(x[i,j])) } i=i+1 } entropy[j]=value(-1/log(nrow(x))) j=j+1 } return(entropy) } Entropy_Weight = function(X, index) { pos = which(index == 1) neg = which(index != 1) X[,pos] = lapply(X[,pos], Rescale, type=1) X[,neg] = lapply(X[,neg], Rescale, type=2) P = data.frame(lapply(X, function(x) x / sum(x))) e = Entropy(P) d = 1 - e # 计算信息熵冗余度 w = d / sum(d) # 计算权重向量 list(X = X,P = P, w=w) } #-------4.代入数据计算权重----- # -------二级指标权重------ ind=array(rep(1,ncol(d1))) aa=Entropy_Weight(X = d1,index = ind) weight=as.data.frame(aa["w"]) weigh X <- as.data.frame(aa["X"]) X P <- as.data.frame(aa["P"]) P d1.a <- X[,c(grep("A",colnames(X)))] d1.b <- X[,c(grep("B",colnames(X)))] d1.c <- X[,c(grep("C",colnames(X)))] d1a <- as.matrix(d1.a) d1b <- as.matrix(d1.b) d1c <- as.matrix(d1.c) n1 <- ncol(d1a) n2 <- ncol(d1b) n3 <- ncol(d1c) wa <- weight[1:n1,1] wb <- weight[(n1+1):(n1+n2),1] wc <- weight[(n1+n2+1):(n1+n2+n3),1] wa <- as.matrix(wa,ncol =1) wb <- as.matrix(wb,ncol =1) wc <- as.matrix(wc,ncol =1) indexa <- d1a%%wa indexb <- d1b%%wb indexc <- d1c%*%wc d1abc <- cbind(indexa,indexb,indexc) 参考以上代码,用不同一级指标下分别计算二级指标权重,再求一级指标权重
好的,根据你的要求,我会根据以上代码,对不同一级指标下分别计算二级指标权重,并求一级指标权重。
首先,我们需要导入数据并进行数据预处理,包括归一化处理等。然后,我们需要定义熵值函数和熵权法函数,最后代入数据计算权重。
以下为代码实现:
#-------1.数据导入-------------
library(xlsx)
d1.1 <- read.xlsx('data.xlsx', '2022', encoding = "UTF-8") #读取数据
head(d1.1,10)
colnames(d1.1)
d1 <- d1.1[,5:ncol(d1.1)]
d1 <- abs(d1)
#-------2.归一化处理---------------
Rescale = function(x, type=1) {
# type=1正向指标, type=2负向指标
rng = range(x, na.rm = TRUE)
if (type == 1) {
(x - rng[1]) / (rng[2] - rng[1])
} else {
(rng[2] - x) / (rng[2] - rng[1])
}
}
#-------3.熵值法步骤----------
#定义熵值函数
Entropy = function(x) {
entropy=array(data = NA, dim = ncol(x),dimnames = NULL)
j=1
while (j<=ncol(x)) {
value=0
i=1
while (i<=nrow(x)) {
if (x[i,j]==0) {
(value=value)
} else {
(value=value+x[i,j]log(x[i,j]))
}
i=i+1
}
entropy[j]=value(-1/log(nrow(x)))
j=j+1
}
return(entropy)
}
#定义熵权法函数
Entropy_Weight = function(X, index) {
pos = which(index == 1)
neg = which(index != 1)
X[,pos] = lapply(X[,pos], Rescale, type=1)
X[,neg] = lapply(X[,neg], Rescale, type=2)
P = data.frame(lapply(X, function(x) x / sum(x)))
e = Entropy(P)
d = 1 - e # 计算信息熵冗余度
w = d / sum(d) # 计算权重向量
list(X = X,P = P, w=w)
}
#-------4.代入数据计算权重-----
# -------一级指标权重------
ind1=array(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
aa1=Entropy_Weight(X = d1,index = ind1)
weight1=as.data.frame(aa1["w"])
weight1
# -------二级指标权重------
d1a <- d1[,c(1:6)]
d1b <- d1[,c(7:12)]
d1c <- d1[,c(13:18)]
#一级指标A下的二级指标权重
ind2a=array(c(1,1,1,1,1,1))
aa2a=Entropy_Weight(X = d1a,index = ind2a)
weight2a=as.data.frame(aa2a["w"])
weight2a
#一级指标B下的二级指标权重
ind2b=array(c(1,1,1,1,1,1))
aa2b=Entropy_Weight(X = d1b,index = ind2b)
weight2b=as.data.frame(aa2b["w"])
weight2b
#一级指标C下的二级指标权重
ind2c=array(c(1,1,1,1,1,1))
aa2c=Entropy_Weight(X = d1c,index = ind2c)
weight2c=as.data.frame(aa2c["w"])
weight2c
#计算一级指标权重
w1a <- weight2a[1:6,1]
w1b <- weight2b[1:6,1]
w1c <- weight2c[1:6,1]
w1a <- as.matrix(w1a,ncol =1)
w1b <- as.matrix(w1b,ncol =1)
w1c <- as.matrix(w1c,ncol =1)
indexa <- d1a%*%w1a
indexb <- d1b%*%w1b
indexc <- d1c%*%w1c
d1abc <- cbind(indexa,indexb,indexc)
ind1abc=array(c(1,1,1))
aa1abc=Entropy_Weight(X = d1abc,index = ind1abc)
weight1abc=as.data.frame(aa1abc["w"])
weight1abc
#------(一)方法1:基于指标体系1的结果---- #--------1.数据导入------------- library(xlsx) d1.1 <- read.xlsx('data.xlsx', '2022', encoding = "UTF-8") #读取数据 head(d1.1,10) colnames(d1.1) d1 <- d1.1[,5:ncol(d1.1)] d1 <- abs(d1) #---------2.归一化处理--------------- Rescale = function(x, type=1) { # type=1正向指标, type=2负向指标 rng = range(x, na.rm = TRUE) if (type == 1) { (x - rng[1]) / (rng[2] - rng[1]) } else { (rng[2] - x) / (rng[2] - rng[1]) } } #---------3.熵值法步骤---------- #定义熵值函数 Entropy = function(x) { entropy=array(data = NA, dim = ncol(x),dimnames = NULL) j=1 while (j<=ncol(x)) { value=0 i=1 while (i<=nrow(x)) { if (x[i,j]==0) { (value=value) } else { (value=value+x[i,j]*log(x[i,j])) } i=i+1 } entropy[j]=value*(-1/log(nrow(x))) j=j+1 } return(entropy) } Entropy_Weight = function(X, index) { pos = which(index == 1) neg = which(index != 1) X[,pos] = lapply(X[,pos], Rescale, type=1) X[,neg] = lapply(X[,neg], Rescale, type=2) P = data.frame(lapply(X, function(x) x / sum(x))) e = Entropy(P) d = 1 - e # 计算信息熵冗余度 w = d / sum(d) # 计算权重向量 list(X = X,P = P, w=w) } #-------4.代入数据计算权重----- # -------二级指标权重------ ind=array(rep(1,ncol(d1))) aa=Entropy_Weight(X = d1,index = ind) weight=as.data.frame(aa["w"]) weigh X <- as.data.frame(aa["X"]) X P <- as.data.frame(aa["P"]) P d1.a <- X[,c(grep("A",colnames(X)))] d1.b <- X[,c(grep("B",colnames(X)))] d1.c <- X[,c(grep("C",colnames(X)))] d1a <- as.matrix(d1.a) d1b <- as.matrix(d1.b) d1c <- as.matrix(d1.c) n1 <- ncol(d1a) n2 <- ncol(d1b) n3 <- ncol(d1c) wa <- weight[1:n1,1] wb <- weight[(n1+1):(n1+n2),1] wc <- weight[(n1+n2+1):(n1+n2+n3),1] wa <- as.matrix(wa,ncol =1) wb <- as.matrix(wb,ncol =1) wc <- as.matrix(wc,ncol =1) indexa <- d1a%*%wa indexb <- d1b%*%wb indexc <- d1c%*%wc d1abc <- cbind(indexa,indexb,indexc) 参考以上代码,用不同一级指标下分别计算二级指标权重,
根据以上代码,可以分别针对不同的一级指标,使用熵值法计算出各自的二级指标权重。具体的实现步骤如下:
1. 读取数据并选择需要计算的指标列。
```
library(xlsx)
d1.1 <- read.xlsx('data.xlsx', '2022', encoding = "UTF-8")
d1 <- d1.1[,5:ncol(d1.1)]
d1 <- abs(d1)
```
2. 进行归一化处理。
```
Rescale = function(x, type=1) {
# type=1正向指标, type=2负向指标
rng = range(x, na.rm = TRUE)
if (type == 1) {
(x - rng[1]) / (rng[2] - rng[1])
} else {
(rng[2] - x) / (rng[2] - rng[1])
}
}
d1[,1:4] = lapply(d1[,1:4], Rescale, type=1)
d1[,5:ncol(d1)] = lapply(d1[,5:ncol(d1)], Rescale, type=2)
```
3. 定义熵值函数。
```
Entropy = function(x) {
entropy=array(data = NA, dim = ncol(x),dimnames = NULL)
j=1
while (j<=ncol(x)) {
value=0
i=1
while (i<=nrow(x)) {
if (x[i,j]==0) {
(value=value)
} else {
(value=value+x[i,j]*log(x[i,j]))
}
i=i+1
}
entropy[j]=value*(-1/log(nrow(x)))
j=j+1
}
return(entropy)
}
```
4. 定义计算二级指标权重的函数。
```
Entropy_Weight = function(X, index) {
pos = which(index == 1)
neg = which(index != 1)
X[,pos] = lapply(X[,pos], Rescale, type=1)
X[,neg] = lapply(X[,neg], Rescale, type=2)
P = data.frame(lapply(X, function(x) x / sum(x)))
e = Entropy(P)
d = 1 - e # 计算信息熵冗余度
w = d / sum(d) # 计算权重向量
list(X = X,P = P, w=w)
}
```
5. 代入数据计算二级指标权重。
```
# 计算一级指标 A 的二级指标权重
d1.a <- d1[,c(grep("A",colnames(d1)))]
aa <- Entropy_Weight(X = d1.a, index = rep(1, ncol(d1.a)))
wa <- aa$w
# 计算一级指标 B 的二级指标权重
d1.b <- d1[,c(grep("B",colnames(d1)))]
aa <- Entropy_Weight(X = d1.b, index = rep(1, ncol(d1.b)))
wb <- aa$w
# 计算一级指标 C 的二级指标权重
d1.c <- d1[,c(grep("C",colnames(d1)))]
aa <- Entropy_Weight(X = d1.c, index = rep(1, ncol(d1.c)))
wc <- aa$w
```
6. 根据计算出来的二级指标权重,计算每个样本的综合得分。
```
indexa <- d1.a %*% wa
indexb <- d1.b %*% wb
indexc <- d1.c %*% wc
d1abc <- cbind(indexa, indexb, indexc)
```
以上代码可以根据实际情况进行调整,具体实现时需要注意数据的格式以及代码的正确性。