R realize the analytic hierarchy process to determine the index weight

Posted by php_guest on Sat, 18 Dec 2021 03:43:46 +0100

Analytic hierarchy process (AHP) is a decision-making method that decomposes the elements related to decision-making into levels such as objectives, quasi indicators and schemes, and carries out qualitative and quantitative analysis on the basis of sub levels. This paper describes the implementation process of R through an example.

summary

The basic idea of calculating index weight by analytic hierarchy process is to first establish an effective hierarchical index system, then compare the indexes to construct a judgment matrix, and then conduct digital processing and consistency test according to the judgment matrix to obtain the relative importance weight of each index.

example:

In the evaluation of regional macroeconomic benefits, three indicators are selected: capital profit and tax rate (x1), investment effect coefficient (x2) and labor productivity (x3). An expert believes that the capital profit and tax rate is extremely important than labor productivity, slightly more important than the investment effect coefficient, and the investment effect coefficient is more important than labor productivity. Try to determine the weights of the three evaluation indicators according to the expert's judgment.

indexX1X2X3
X1139
X21/315
X31/91/51

tibble storage decision matrix

options(digits = 2)
library(tidyverse)

macro <- tibble(x1=c(1,1/3,1/9), x2=c(3,1,1/5), x3=c(9,5,1))
macro

# A tibble: 3 x 3
#      x1    x2    x3
#   <dbl> <dbl> <dbl>
# 1 1       3       9
# 2 0.333   1       5
# 3 0.111   0.2     1

Calculate row geometric average

That is, calculate the row data score, and then find the P-th root of the row product result, that is, the row geometric average.

# Add w variable
macro %>% mutate(w = '^'(x1*x2*x3, 1/3)) -> macro
macro

# A tibble: 3 x 4
#      x1    x2    x3     w
#   <dbl> <dbl> <dbl> <dbl>
# 1 1       3       9 3    
# 2 0.333   1       5 1.19 
# 3 0.111   0.2     1 0.281

Normalizing w variables

The value of the w variable divided by the sum of the w column vectors.

# Define normalization function
unif <- function(x){
  x / sum(x)
}

# Weights are calculated by normalization
macro %>% mutate_at(c("w"), .funs = std) -> macro
macro

# A tibble: 3 x 4
#      x1    x2    x3      w
#   <dbl> <dbl> <dbl>  <dbl>
# 1 1       3       9 0.672 
# 2 0.333   1       5 0.265 
# 3 0.111   0.2     1 0.0629

The following is to test the weight of three variables

Consistency test

Consistency test ensures that the determination of the relative importance of each index should be coordinated and consistent, and there should be no contradiction.
The condition for judging the consistency of matrix B is that the maximum eigenvalue of matrix B is equal to the number of indexes.

The calculation process is as follows:

options(digits = 2)
library(tidyverse)

# Random consistency table
ri_table <- c(0, 0, 0.58, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52,1.54)

b <- as.matrix(macro[,-4])
w <- as.matrix(macro[,4])

## matrix product 
bw <- b %*% w  
## Maximum characteristic root
lmda <- 1/3 * sum(bw / wein)
lmda

## Consistency index CI
ci <- (lmda-length(bw)) / (length(bw) -1)
ci

## Consistency ratio CR
cr <- ci / ri_table[length(bw)]
cr
# [1] 0.025

# CR = 0.025 < 0.10, the consistency test is passed, and the weight of w above is reasonable
#          w
# [1,] 0.672
# [2,] 0.265
# [3,] 0.063

CR = 0.025 < 0.10, the consistency test is passed, so the weight of w above is reasonable.
Finally, X1 (67%), X2 (27%) and X3 (6%) are calculated, and the sum of the three is equal to 1

Complete code

options(digits = 2)
library(tidyverse)

macro <- tibble(x1=c(1,1/3,1/9), x2=c(3,1,1/5), x3=c(9,5,1))
macro

# Define normalization function
unif <- function(x){
  x / sum(x)
}

# Weights are calculated by normalization
macro %>% mutate_at(c("w"), .funs = std) -> macro
macro

# Random consistency table
ri_table <- c(0, 0, 0.58, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52,1.54)

# Consistency test
b <- as.matrix(macro[,-4])
w <- as.matrix(macro[,4])

bw <- b %*% w  
lmda <- 1/3 * sum(bw / wein)
lmda

ci <- (lmda-length(bw)) / (length(bw) -1)
ci

cr <- ci / ri_table[length(bw)]
cr

Topics: R Language linear algebra