# Introduction to AHPWR package

knitr::opts_chunk\$set(warning = FALSE, message = FALSE)
library(AHPWR)
library(kableExtra)
#> Warning: package 'kableExtra' was built under R version 4.2.2

This is the introduction to our package AHPWR. All theory about this method is concerned about Saaty and Vargas (2012). The examples are inspired by this reference.

## AHP

According to Saaty and Vargas (2012), The Analytic Hierarchy Process (AHP) is a basic approach to decision making. It is designed to cope with both the rational and the intuitive to select the best from a number of alternatives evaluated with respect to several criteria. In this process, the decision maker carries out simple pairwise comparison judgments which are then used to develop overall priorities for ranking the alternatives. The AHP both allows for inconsistency in the judgments and provides a means to improve consistency.

The simplest form used to structure a decision problem is a hierarchy consisting of three levels: the goal of the decision at the top level, followed by a second level consisting of the criteria by which the alternatives, located in the third level, will be evaluated.

## A tree level hierarchy

In the AHPWR package we can create the tree level hierarchy of the problem, as in the following example:

#generic, c= 4 criteria and a = 3 alternatives
flow_chart(names=NULL, c=4, a=3) You can change the graphics according to ggplot2 options:

#generic, c= 4 criteria and a = 3 alternatives
p=flow_chart(names=NULL, c=4, a=3)
p+ggplot2::labs(title = "A tree level hierarchy", x="", y="") #generic, c= 4 criteria and a = 3 alternatives
p=flow_chart(names=NULL, c=4, a=3)
p+ggplot2::labs(title = "A tree level hierarchy", x="", y="")+ggplot2::theme_void() #generic, c= 4 criteria and a = 3 alternatives
goal = "Satisfation with House"
criterios = c("Size", "Age", "Yard", "Neighborhood" )
alternatives = c("house A", "house B", "house C")
names = c(goal, criterios, alternatives)
p=flow_chart(names, c=4, a=3)
p+ggplot2::labs(title = "A tree level hierarchy", x="", y="")+ggplot2::theme_void() ## The comparative judment

The next step is comparative judgment. The elements on the second level are arranged into a matrix and the family buying the house makes judgments about the relative importance of the elements with respect to the overall goal, Satisfaction with House.

The questions to ask when comparing two criteria are of the following kind: of the two alternatives being compared, which is considered more important by the family and how much more important is it with respect to family satisfaction with the house, which is the overall goal?

Paired comparison judgments in the AHP are applied to pairs of homogeneous elements. The fundamental scale of values to represent the intensities of judgments is shown in Table 1. This scale has been validated for effectiveness, not only in many applications by a number of people, but also through theoretical justification of what scale one must use in the comparison of homogeneous elements.

Table 1: The fundamental Scale
Intensity.of.importance Definicion
1 Equal Importance
2 Weak
3 Moderate importance
4 Moderate plus
5 Strong importance
6 Strong plus
7 Very strong or demonstrated importance
8 Very, very strong
9 Extreme importance

Here we will use the holistic judgment criterion proposed by Godoy (2014). It provides weights for each criterion using the Saaty scale: assuming that there are $$n$$ criteria, establish different weights for each of the criteria according to their importance, with $$w1$$ being the weight of criterion 1; $$w2$$ the weight of criterion 2 and so on.

The judge, before assigning a holistic weight, should order the items from the most important to the least important and then establish the weights that should be different for each item and descending according to the established order, unless two consecutive items have the same importance, only in this case can they have the same weight. For example, if $$w1 < w2 < ... < wn$$ so order the items are A1 is less important than A2 which is less important than A3 and so on ; or An is more important than An-1 which is more important than An-2 and so on.

The hierarchy matrix will be constructed by making $$a_{ij} = wi – wj +1$$ if $$wi > wj$$ (ie, criterion $$i$$ has greater importance than criterion $$j$$); $$aij = 1/(wj – wi +1)$$ if $$wi < wj$$.

### Example 1:

The problem is to determine the best choice between two alternatives A1 = construction of a bridge connecting two points; A2 = construction of a tunnel connecting two points, based on the following criteria: C1-life cycle, C2-maintenance cost, C3-environmental impacts, C4-construction cost.

Holistic Judgment:

M1 - Criteria judgment matrix Weights attributed by the evaluators to each criterion: w1=2; w2 = 5; w3 = 2; w4 = 3, therefore, the order of importance of the criteria according to the judge is criterion 2 followed by criterion 4 followed by both criterion 1 and 3 with the same importance.

x = c("life cycle", "maintenance cost", "environmental impacts", "construction cost") #criteria
y = c(2,5,2,3) #weights
m1 = matrix_ahp(x,y)
m1
#>                       life cycle maintenance cost environmental impacts
#> life cycle                     1        0.2500000                     1
#> maintenance cost               4        1.0000000                     4
#> environmental impacts          1        0.2500000                     1
#> construction cost              2        0.3333333                     2
#>                       construction cost
#> life cycle                          0.5
#> maintenance cost                    3.0
#> environmental impacts               0.5
#> construction cost                   1.0

The table with all date about the matrix, the first line informs the weights assigned by the evaluators, the following lines up to the penultimate one show the comparison matrix between criteria or alternatives and the last line informs the priority vector and the CR consistency index.

names(y) = x
table=tabela_holistica(pesos=y)
table
#> # A tibble: 6 × 6
#>   ...1                  life cycle maintenance cost environm…¹ const…² CR
#>   <chr>                        <dbl>              <dbl>      <dbl>   <dbl> <chr>
#> 1 weights                      2                  5          2       3     _
#> 2 life cycle                   1                  0.25       1       0.5   _
#> 3 maintenance cost             4                  1          4       3     _
#> 4 environmental impacts        1                  0.25       1       0.5   _
#> 5 construction cost            2                  0.333      2       1     _
#> 6 priority                     0.121              0.538      0.121   0.220 0.01
#> # … with abbreviated variable names ¹​environmental impacts,
#> #   ²​construction cost
knitr::kable(table)
…1 life cycle maintenance cost environmental impacts construction cost CR
weights 2.0000000 5.0000000 2.0000000 3.0000000 _
life cycle 1.0000000 0.2500000 1.0000000 0.5000000 _
maintenance cost 4.0000000 1.0000000 4.0000000 3.0000000 _
environmental impacts 1.0000000 0.2500000 1.0000000 0.5000000 _
construction cost 2.0000000 0.3333333 2.0000000 1.0000000 _
priority 0.1209627 0.5384703 0.1209627 0.2196042 0.01

We can customize our table, highlighting the main information in gray:

require(magrittr)
require(kableExtra)
knitr::kable(as.data.frame(table), align = 'c', digits = 2) %>%
row_spec(1, italic = TRUE, background = 'gray') %>%
row_spec(2:5, color = 'black', background = 'yellow') %>%
row_spec(6, underline = TRUE,  color = 'black',background = 'gray',bold = TRUE,) %>%
column_spec(6, background = 'gray')
…1 life cycle maintenance cost environmental impacts construction cost CR
weights 2.00 5.00 2.00 3.00 _
life cycle 1.00 0.25 1.00 0.50 _
maintenance cost 4.00 1.00 4.00 3.00 _
environmental impacts 1.00 0.25 1.00 0.50 _
construction cost 2.00 0.33 2.00 1.00 _
priority 0.12 0.54 0.12 0.22 0.01

M2 -Judgment matrix of alternatives in relation to criterion C1 - life cycle Weights assigned by evaluators for each Alternative: w1= 1, w2 = 3

x = c("bridge", "tunnel") #criteria life cycle
y = c(1,3) #weights
m2 = matrix_ahp(x,y)
m2
#>        bridge    tunnel
#> bridge      1 0.3333333
#> tunnel      3 1.0000000
names(y) = x
table=tabela_holistica(pesos=y)
table
#> # A tibble: 4 × 4
#>   ...1     bridge tunnel CR
#>   <chr>     <dbl>  <dbl> <chr>
#> 1 weights    1     3     _
#> 2 bridge     1     0.333 _
#> 3 tunnel     3     1     _
#> 4 priority   0.25  0.75  0

M3 -Judgment matrix of alternatives in relation to criterion C2 - maintenance cost Weights assigned by evaluators for each Alternative: w1= 1, w2 = 4

x = c("bridge", "tunnel") #criteria maintenance cost
y = c(1,4) #weights
m3 = matrix_ahp(x,y)
m3
#>        bridge tunnel
#> bridge      1   0.25
#> tunnel      4   1.00
names(y) = x
table=tabela_holistica(pesos=y)
table
#> # A tibble: 4 × 4
#>   ...1     bridge tunnel CR
#>   <chr>     <dbl>  <dbl> <chr>
#> 1 weights     1     4    _
#> 2 bridge      1     0.25 _
#> 3 tunnel      4     1    _
#> 4 priority    0.2   0.8  0

M4 -Judgment matrix of alternatives in relation to criterion C3 - environmental impacts Weights assigned by evaluators for each Alternative: w1= 1, w2 = 2

x = c("bridge", "tunnel") #criteria environmental impacts
y = c(1,2) #weights
m4 = matrix_ahp(x,y)
m4
#>        bridge tunnel
#> bridge      1    0.5
#> tunnel      2    1.0
names(y) = x
table=tabela_holistica(pesos=y)
table
#> # A tibble: 4 × 4
#>   ...1     bridge tunnel CR
#>   <chr>     <dbl>  <dbl> <chr>
#> 1 weights   1      2     _
#> 2 bridge    1      0.5   _
#> 3 tunnel    2      1     _
#> 4 priority  0.333  0.667 0

M5 -Judgment matrix of alternatives in relation to criterion C4 - construction cost Weights assigned by evaluators for each Alternative: w1= 5, w2 = 3

x = c("bridge", "tunnel") #criteria construction cost
y = c(5,3) #weights
m5 = matrix_ahp(x,y)
m5
#>           bridge tunnel
#> bridge 1.0000000      3
#> tunnel 0.3333333      1
names(y) = x
table=tabela_holistica(pesos=y)
table
#> # A tibble: 4 × 4
#>   ...1     bridge tunnel CR
#>   <chr>     <dbl>  <dbl> <chr>
#> 1 weights   5       3    _
#> 2 bridge    1       3    _
#> 3 tunnel    0.333   1    _
#> 4 priority  0.75    0.25 0

## Consistency index and ratio

If $$a_{ij}$$ represents the importance of alternative i over alternative j and $$a_{jk}$$ represents the importance of alternative j over alternative k and $$a_{ik}$$, the importance of alternative i over alternative k, must equal $$a_{ij}a_{jk}$$ or $$a_{ij}a_{jk} = a_{ik}$$ for the judgments to be consistent.

The consistency index of a matrix of comparisons is given by $$ic = (\lambda_{max} - n)/(n - 1)$$. The consistency ratio (RC) is obtained by comparing the C.I. with the appropriate one of the following set of numbers (See Table 1.2) each of which is an average random consistency index derived from a sample of randomly generated reciprocal matrices using the scale 1/9, 1/8,…, 1,…, 8, 9. If it is not less than 0.10, study the problem and revise the judgments.

#consistency index
CI(m1)
#>  0.006873396
CI(m2)
#>  0
CI(m3)
#>  0
CI(m4)
#>  0
CI(m5)
#>  0
#consistency ratio
CR(m1)
#>  0.007637107
CR(m2)
#>  0
CR(m3)
#>  0
CR(m4)
#>  0
CR(m5)
#>  0

All the consistency ratio are less than 0.1, therefore all judgment matrices are considered consistent.

## Priority vectors


lista = list(m1, m2, m3, m4, m5)
#> []
#>  0.1209627 0.5384703 0.1209627 0.2196042
#>
#> []
#>  0.25 0.75
#>
#> []
#>  0.2 0.8
#>
#> []
#>  0.3333333 0.6666667
#>
#> []
#>  0.75 0.25

Each vector shows the weight of the criterio or alternative relative to the corresponding judgment matrix.

For example, the first vector matches the m1 matrix, so it provides the relative weights of each criteria: 0.12 for criteria 1; 0.54 for criteria 2, 0.12 for criteria 3 and 0.22 for criteria 4. The second vector corresponds to the m2 matrix, so it provides the weights of each alternative when considering criterion 1: 0.25 for alternative 1 and 0.75 for alternative 2, and so on.

## Problem has only one level of criteria

Let be a problem with m alternatives, $$A_1, A_2, ..., A_m$$ and n criteria $$C_1, C_2, ..., C_n$$.

The first matrix produces $$P(C_i)$$ = priority of the ith criterion for i = 1, 2, …, n

The second until n+1 matrix produces $$P(A_j|C_i)$$ = priority of the j-th alternative conditional on the i-th criterion in case the problem has only one level of criteria, j=1, 2, …, m and i=1, 2, …,n. In this case

$$P(A_j) = \sum_{i=1}^{n}P(A_j|C_i)P(C_i)$$, $$j=1, 2, ...,m$$

The function ahp_geral() will provide a table containing the marginal weights of each criterion, the conditional weights of each alternative given a certain criterion, the global weights of each alternative and a consistency ratio CR.

Criteria Weights A1 A2 Am CR
Alternatives -> 1 $$P(A1)$$ $$P(A2)$$ $$P(Am)$$
$$C1$$ $$P(C1)$$ $$P(A1|C1)P(C1)$$ $$P(A2|C1)P(C1)$$ $$P(Am|C1)P(C1)$$ $$CR(M_1)$$
$$C2$$ $$P(C2)$$ $$P(A1|C2)P(C2)$$ $$P(A2|C2)P(C2)$$ $$P(Am|C2)P(C2)$$ $$CR(M_2)$$
$$Cn$$ $$P(Cn)$$ $$P(A1|Cn)P(Cn)$$ $$P(A2|Cn)P(Cn)$$ $$P(Am|Cn)P(Cn)$$ $$CR(M_{n+1}$$)

Observe that

$$\sum_{j=1}^{m}P(A_j) =1$$, $$\sum_{i=1}^{n}P(C_i) =1$$, $$P(A_j) = \sum_{i=1}^{n}P(A_j|C_i)P(C_i)$$, $$j=1, 2, ...,m$$

The alternative with the highest priority value may be the decision maker’s final choice.

## Hierarchic Synthesis and Rank

Hierarchic synthesis is obtained by a process of weighting and adding down the hierarchy leading to a multilinear form.

### Example 2: Problem with 4 criteria and 2 alternatives

lista
#> []
#>                       life cycle maintenance cost environmental impacts
#> life cycle                     1        0.2500000                     1
#> maintenance cost               4        1.0000000                     4
#> environmental impacts          1        0.2500000                     1
#> construction cost              2        0.3333333                     2
#>                       construction cost
#> life cycle                          0.5
#> maintenance cost                    3.0
#> environmental impacts               0.5
#> construction cost                   1.0
#>
#> []
#>        bridge    tunnel
#> bridge      1 0.3333333
#> tunnel      3 1.0000000
#>
#> []
#>        bridge tunnel
#> bridge      1   0.25
#> tunnel      4   1.00
#>
#> []
#>        bridge tunnel
#> bridge      1    0.5
#> tunnel      2    1.0
#>
#> []
#>           bridge tunnel
#> bridge 1.0000000      3
#> tunnel 0.3333333      1
ahp_geral(lista)
#> # A tibble: 5 × 5
#>   Criteria        Weights      A      B      CR
#>   <chr>             <dbl>  <dbl>  <dbl>   <dbl>
#> 1 ---Alternatives   1     0.343  0.657  0.00764
#> 2 --C1              0.121 0.0302 0.0907 0
#> 3 --C2              0.538 0.108  0.431  0
#> 4 --C3              0.121 0.0403 0.0806 0
#> 5 --C4              0.220 0.165  0.0549 0

### Example 3: Problem with 5 criteria and 3 alternatives

x=paste0(letters,1:5) #criteria names C1, C2, ..., C5
y=c(5,2,7,3,2) #judgments
m1=matrix_ahp(x,y)
x=paste0(letters,1:3) #alternatives names A1, A2, A3
y=c(4.4,5.2,3)
m2=matrix_ahp(x,y)
y=c(2,4,3)
m3=matrix_ahp(x,y)
y=c(4.9,5,3.3)
m4=matrix_ahp(x,y)
y=c(4.4,4.2,4.3)
m5=matrix_ahp(x,y)
y=c(5.4,5.2,5.7)
m6=matrix_ahp(x,y)
base=list(m1, m2, m3, m4, m5, m6)
base
#> []
#>           c1 c2        c3  c4 c5
#> c1 1.0000000  4 0.3333333 3.0  4
#> c2 0.2500000  1 0.1666667 0.5  1
#> c3 3.0000000  6 1.0000000 5.0  6
#> c4 0.3333333  2 0.2000000 1.0  2
#> c5 0.2500000  1 0.1666667 0.5  1
#>
#> []
#>           a1        a2  a3
#> a1 1.0000000 0.5555556 2.4
#> a2 1.8000000 1.0000000 3.2
#> a3 0.4166667 0.3125000 1.0
#>
#> []
#>    a1        a2  a3
#> a1  1 0.3333333 0.5
#> a2  3 1.0000000 2.0
#> a3  2 0.5000000 1.0
#>
#> []
#>           a1        a2  a3
#> a1 1.0000000 0.9090909 2.6
#> a2 1.1000000 1.0000000 2.7
#> a3 0.3846154 0.3703704 1.0
#>
#> []
#>           a1  a2        a3
#> a1 1.0000000 1.2 1.1000000
#> a2 0.8333333 1.0 0.9090909
#> a3 0.9090909 1.1 1.0000000
#>
#> []
#>           a1  a2        a3
#> a1 1.0000000 1.2 0.7692308
#> a2 0.8333333 1.0 0.6666667
#> a3 1.3000000 1.5 1.0000000

#> []
#>  0.25002869 0.06547834 0.50960586 0.10940877 0.06547834
#>
#> []
#>  0.3237132 0.5272156 0.1490712
#>
#> []
#>  0.1634241 0.5396146 0.2969613
#>
#> []
#>  0.4046683 0.4366744 0.1586573
#>
#> []
#>  0.3646121 0.3030041 0.3323838
#>
#> []
#>  0.3197529 0.2699673 0.4102798
lapply(base,tabela_holistica) #fornece uma tabela com a matriz de comparação o vetor prioridade e o CR.
#> []
#>       ...1        c1         c2        c3        c4         c5   CR
#> 1       c1 1.0000000 4.00000000 0.3333333 3.0000000 4.00000000    _
#> 2       c2 0.2500000 1.00000000 0.1666667 0.5000000 1.00000000    _
#> 3       c3 3.0000000 6.00000000 1.0000000 5.0000000 6.00000000    _
#> 4       c4 0.3333333 2.00000000 0.2000000 1.0000000 2.00000000    _
#> 5       c5 0.2500000 1.00000000 0.1666667 0.5000000 1.00000000    _
#> 6 priority 0.2500287 0.06547834 0.5096059 0.1094088 0.06547834 0.02
#>
#> []
#>       ...1        a1        a2        a3   CR
#> 1       a1 1.0000000 0.5555556 2.4000000    _
#> 2       a2 1.8000000 1.0000000 3.2000000    _
#> 3       a3 0.4166667 0.3125000 1.0000000    _
#> 4 priority 0.3237132 0.5272156 0.1490712 0.01
#>
#> []
#>       ...1        a1        a2        a3   CR
#> 1       a1 1.0000000 0.3333333 0.5000000    _
#> 2       a2 3.0000000 1.0000000 2.0000000    _
#> 3       a3 2.0000000 0.5000000 1.0000000    _
#> 4 priority 0.1634241 0.5396146 0.2969613 0.01
#>
#> []
#>       ...1        a1        a2        a3 CR
#> 1       a1 1.0000000 0.9090909 2.6000000  _
#> 2       a2 1.1000000 1.0000000 2.7000000  _
#> 3       a3 0.3846154 0.3703704 1.0000000  _
#> 4 priority 0.4046683 0.4366744 0.1586573  0
#>
#> []
#>       ...1        a1        a2        a3 CR
#> 1       a1 1.0000000 1.2000000 1.1000000  _
#> 2       a2 0.8333333 1.0000000 0.9090909  _
#> 3       a3 0.9090909 1.1000000 1.0000000  _
#> 4 priority 0.3646121 0.3030041 0.3323838  0
#>
#> []
#>       ...1        a1        a2        a3 CR
#> 1       a1 1.0000000 1.2000000 0.7692308  _
#> 2       a2 0.8333333 1.0000000 0.6666667  _
#> 3       a3 1.3000000 1.5000000 1.0000000  _
#> 4 priority 0.3197529 0.2699673 0.4102798  0

ahp_geral(base)
#> # A tibble: 6 × 6
#>   Criteria        Weights      A      B      C         CR
#>   <chr>             <dbl>  <dbl>  <dbl>  <dbl>      <dbl>
#> 1 ---Alternatives  1      0.359  0.441  0.201  0.0195
#> 2 --C1             0.250  0.0809 0.132  0.0373 0.00863
#> 3 --C2             0.0655 0.0107 0.0353 0.0194 0.00793
#> 4 --C3             0.510  0.206  0.223  0.0809 0.000317
#> 5 --C4             0.109  0.0399 0.0332 0.0364 0.00000660
#> 6 --C5             0.0655 0.0209 0.0177 0.0269 0.000147

## Table

table1 = ahp_geral(base)
transforma_tabela(table1)
#> # A tibble: 6 × 6
#>   Criteria        Weights A      B      C      CR
#>   <chr>           <chr>   <chr>  <chr>  <chr>  <chr>
#> 1 ---Alternatives 100%    35.87% 44.05% 20.08% 1.95%
#> 2 --C1            25%     8.09%  13.18% 3.73%  0.86%
#> 3 --C2            6.55%   1.07%  3.53%  1.94%  0.79%
#> 4 --C3            50.96%  20.62% 22.25% 8.09%  0.03%
#> 5 --C4            10.94%  3.99%  3.32%  3.64%  0%
#> 6 --C5            6.55%   2.09%  1.77%  2.69%  0.01%
formata_tabela(table1)
Criteria Weights A B C CR
—Alternatives 100% 35.87% 44.05% 20.08% 1.95%
–C1 25% 8.09% 13.18% 3.73% 0.86%
–C2 6.55% 1.07% 3.53% 1.94% 0.79%
–C3 50.96% 20.62% 22.25% 8.09% 0.03%
–C4 10.94% 3.99% 3.32% 3.64% 0%
–C5 6.55% 2.09% 1.77% 2.69% 0.01%
formata_tabela(table1, cores = "GRAY")
Criteria Weights A B C CR
—Alternatives 100% 35.87% 44.05% 20.08% 1.95%
–C1 25% 8.09% 13.18% 3.73% 0.86%
–C2 6.55% 1.07% 3.53% 1.94% 0.79%
–C3 50.96% 20.62% 22.25% 8.09% 0.03%
–C4 10.94% 3.99% 3.32% 3.64% 0%
–C5 6.55% 2.09% 1.77% 2.69% 0.01%
formata_tabela(table1, cores = "WHITE")
Criteria Weights A B C CR
—Alternatives 100% 35.87% 44.05% 20.08% 1.95%
–C1 25% 8.09% 13.18% 3.73% 0.86%
–C2 6.55% 1.07% 3.53% 1.94% 0.79%
–C3 50.96% 20.62% 22.25% 8.09% 0.03%
–C4 10.94% 3.99% 3.32% 3.64% 0%
–C5 6.55% 2.09% 1.77% 2.69% 0.01%
ranque(table1)
#> # A tibble: 3 × 3
#>   Ranque Alternativas Pesos
#>    <int> <chr>        <dbl>
#> 1      1 B            0.441
#> 2      2 A            0.359
#> 3      3 C            0.201

## Criteria and sub-criteria

When the problem has one level of criteria and a second level of subcriteria, it will be necessary to map the hierarchical structure as follows:

Let $$n$$ be the number of criteria in the problem with $$m$$ alternatives and $$n_{i}$$ the number of sub-criteria of the ith criterion, so let’s define the mapping vector $$map = c(n_1, n_2, ..., n_n)$$.

This mapping must match the list of paired matrices $$M_1, M_2, ..., M_h$$, as follows:

• M1 must be an $$nxn$$ matrix comparing criteria
• If $$n_1=0$$ M2 must be an $$mxm$$ matrix comparing the alternatives; if not there should be a $$n_ixn_i$$ matrix comparing the sub-criteria in the light of criterion 1 and a sequence of $$n_1$$ $$mxm$$ matrices comparing the alternatives in the light of each sub-criterion of the current criterion, in this case there will be $$n_1+1$$ matrices: $$M_2, M_3, ..., M_{n1+2}$$
• For $$i, i=1,...,n$$, if $$n_i=0$$ the next matrix must be an $$mxm$$ matrix comparing the alternatives; if not there should be a $$n_ixn_i$$ matrix comparing the sub-criteria in the light of criterion i and a sequence of $$n_i$$ $$mxm$$ matrices comparing the alternatives in the light of each sub-criterion of the current criterion, in this case there will be $$n_i+1$$ matrices: $$M_{i1}, M_{i2}, ..., M_{ini+1}$$

For example suppose a problem with n=5 criteria, m=2 alternatives and $$n_1=0, n_2=2, n_3=4, n_4=0, n_5=0$$ the number of sub-criterion for each corresponding criterion. So,

M1 will be a 5x5 matrix comparing all five criteria

M2 will be a 2x2 matrix comparing all two alternatives in the light of criterion 1 because n1=0

M3 will be a 2x2 matrix comparing all two sub-criteria of criteria 2, because n2=2

M4 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 1 of criteria 2

M5 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 2 of criteria 2

M6 will be a 4x4 matrix comparing all four sub-criteria of criteria 3, because n3=4

M7 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 1 of criteria 3

M8 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 2 of criteria 3

M9 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 3 of criteria 3

M10 will be a 2x2 matrix comparing all two alternatives in the light of sub-criterion 4 of criteria 3

M11 will be a 2x2 matrix comparing all two alternatives in the light of criterion 4 because n4=0

M12 will be a 2x2 matrix comparing all two alternatives in the light of criterion 5 because n5=0

It is extremely important that the list of matrices be in this order because the method takes this matched mapping into account.

### Example 4: two criteria with two subcriteria

#two criteria, each with two subcriteria
map = c(2,2)
#x with names and y with holistic judgment
x=paste0(letters,1:2) #2 criteria
y=c(5,7)
m1=matrix_ahp(x,y) # matrix compare two criteria
x=paste0("SC1",1:2)
y=c(4,6)
m2=matrix_ahp(x,y) # matrix compare two subcriteria of criteria 1
x=paste0(letters,1:3)
y=c(2,4,5)
m3=matrix_ahp(x,y) #alternatives for subcriteria 1 - criteria 1
y=c(4.9,5, 2)
m4=matrix_ahp(x,y) #alternatives for subcriteria 2 - criteria 1
y=c(4.4,8, 6)
x=paste0("SC2",1:2)
m5=matrix_ahp(x,y) #matrix compare two subcriteria of criteria 2
y=c(5.4,5.2, 1)
x=paste0(letters,1:3)
m6=matrix_ahp(x,y) #alternatives for subcriteria 1 - criteria 2
y=c(9,5.2, 3)
m7=matrix_ahp(x,y) #alternatives for subcriteria 2 - criteria 2

base=list(m1, m2, m3, m4, m5, m6, m7)
base
#> []
#>    c1        c2
#> c1  1 0.3333333
#> c2  3 1.0000000
#>
#> []
#>      SC11      SC12
#> SC11    1 0.3333333
#> SC12    3 1.0000000
#>
#> []
#>    a1        a2   a3
#> a1  1 0.3333333 0.25
#> a2  3 1.0000000 0.50
#> a3  4 2.0000000 1.00
#>
#> []
#>           a1        a2  a3
#> a1 1.0000000 0.9090909 3.9
#> a2 1.1000000 1.0000000 4.0
#> a3 0.2564103 0.2500000 1.0
#>
#> []
#>      SC21      SC22
#> SC21  1.0 0.2173913
#> SC22  4.6 1.0000000
#>
#> []
#>           a1        a2  a3
#> a1 1.0000000 1.2000000 5.4
#> a2 0.8333333 1.0000000 5.2
#> a3 0.1851852 0.1923077 1.0
#>
#> []
#>           a1     a2  a3
#> a1 1.0000000 4.8000 7.0
#> a2 0.2083333 1.0000 3.2
#> a3 0.1428571 0.3125 1.0

## Problem has two levels of criteria

Let be a problem with m alternatives, $$A_1, A_2, ..., A_m$$, n criteria $$C_1, C_2, ..., C_n$$ with n_k_i sub-criteria corresponding to the ith criterion.

The first matrix produces $$P(C_i)$$ or $$P(SCi_k|C_i)$$ = priority of the ith criterion or kth subcriterion of ith criterion for $$i = 1, 2, ..., n$$ and $$k = 1, ..., n_{i}$$.

The next matrices produce comparisons of criteria versus alternatives or criteria versus subcriteria followed by comparisons of alternatives versus each subcriteria corresponding to the parent criteria, according to the established mapping, $$map = c(n_{k1}, n_{k2}, ..., n_{kn})$$. We will consider two situations:

• For each criterion i with $$n_{i}$$ > 0 we will have $$n_i$$ subcriteria $$SC_{i1}, SC_{i2},...SC_{in_i}$$:

$$C_i = SC_{i1}\cup SC_{i2}\cup...\cup SC_{in_i}$$

$$P(SC_{ik}) = P(SC_{ik}|C_i)P(C_i)$$, $$k=1, 2, ...,n_{i}, i=1,2,...n$$

$$P(C_i) = \sum_{k=1}^{n_{i}}P(SC_{ik})$$, $$i=1, 2, ...,n$$

$$P(A_j|C_i) = \sum_{k=1}^{n_i}P(Aj|SCik)P(SCik|Ci)$$

$$P(A_j) = \sum_{i=1}^{n}P(A_j|C_i)P(C_i)$$, $$j=1, 2, ...,m$$

• For otherwise (each criterion i with $$n_{i}$$ = 0) we have the same expression for a one level of criterion:

$$P(A_j) = \sum_{i=1}^{n}P(A_j|C_i)P(C_i)$$, $$j=1, 2, ...,m$$

The function ahp_s() will provide a table containing the marginal weights of each criterion/subcriterion, the conditional weights of each alternative given a certain criterion/sucriterion and the global weights of each alternative.

Criteria Weights A1 A2 Am CR
Alternatives -> 1 $$P(A1)$$ $$P(A2)$$ $$P(Am)$$ $$CR(M_1)$$
$$SC_{11}$$ $$P(SC_{11}|C1)$$ $$P(A1|SC_{11})P(SC_{11}|C1)$$ $$P(A2|SC11)P(SC11|C1)$$ $$P(Am|SC11)P(SC11|C1)$$ $$CR(M_3)$$
$$SC_{12}$$ $$P(SC_{12}|C1)$$ $$P(A1|SC_{12})P(SC_{12}|C1)$$ $$P(A2|SC_{12})P(SC_{12}|C1)$$ $$P(Am|SC_{12})P(SC_{12}|C1)$$ $$CR(M_4)$$
$$SC_{1n_1}$$ $$P(SC_{1n_1}|C1)$$ $$P(A1|SC_{1n_1})P(SC_{1n_1}|C1)$$ $$P(A2|SC_{1n_1})P(SC_{1n_1}|C1)$$ $$P(Am|SC_{1n_1})P(SC_{1n_1}|C1)$$ $$CR(M_{2+n_1})$$
$$C1$$ $$P(C1)$$ $$P(A1|C1)P(C1)$$ $$P(A2|C1)P(C1)$$ $$P(Am|C1)P(C1)$$ $$CR(M_{2})$$
$$Cn$$ $$P(Cn)$$ $$P(A1|Cn)P(Cn)$$ $$P(A2|Cn)P(Cn)$$ $$P(Am|Cn)P(Cn)$$ $$CR(M_{1+n+n_1+...+n_n})$$

Observe that

$$\sum_{j=1}^{m}P(A_j) =1$$, $$\sum_{i=1}^{n}P(C_i) =1$$, $$\sum_{k=1}^{n_i}P(SC_{ik}|Ci) =1$$, $$P(C_i) = \sum_{k=1}^{n_{i}}P(SC_{ik})$$, $$i=1, 2, ...,n$$ and $$P(A_j) = \sum_{i=1}^{n}P(A_j|C_i)P(C_i)$$, $$j=1, 2, ...,m$$

The alternative with the highest priority value may be the decision maker’s final choice.

## Hierarchic Synthesis and Rank

Hierarchic synthesis is obtained by a process of weighting and adding down the hierarchy leading to a multilinear form.

### Example 5: Problem with 2 criteria, two subcriteria and 3 alternatives

#Priority vector and CR
#
#> []
#>  0.25 0.75
#>
#> []
#>  0.25 0.75
#>
#> []
#>  0.1219572 0.3196183 0.5584245
#>
#> []
#>  0.4278814 0.4598156 0.1123030
#>
#> []
#>  0.1785714 0.8214286
#>
#> []
#>  0.48757884 0.42637711 0.08604404
#>
#> []
#>  0.72429277 0.19608181 0.07962542
lapply(base,tabela_holistica) #fornece uma tabela com a matriz de comparação o vetor prioridade e o CR.
#> []
#>       ...1   c1        c2 CR
#> 1       c1 1.00 0.3333333  _
#> 2       c2 3.00 1.0000000  _
#> 3 priority 0.25 0.7500000  0
#>
#> []
#>       ...1 SC11      SC12 CR
#> 1     SC11 1.00 0.3333333  _
#> 2     SC12 3.00 1.0000000  _
#> 3 priority 0.25 0.7500000  0
#>
#> []
#>       ...1        a1        a2        a3   CR
#> 1       a1 1.0000000 0.3333333 0.2500000    _
#> 2       a2 3.0000000 1.0000000 0.5000000    _
#> 3       a3 4.0000000 2.0000000 1.0000000    _
#> 4 priority 0.1219572 0.3196183 0.5584245 0.02
#>
#> []
#>       ...1        a1        a2       a3 CR
#> 1       a1 1.0000000 0.9090909 3.900000  _
#> 2       a2 1.1000000 1.0000000 4.000000  _
#> 3       a3 0.2564103 0.2500000 1.000000  _
#> 4 priority 0.4278814 0.4598156 0.112303  0
#>
#> []
#>       ...1      SC21      SC22 CR
#> 1     SC21 1.0000000 0.2173913  _
#> 2     SC22 4.6000000 1.0000000  _
#> 3 priority 0.1785714 0.8214286  0
#>
#> []
#>       ...1        a1        a2         a3 CR
#> 1       a1 1.0000000 1.2000000 5.40000000  _
#> 2       a2 0.8333333 1.0000000 5.20000000  _
#> 3       a3 0.1851852 0.1923077 1.00000000  _
#> 4 priority 0.4875788 0.4263771 0.08604404  0
#>
#> []
#>       ...1        a1        a2         a3   CR
#> 1       a1 1.0000000 4.8000000 7.00000000    _
#> 2       a2 0.2083333 1.0000000 3.20000000    _
#> 3       a3 0.1428571 0.3125000 1.00000000    _
#> 4 priority 0.7242928 0.1960818 0.07962542 0.06

ahp_s(base,map)
#> # A tibble: 7 × 6
#>   criteria       Weithts     A1     A2     A3       CR
#>   <chr>            <dbl>  <dbl>  <dbl>  <dbl>    <dbl>
#> 1 Alternatives->   1     0.599  0.284  0.117  0
#> 2 --SC11           0.25  0.0305 0.0799 0.140  0.0158
#> 3 --SC12           0.75  0.321  0.345  0.0842 0.000469
#> 4 -C1              0.25  0.0879 0.106  0.0560 0
#> 5 --SC21           0.179 0.0871 0.0761 0.0154 0.00200
#> 6 --SC22           0.821 0.595  0.161  0.0654 0.0595
#> 7 -C2              0.75  0.512  0.178  0.0606 0

tb = ahp_s(base,map)

transforma_tabela(tb)
#> # A tibble: 7 × 6
#>   criteria       Weithts A1     A2     A3     CR
#>   <chr>          <chr>   <chr>  <chr>  <chr>  <chr>
#> 1 Alternatives-> 100%    59.94% 28.41% 11.65% 0%
#> 2 --SC11         25%     3.05%  7.99%  13.96% 1.58%
#> 3 --SC12         75%     32.09% 34.49% 8.42%  0.05%
#> 4 -C1            25%     8.79%  10.62% 5.6%   0%
#> 5 --SC21         17.86%  8.71%  7.61%  1.54%  0.2%
#> 6 --SC22         82.14%  59.5%  16.11% 6.54%  5.95%
#> 7 -C2            75%     51.15% 17.79% 6.06%  0%

formata_tabela(tb)
criteria Weithts A1 A2 A3 CR
Alternatives-> 100% 59.94% 28.41% 11.65% 0%
–SC11 25% 3.05% 7.99% 13.96% 1.58%
–SC12 75% 32.09% 34.49% 8.42% 0.05%
-C1 25% 8.79% 10.62% 5.6% 0%
–SC21 17.86% 8.71% 7.61% 1.54% 0.2%
–SC22 82.14% 59.5% 16.11% 6.54% 5.95%
-C2 75% 51.15% 17.79% 6.06% 0%

## Comparing ahp_geral and ahp_s with one level

The ahp_geral() function constructs the summary table equal from ahp_s, for problem with no subcriteria. Anyway, both produce the criteria and alternative weights, in this respect the functions return the same value when problem has one level of criteria. We recommend using ahp_s when the problem has subcriteria.

### Example 6

Consider the problem with 6 criteria and 4 alternatives

p1=c(2,4,5,1,6,3) #holistcs weights for compare 6 criteria
p2=c(5, 4, 6, 7) #holistcs weights for compare 4 alternatives for criterion 1
p3=c(2, 8, 2, 7) #holistcs weights for compare 4 alternatives for criterion 2
p4=c(5, 1, 4, 1) #holistcs weights for compare 4 alternatives for criterion 3
p5=c(3.4, 4, 2, 3) #holistcs weights for compare 4 alternatives for criterion 4
p6=c(6, 4, 2, 2.5) #holistcs weights for compare 4 alternatives for criterion 5
p7=c(5, 3, 6, 1.8) #holistcs weights for compare 4 alternatives for criterion 6

x1=paste0("C",1:6)
x= paste0("A",1:4)

m1 = matrix_ahp(x1,p1)
m2 = matrix_ahp(x,p2)
m3 = matrix_ahp(x,p3)
m4 = matrix_ahp(x,p4)
m5 = matrix_ahp(x,p5)
m6 = matrix_ahp(x,p6)
m7 = matrix_ahp(x,p7)

base=list(m1,m2, m3, m4, m5, m6, m7)
formata_tabela(ahp_geral(base))
Criteria Weights A B C D CR
—Alternatives 100% 40.13% 23.5% 19.61% 16.77% 1.98%
–C1 6.41% 1.03% 0.61% 1.78% 2.99% 1.15%
–C2 15.96% 1.04% 8.4% 1.04% 5.48% 1.35%
–C3 25.04% 12.62% 2.23% 7.97% 2.23% 1.03%
–C4 4.28% 1.17% 1.72% 0.5% 0.89% 0.42%
–C5 38.25% 21.19% 9.27% 3.37% 4.42% 1.74%
–C6 10.06% 3.09% 1.28% 4.96% 0.74% 2.04%
formata_tabela(ahp_s(base, map=c(0,0,0,0,0,0)))
criteria Weithts A1 A2 A3 A4 CR
Alternatives-> 100% 40.13% 23.5% 19.61% 16.77% 1.98%
-C1 6.41% 1.03% 0.61% 1.78% 2.99% 1.15%
-C2 15.96% 1.04% 8.4% 1.04% 5.48% 1.35%
-C3 25.04% 12.62% 2.23% 7.97% 2.23% 1.03%
-C4 4.28% 1.17% 1.72% 0.5% 0.89% 0.42%
-C5 38.25% 21.19% 9.27% 3.37% 4.42% 1.74%
-C6 10.06% 3.09% 1.28% 4.96% 0.74% 2.04%