R中的成对比较公式

时间:2018-07-18 23:56:05

标签: r matrix pairwise

我是R的新手,需要对一组变量进行成对比较公式。要比较的元素数量将动态变化,但这是一个包含4个元素的硬编码示例,每个元素彼此比较:

#there are 4 choices A, B, C, D - 
#they are compared against each other and comparisons are stored:
df1 <- data.frame("A" = c(80),"B" = c(20))
df2 <- data.frame("A" = c(90),"C" = c(10))
df3 <- data.frame("A" = c(95), "D" = c(5))
df4 <- data.frame("B" = c(80), "C" = c(20))
df5 <- data.frame("B" = c(90), "D" = c(10))
df6 <- data.frame("C" = c(80), "D" = c(20))

#show the different comparisons in a matrix
matrixA <- matrix(c("", df1$B[1], df2$C[1], df3$D[1],
                df1$A[1],     "", df4$C[1], df5$D[1],
                df2$A[1], df4$B[1],     "", df6$D[1],
                df3$A[1], df5$B[1], df6$C[1],    ""),
              nrow=4,ncol = 4,byrow = TRUE)
dimnames(matrixA) = list(c("A","B","C","D"),c("A","B","C","D"))

#perform calculations on the comparisons
matrixB <- matrix(
      c(1,              df1$B[1]/df1$A[1], df2$C[1]/df2$A[1], df3$D[1]/df3$A[1], 
        df1$A[1]/df1$B[1],              1, df4$C[1]/df4$B[1], df5$D[1]/df5$B[1],
        df2$A[1]/df2$C[1], df4$B[1]/df4$C[1],              1, df6$D[1]/df6$C[1],
        df3$A[1]/df3$D[1], df5$B[1]/df5$D[1], df6$C[1]/df6$D[1],         1),
              nrow = 4, ncol = 4, byrow = TRUE)
matrixB <- rbind(matrixB, colSums(matrixB)) #add the sum of the colums
dimnames(matrixB) = list(c("A","B","C","D","Sum"),c("A","B","C","D"))

#so some more calculations that I'll use later on
dfC <- data.frame("AB" = c(matrixB["A","A"] / matrixB["A","B"], 
                        matrixB["B","A"] / matrixB["B","B"],
                        matrixB["C","A"] / matrixB["C","B"],
                        matrixB["D","A"] / matrixB["D","B"]),
              "BC" = c(matrixB["A","B"] / matrixB["A","C"],
                        matrixB["B","B"] / matrixB["B","C"],
                        matrixB["C","B"] / matrixB["C","C"],
                        matrixB["D","B"] / matrixB["D","C"]
                        ), 
              "CD" = c(matrixB["A","C"] / matrixB["A","D"],
                        matrixB["B","C"] / matrixB["B","D"],
                        matrixB["C","C"] / matrixB["C","D"],
                        matrixB["D","C"] / matrixB["D","D"]))

dfCMeans <- colMeans(dfC)

#create the normalization matrix
matrixN <- matrix(c(
  matrixB["A","A"] / matrixB["Sum","A"], matrixB["A","B"] / matrixB["Sum","B"], matrixB["A","C"] / matrixB["Sum","C"], matrixB["A","D"] / matrixB["Sum","D"],
  matrixB["B","A"] / matrixB["Sum","A"], matrixB["B","B"] / matrixB["Sum","B"], matrixB["B","C"] / matrixB["Sum","C"], matrixB["B","D"] / matrixB["Sum","D"],
  matrixB["C","A"] / matrixB["Sum","A"], matrixB["C","B"] / matrixB["Sum","B"], matrixB["C","C"] / matrixB["Sum","C"], matrixB["C","D"] / matrixB["Sum","D"],
  matrixB["D","A"] / matrixB["Sum","A"], matrixB["D","B"] / matrixB["Sum","B"],     matrixB["D","C"] / matrixB["Sum","C"], matrixB["D","D"] / matrixB["Sum","D"]
  ), nrow = 4, ncol = 4, byrow = TRUE)

由于R非常简洁,因此似乎应该有一种更好的方法,我想知道一种使用R来计算这些类型的计算的简便方法。

1 个答案:

答案 0 :(得分:1)

好的,我可能会在这里开始拼凑一些东西。

我们从这样的矩阵开始:

A <- structure(
  c(NA, 20, 10, 5, 80, NA, 20, 10, 90, 80, NA, 20, 95, 90, 80, NA),
  .Dim = c(4, 4),
  .Dimnames = list(LETTERS[1:4], LETTERS[1:4]))

A
#    A  B  C  D
# A NA 80 90 95
# B 20 NA 80 90
# C 10 20 NA 80
# D  5 10 20 NA

此矩阵是对长度为4的向量进行成对比较的结果,我们对该向量一无所知,而我们唯一了解的是用于比较的函数是二进制非可交换的,或者更多精确地:f(x,y)= 100-f(y,x),结果是∈[0,100]。

matrixB似乎只是matrixA除以其自身的转置:

  

B = A T A -1

或者,如果您愿意:

  

B =(100-A)/ A

由于上述特性而导致的马铃薯patato。

B <- (100 - A) / A
B <- t(A) / A

# fill in the diagonal with 1s
diag(B) <- 1

round(B, 2)
#    A    B    C    D
# A  1 0.25 0.11 0.05
# B  4 1.00 0.25 0.11
# C  9 4.00 1.00 0.25
# D 19 9.00 4.00 1.00

您所谓的“规范化”矩阵似乎就是每一列除以其总和。

B.norm <- t(t(B) / colSums(B))

round(B.norm, 3)
#       A     B     C     D
# A 0.030 0.018 0.021 0.037
# B 0.121 0.070 0.047 0.079
# C 0.273 0.281 0.187 0.177
# D 0.576 0.632 0.746 0.707