--- title: "Introduction to AHPWR package" author: "Luciane Ferreira Alcoforado and Orlando Celso Longo" subtitle: "Academia da Força Aérea and Universidade Federal Fluminense" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Introduction to AHP package} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup} knitr::opts_chunk$set(warning = FALSE, message = FALSE) library(AHPWR) library(kableExtra) ``` 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: ```{r} #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: ```{r} #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="") ``` ```{r} #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() ``` ```{r} #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. ```{r echo=FALSE} `Intensity of importance` = 1:9 Definicion = c("Equal Importance", " Weak", "Moderate importance", "Moderate plus", "Strong importance", "Strong plus", "Very strong or demonstrated importance", "Very, very strong", "Extreme importance") tab = data.frame(`Intensity of importance`, Definicion) knitr::kable(tab, caption = "Table 1: The fundamental Scale") ``` 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. ```{r} x = c("life cycle", "maintenance cost", "environmental impacts", "construction cost") #criteria y = c(2,5,2,3) #weights m1 = matrix_ahp(x,y) m1 ``` 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. ```{r} names(y) = x table=tabela_holistica(pesos=y) table knitr::kable(table) ``` We can customize our table, highlighting the main information in gray: ```{r} 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') ``` M2 -Judgment matrix of alternatives in relation to criterion C1 - life cycle Weights assigned by evaluators for each Alternative: w1= 1, w2 = 3 ```{r} x = c("bridge", "tunnel") #criteria life cycle y = c(1,3) #weights m2 = matrix_ahp(x,y) m2 ``` ```{r} names(y) = x table=tabela_holistica(pesos=y) table ``` M3 -Judgment matrix of alternatives in relation to criterion C2 - maintenance cost Weights assigned by evaluators for each Alternative: w1= 1, w2 = 4 ```{r} x = c("bridge", "tunnel") #criteria maintenance cost y = c(1,4) #weights m3 = matrix_ahp(x,y) m3 ``` ```{r} names(y) = x table=tabela_holistica(pesos=y) table ``` M4 -Judgment matrix of alternatives in relation to criterion C3 - environmental impacts Weights assigned by evaluators for each Alternative: w1= 1, w2 = 2 ```{r} x = c("bridge", "tunnel") #criteria environmental impacts y = c(1,2) #weights m4 = matrix_ahp(x,y) m4 ``` ```{r} names(y) = x table=tabela_holistica(pesos=y) table ``` M5 -Judgment matrix of alternatives in relation to criterion C4 - construction cost Weights assigned by evaluators for each Alternative: w1= 5, w2 = 3 ```{r} x = c("bridge", "tunnel") #criteria construction cost y = c(5,3) #weights m5 = matrix_ahp(x,y) m5 ``` ```{r} names(y) = x table=tabela_holistica(pesos=y) table ``` ## 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. ```{r} #consistency index CI(m1) CI(m2) CI(m3) CI(m4) CI(m5) ``` ```{r} #consistency ratio CR(m1) CR(m2) CR(m3) CR(m4) CR(m5) ``` All the consistency ratio are less than 0.1, therefore all judgment matrices are considered consistent. ## Priority vectors ```{r} lista = list(m1, m2, m3, m4, m5) calcula_prioridades(lista) ``` 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 ```{r} lista ahp_geral(lista) ``` ### Example 3: Problem with 5 criteria and 3 alternatives ```{r} x=paste0(letters[3],1:5) #criteria names C1, C2, ..., C5 y=c(5,2,7,3,2) #judgments m1=matrix_ahp(x,y) x=paste0(letters[1],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 calcula_prioridades(base) #fornece somente os vetores prioridades lapply(base,tabela_holistica) #fornece uma tabela com a matriz de comparação o vetor prioridade e o CR. ahp_geral(base) ``` ## Table ```{r} table1 = ahp_geral(base) transforma_tabela(table1) ``` ```{r} formata_tabela(table1) formata_tabela(table1, cores = "GRAY") formata_tabela(table1, cores = "WHITE") ``` ```{r} ranque(table1) ``` ## 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 ```{r} #two criteria, each with two subcriteria map = c(2,2) #x with names and y with holistic judgment x=paste0(letters[3],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],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],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 ``` ## 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 ```{r} #Priority vector and CR # calcula_prioridades(base) #fornece somente os vetores prioridades lapply(base,tabela_holistica) #fornece uma tabela com a matriz de comparação o vetor prioridade e o CR. ahp_s(base,map) tb = ahp_s(base,map) transforma_tabela(tb) formata_tabela(tb) ``` ## 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 ```{r} 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)) formata_tabela(ahp_s(base, map=c(0,0,0,0,0,0))) ``` ## References Alcoforado, L.F. (2021) Utilizando a Linguagem R: conceitos, manipulação, visualização, Modelagem e Elaboração de Relatório, Alta Books, Rio de Janeiro. Godoi, W.C. (2014). Método de construção das matrizes de julgamento paritário no AHP – método de julgamento holístico. Revista Gestão Industrial, ISSN 1808-0448 / v. 10, n. 03: p.474- 493, D.O.I: 10.3895/gi.v10i3.1970 Longo, O.C., Alcoforado, L.F., Levy, A (2022). Utilização do pacote AHP na tomada de decisão. In IX Xornada de Usuarios de R en Galicia, Oliveira, L.S., AHP, Github.com. (2020) URL = https://github.com/Lyncoln/AHP, Acesso em 20/09/2022. Oliveira, L.S., Alcoforado, L.F., Ross, S.D., Simão, A.S. (2019). Implementando a AHP com R. Anais do SER, ISSN 2526-7299, v.4, n.2. URL: https://periodicos.uff.br/anaisdoser/article/view/29331 Saaty, T.L., Vargas, L.G. (2012), Models, Methods, Concepts and Applications of the Analytic Hierarchy Process, Second Edition, Springer, New York. Triantaphyllou, E., Shu, B., Nieto Sanchez, S., Ray, T. (1998). Multi-Criteria Decision Making: An Opera-tions Research Approach. Encyclopedia of Electrical and Electronics Engineering, (J.G. Webster, Ed.), John Wiley & Sons, New York, NY, Vol. 15, pp. 175-186. ```{r echo=FALSE} #para checar o pacote #devtools::check(args = c("--as-cran"), check_dir = dirname(getwd())) ```