0

私は 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 を使用してこれらのタイプの計算を理解する簡単な方法を知りたいです。

4

1 に答える 1