Analytic Hierarchy Process for Survey Data in R

Vignettes for the ahpsurvey package (ver 0.4.0)

based on Frankie Cho paper

Department of Geography, The University of Hong Kong, Pokfulam Road, Hong Kong

24 November 2019

Introduction

The Analytic Hierarchy Process (AHP), introduced by Saaty (1987), is a versatile multi-criteria decision-making tool that allows individuals to rationally weigh attributes and evaluate alternatives presented to them. While most applications of the AHP are focused on implementation at the individual or small-scale, the AHP was increasingly adopted in survey designs, which involve a large number of decision-makers and a great deal of heterogeneity in responses. The tools currently available in R for the analysis of AHP data, such as the packages ahp by Glur (2018) and Prize by Dargahi (2016), are excellent tools for performing the AHP at a small scale and offers are excellent in terms of interactivity, user-friendliness, and for comparing alternatives.

However, researchers looking to adopt the AHP in the analysis of survey data often have to manually reformat their data, sometimes even involving dragging and copying across Excel spreadsheets, which is painstaking and prone to human error. Hitherto, there are no good ways of computing and visualising the heterogeneity amongst AHP decision-makers, which is common in survey data. Inconsistent choices are also prevalent in AHP conducted in the survey format, where it is impractical for enumerators to identify and correct for inconsistent responses on the spot when the surveys are delivered in paper format. Even if an electronic version that allows immediate feedback of consistency ratio is used, respondents asked to repeatedly change their answers are likely to be mentally fatigued. Censoring observations with inconsistency is likely to result in a greatly decreased statistical power of the sample, or may lead to unrepresentative samples and nonresponse bias.

The ahpsurvey package provides a workflow for researchers to quantify and visualise inconsistent pairwise comparisons that aids researchers in improving the AHP design and adopting appropriate analytical methods for the AHP.

Install the package directly from CRAN:

install.packages("ahpsurvey")

And load the ahpsurvey library.

library(ahpsurvey)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.4.2     ✔ purrr   1.0.2
## ✔ tibble  3.2.1     ✔ dplyr   1.1.2
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

The Saaty AHP and the toy dataset

A gentle introduction of the AHP survey methodology:

Rating Scale Definition

Rating Meaning
1 Two characteristics are equally important
2 Between 1 and 3
3 The preferred characteristics are slightly more important
4 Between 3 and 5
5 The preferred characteristics are moderately more important
6 Between 5 and 7
7 The preferred characteristics are strongly more important
8 Between 7 and 9
9 The preferred characteristics are absolutely more important

A Saaty scale is composed of 9 items on each end (17 options per pairwise comparison) where decision-makers are asked to indicate how much attribute/ characteristic A is more preferred to B (or vice versa), and how much it is preferred in a 9-point scale. Respondents are asked to make pairwise comparisons for a range of attributes, and indicate their priorities for each of them.

Afterwards, we load the data needed, city200, which consists of randomly generated data of 200 individuals based on the weights provided in Saaty (2004). The methodology of data generation is explained at the end of this vignette.

atts <- c("cult", "fam", "house", "jobs", "trans")
data(city200)
head(city200)
##   cult_fam cult_house cult_jobs cult_trans fam_house fam_jobs fam_trans
## 1        2         -2         2         -6        -4       -4        -8
## 2        2         -4         1         -4        -4       -2        -8
## 3        4         -2         1         -3        -7       -3        -5
## 4        8         -4         3         -4        -8        1        -7
## 5        3         -3         5         -6        -8        1        -4
## 6        6         -4         2         -4        -7       -2        -4
##   house_jobs house_trans jobs_trans
## 1          4          -3         -8
## 2          4          -3         -7
## 3          4          -3         -6
## 4          4          -3         -9
## 5          4          -3         -6
## 6          4          -3         -6

Individual and Aggregated priorities

Creating pairwise comparison matrices

Based on the Saaty scale, a pairwise comparison matrix of N attributes for the \(k^{th}\) individual is obtained:

\[ \begin{bmatrix} a_{1,1} & a_{1,2} & \dotsi & a_{1,N} \\ a_{2,1} & a_{2,2} & ... & a_{2,N} \\ \vdots & \vdots & a_{i,j} & \vdots \\ a_{N,1} & a_{N,2} & \dotsi & a_{N,N} \end{bmatrix} \]

Where \(ai,j\) represents the pairwise comparison between the attribute \(i\) and \(j\)

If \(i\) is more important than \(j\) for 6 units, \(a_{i,j}=6\) and \(a_{j,i}=\frac{1}{6}\) , i.e. the reciprocal. Data must be reformatted into this pairwise comparison matrix format to proceed.

The reformatting of the survey data (with one row per individual) into such a matrix necessary for further analysis is cumbersome for researchers. Furthermore, as researchers conducting the AHP as an integrated part of a survey, we typically receive data in the above format: the pairwise comparisons are coded in positive and negative numbers as opposed to reciprocals. In the pairwise comparison of cult_fam:

In the case where the decision-maker chose \(6\), the sensible codebook maker would code it as \(-6\), which denotes that Culture is more important than Family in \(6\) units for that decision-maker. For ahp.mat to work, the value in A_B variable have to be the importance A has over B in positive values. In this case, the values should be converted from negative to positive, and the negative values would be converted to its reciprocal in the pairwise matrix. When data is coded in the above way, set negconvert = TRUE. If the data is already coded in the reciprocal (as opposed to negatives), set reciprocal = FALSE.

Some caveats prior to entering the data into the ahp.mat function. ahp.mat does not recognise the names of the original dataframe, and figures out which attribute corresponds to which entirely based on the order of the columns. For example, when the attributes are \(A, B, C, and D\), the dataframe should be ordered in A_B, A_C, A_D, B_C, B_D, C_D, and the attributes listed as c(A,B,C,D), in that order.

ahp.mat takes four arguments:

Lets see an example

city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  head(3)
## [[1]]
##            cult   fam     house  jobs trans
## cult  1.0000000 0.500 2.0000000 0.500     6
## fam   2.0000000 1.000 4.0000000 4.000     8
## house 0.5000000 0.250 1.0000000 0.250     3
## jobs  2.0000000 0.250 4.0000000 1.000     8
## trans 0.1666667 0.125 0.3333333 0.125     1
## 
## [[2]]
##       cult   fam     house      jobs trans
## cult  1.00 0.500 4.0000000 1.0000000     4
## fam   2.00 1.000 4.0000000 2.0000000     8
## house 0.25 0.250 1.0000000 0.2500000     3
## jobs  1.00 0.500 4.0000000 1.0000000     7
## trans 0.25 0.125 0.3333333 0.1428571     1
## 
## [[3]]
##            cult       fam     house      jobs trans
## cult  1.0000000 0.2500000 2.0000000 1.0000000     3
## fam   4.0000000 1.0000000 7.0000000 3.0000000     5
## house 0.5000000 0.1428571 1.0000000 0.2500000     3
## jobs  1.0000000 0.3333333 4.0000000 1.0000000     6
## trans 0.3333333 0.2000000 0.3333333 0.1666667     1

The ahp.mat function creates a list of pairwise comparison matrices for all decision-makers. As seen above, the pairwise matrices resembles the original Saaty criteria weights, which is a good sanity check.

Individual preference weights

The ahp.indpref function computes the individual priorities of the decision-makers, and returns a data.frame containing the preference weights of the decision-makers. The three arguments are as follows:

Four modes of finding the averages are available:

Here I demonstrate the difference of using arithmetic aggregation and dominant eigenvalue methods. In my own testing with real datasets, a much higher proportion of respondents have at least one attribute with a difference larger than 0.05 due to presence of inconsistent and heterogeneous responses.

cityahp <- city200 %>% 
  ahp.mat(atts, negconvert = T)
eigentrue <- ahp.indpref(cityahp, atts, method = "eigen")
geom <- ahp.indpref(cityahp, atts, method = "arithmetic")
error <- data.frame(id = 1:length(cityahp), maxdiff = apply(abs(eigentrue - geom), 1, max))
error %>%
  ggplot(aes(x = id, y = maxdiff)) +
  geom_point() +
  geom_hline(yintercept = 0.05, linetype = "dashed", color = "red") +
  geom_hline(yintercept = 0, color = "gray50") +
  scale_x_continuous("Respondent ID") +
  scale_y_continuous("Maximum difference") +
  theme_minimal()

Maximum difference of between eigenvalue and mean aggregation

Aggregated preference weights

The ahp.aggpref function computes the aggregated priorities of all decision-makers using the specified methods. The following arguments are given:

When tmean or tgmean is specified, ahpsurvey needs an additional argument qt, which specifes the quantile which the top and bottom preference weights are trimmed. \(qt = 0.25\) specifies that the aggregation is the arithmetic mean of the values from the 25 to 75 percentile. This visualisation offers researchers a good way to determine the amount of preference weights to be trimmed. By default, qt = 0, hence the result you would get by using tmean and tgmean and not specifying qt is the same as arithmetic and geometric respectively.

amean <- ahp.aggpref(cityahp, atts, method = "arithmetic")
amean
##       cult        fam      house       jobs      trans 
## 0.16200828 0.43673193 0.07607178 0.28274933 0.04243868

Two steps were simutaneously conducted in the above command:

1- Compute the individual priorities of each decision-maker (using method)

2- Aggregate the priorities (using aggmethod)

By default, the two steps rely on the same aggregation method as specified in method (unless when method = “eigen”, where aggmethod defaults to arithmetic). However, it is possible to specify different aggregation methods for the individual and group level. For instance, one can specify that in the individual level, the arithmetic mean is used to compute the individual priorities; the priorities are aggregated using a trimmed mean by trimming observations higher and lower quantile.

qtresults <- matrix(nrow = 50, ncol = 5, data = NA)
for (q in 1:50){
  qtresults[q,] <- ahp.aggpref(cityahp, atts, method = "arithmetic", 
                               aggmethod = "tmean", qt = (q-1)/100)
}
colnames(qtresults) <- atts
qtresults %>%
  as.data.frame() %>%
  mutate(trimperc = 1:nrow(qtresults)-1) %>%
  mutate(cult = cult - amean[1],
         fam = fam - amean[2],
         house = house - amean[3],
         jobs = jobs - amean[4],
         trans = trans - amean[5]) %>%
  gather(cult, fam, house, jobs, trans, key = "att", value = "weight") %>%
  ggplot(aes(x = trimperc, y = weight, group = att, shape = att, color = att, fill = att)) +
  geom_line() +
  geom_point() +
  scale_x_continuous("Quantile (from top and bottom) trimmed") +
  scale_y_continuous("Change from untrimmed mean") +
  geom_hline(yintercept = 0, color = "gray") +
  theme_minimal()

Changes of aggregated weights based on quantile of data trimmed

It is also possible to quantify the heterogeneity amongst decision-makers’ priorities, information possibly lost by group aggregation. This is specified using aggmethod = “sd”:

mean <- city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.aggpref(atts, method = "arithmetic")

sd <- city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.aggpref(atts, method = "arithmetic", aggmethod = "sd")

t(data.frame(mean, sd))%>% kable()
cult fam house jobs trans
mean 0.1620083 0.4367319 0.0760718 0.2827493 0.0424387
sd 0.0333849 0.0544975 0.0088232 0.0482966 0.0074665

Aggregated individual judgements

Similarly, ahp.aggjudge aggregates the individual judgements of all decision-makers to generate a row-standardised pairwise comparison matrix of all decision-makers. This allows one to compare priorities directly based on the aggregated pairwise judgements of all decision-makers. It takes the argument aggmethod with the exact same options as ahp.aggpref.

city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.aggjudge(atts, aggmethod = "geometric")
##            cult       fam     house      jobs    trans
## cult  1.0000000 0.2202027 3.0925191 0.4882218 4.638350
## fam   4.5412708 1.0000000 6.4612364 1.7035125 6.145824
## house 0.3233610 0.1547691 1.0000000 0.2488201 2.926539
## jobs  2.0482496 0.5870224 4.0189678 1.0000000 7.039173
## trans 0.2155939 0.1627121 0.3417005 0.1420621 1.000000

Measuring and visualising consistency

Measuring consistency

The consistency indices and consistency ratio of a given choice is defined by the following equation:

\[CR = (\frac{\lambda_{max}-n}{n -1 })* (\frac{1}{RI}) \]

Where \(λmax\) is the maximum eigenvalue of the pairwise comparison vector and \(n\) is the number of attributes. The \(RI\) when five attributes are present is 1.11. See the documentation for ahp.ri to generate your own RI based on a specific number of dimensions and random seed.

The RI below was generated from ahp.ri with 500000 simulations (which takes some time), as follows:

weight <- c(5,-3,2,-5,
            -7,-1,-7,
            4,-3,
            -7)
sample_mat <- ahp.mat(t(weight), atts, negconvert = TRUE)

(cr_std <- ahp.cr(sample_mat, atts))
## [1] 0.05081557

The ahp.cr function returns a vector of CR that can be merged to other dataframes as a measure of the individuals’ consistency.

cr <- city200 %>%
  ahp.mat(atts, negconvert = T) %>% 
  ahp.cr(atts)
table(cr <= 0.1)
## 
## FALSE  TRUE 
##    70   130

You may also specify your own random index generated with ahp.ri to be used with ahp.cr, as follows:

## Generate a random index with 1000 simulations, 5 dimensions and seed 30000 for reproducibility (seed = 42 by default).
(RI <- ahp.ri(nsims = 1000, dim = 5, seed = 30000))
## [1] 1.115356
## [1] 1.12
## Use this RI to calculate the consistency ratio instead of the default one.
ahp.cr(sample_mat, atts, RI)
## [1] 0.05048507
## [1] 0.0505

The processing time of ahp.ri increases exponentially as nsims increase, and unfortunately I haven’t written it to optimise for speed. Generally I wouldn’t go beyond 6 digits of nsims unless I have too much time lying around.

Visualising individual priorities and consistency ratios

The ahp.indpref function provides a detailed account of each individuals’ priorities and its corresponding weighting. An overlay of the violin density, boxplots and jitter plots is useful in visualising the heterogeneity in weights each respondent gives.

thres <- 0.1
dict <- c("cult" = "Culture", 
          "fam" = "Family", 
          "house" = "Housing", 
          "jobs" = "Jobs", 
          "trans" = "Transportation")

cr.df <- city200 %>%
  ahp.mat(atts, negconvert = TRUE) %>% 
  ahp.cr(atts) %>% 
  data.frame() %>%
  mutate(rowid = 1:length(cr), cr.dum = as.factor(ifelse(cr <= thres, 1, 0))) %>%
  select(cr.dum, rowid)

city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.indpref(atts, method = "eigen") %>% 
  mutate(rowid = 1:nrow(eigentrue)) %>%
  left_join(cr.df, by = 'rowid') %>%
  gather(cult, fam, house, jobs, trans, key = "var", value = "pref") %>%
  ggplot(aes(x = var, y = pref)) + 
  geom_violin(alpha = 0.6, width = 0.8, color = "transparent", fill = "gray") +
  geom_jitter(alpha = 0.6, height = 0, width = 0.1, aes(color = cr.dum)) +
  geom_boxplot(alpha = 0, width = 0.3, color = "#808080") +
  scale_x_discrete("Attribute", label = dict) +
  scale_y_continuous("Weight (dominant eigenvalue)", 
                     labels = scales::percent, 
                     breaks = c(seq(0,0.7,0.1))) +
  guides(color=guide_legend(title=NULL))+
  scale_color_discrete(breaks = c(0,1), 
                       labels = c(paste("CR >", thres), 
                                  paste("CR <", thres))) +
  labs(NULL, caption = paste("n =", nrow(city200), ",", "Mean CR =",
                           round(mean(cr),3)))+
  theme_minimal()

Dealing with inconsistent and missing data

Identifying inconsistent pairwise comparisons Not only are survey designers interested in the level of inconsistency present in their surveys, they are also interested in the source of inconsistency. Are respondents making inconsistent choices because some attributes are ill-defined, or that a pairwise comparison between those attributes simply do not make sense? ahpsurvey provides easy tools for researchers to identify the pairwise comparisons which respondents make inconsistent choices, which could contribute to better survey designs.

The ahp.pwerror compares the pairwise matrix of each individual with a Saaty Matrix (that has the property of CR=0 ) generated using the obtained preference weights. It is always better to understand this with an example.

The Saaty matrix is defined as the following:

\[S = \begin{bmatrix} \frac{p1}{p1} & \frac{p1}{p2} & \dotsi & \frac{p1}{pN} \\ \frac{p2}{p1} & \frac{p2}{p2} & ... & \frac{p2}{pN} \\ \vdots & \vdots & \frac{pi}{pj} & \vdots \\ \frac{pN}{p1} & \frac{pN}{p2} & \dotsi & \frac{pN}{pN} \end{bmatrix} \]

Where \(pi\) and \(pj\) are the final weights of the \(i_{th}\) and \(j_{th}\) attribute respectively, and \(N\) is the number of attributes.

I am no math major, and I find linear algebra intimidating. Here, I will demonstrate with an example from Saaty’s original matrix how we arrive the consistency error matrix from the original pairwise matrix.

Consider this matrix of the original pairwise comparison and the resultant preference weights below.

cult fam house jobs trans
cult 1.000 0.200 3.000 0.500 5
fam 5.000 1.000 7.000 1.000 7
house 0.333 0.143 1.000 0.250 3
jobs 2.000 1.000 4.000 1.000 7
trans 0.200 0.143 0.333 0.143 1

The goal is to compare the above matrix with a perfectly consistent Saaty matrix generated from the preference weights calculated using the dominant eigenvalue method.

preference <- t(ahp.indpref(sample_mat, atts, method = "eigen"))
preference
##             [,1]
## cult  0.15216708
## fam   0.43345437
## house 0.07155648
## jobs  0.30500643
## trans 0.03781564

The matrix is generated by multiplying its transposed reciprocal version of itself. This is no rocket science – for example, the cult fam comparison is calculated by dividing weight of cult by the weight of fam, 0.152 / 0.433 = 0.351.

S <- preference %*% t((preference)^-1)
S
##            cult       fam     house      jobs     trans
## cult  1.0000000 0.3510568 2.1265313 0.4988979  4.023919
## fam   2.8485423 1.0000000 6.0575141 1.4211319 11.462304
## house 0.4702494 0.1650842 1.0000000 0.2346064  1.892246
## jobs  2.0044179 0.7036645 4.2624574 1.0000000  8.065616
## trans 0.2485139 0.0872425 0.5284726 0.1239831  1.000000

The transposed Saaty matrix is multiplied element-by-element with the original pairwise comparison matrix (or taken its reciprocals if the product is smaller than 1) to generate a measure of how well the pairwise matrix resembles the Saaty matrix. If the matrix perfectly resembles the transposed Saaty matrix, the consistency error matrix (shown below) should very close to 1. This matrix is expressed as the following:

\[\epsilon_{ij}= a_{ij}.\frac{P_j}{P_i} \]

Where:

\(aij\) is the value in the pairwise comparison matrix. The values can be obtained with a simple matrix multiplication of the transpose of \(S\)

sample_mat[[1]] * t(S)
##            cult       fam     house      jobs     trans
## cult  1.0000000 0.5697085 1.4107481 1.0022090 1.2425697
## fam   1.7552838 1.0000000 1.1555895 0.7036645 0.6106975
## house 0.7088438 0.8653592 1.0000000 1.0656144 1.5854179
## jobs  0.9977959 1.4211319 0.9384258 1.0000000 0.8678816
## trans 0.8047838 1.6374720 0.6307485 1.1522308 1.0000000

The process is automated in ahp.error. Then ahp.error also loops through all pairwise comparison matrices generated by ahp.mat, and returns a list of error consistency matrices. The consistency matrices quantifies the inconsistency underlying each pairwise comparison of each decision-maker. I can also use reciprocal = TRUE to put all the errors that are above 1 into the upper triangular matrix. If reciprocal = FALSE, the below output will be essentially the same as the matrix above.

error <- ahp.error(sample_mat, atts, reciprocal = TRUE)
error
## [[1]]
##       cult      fam    house     jobs    trans
## cult     1 1.755284 1.410748 1.002209 1.242570
## fam     NA 1.000000 1.155590 1.421132 1.637472
## house   NA       NA 1.000000 1.065614 1.585418
## jobs    NA       NA       NA 1.000000 1.152231
## trans   NA       NA       NA       NA 1.000000

Here I demonstrate how to perform ahp.error in our 200 simulated decision-makers and compute the mean consistency error for each pairwise comparison. By using reciprocal = TRUE, I put all the errors that are above 1 into the upper triangular matrix so that we can summarise (by taking geometric mean) quickly the average error of each pairwise comparison (larger means more error).

gm_mean <- function(x, na.rm=TRUE){
  exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}

mat <- cityahp %>%
  ahp.error(atts, reciprocal = TRUE) %>%
  unlist() %>%
  as.numeric() %>%
  array(dim=c(length(atts), length(atts), length(cityahp))) %>%
  apply(c(1,2), gm_mean)

colnames(mat) <- rownames(mat) <- atts

mat
##       cult     fam    house     jobs    trans
## cult     1 1.88762 1.546260 1.916822 1.285215
## fam      1 1.00000 1.330816 1.590563 2.188926
## house    1 1.00000 1.000000 1.369874 1.235307
## jobs     1 1.00000 1.000000 1.000000 1.132664
## trans    1 1.00000 1.000000 1.000000 1.000000

The above matrix is a quick way for revealing inconsistencies within the data, but it is not the best way as it can be biased. If one or more decision-maker makes an incredibly inconsistent pairwise comparison, the consistency error for that pairwise comparison will be very high, which biases the mean error consistency of that pairwise comparison upwards even if many other decision-makers are making perfectly consistent choices.

Finding inconsistent pairwise comparisons by maximum

A better way, as I reckon, would be to extract the pairwise comparison with the maximum inconsistency error, and returning a list of the most inconsistent pairwise comparisons for each decision-maker. This process is automated in the ahp.pwerror function, which returns a dataframe of the top three most inconsistent pairwise comparison made by each decision-maker.

city200 %>%
  ahp.mat(atts) %>%
  ahp.pwerror(atts) %>%
  head()
##         top1       top2       top3
## 1   fam_jobs house_jobs  cult_jobs
## 2 cult_house house_jobs  fam_trans
## 3  fam_trans   cult_fam cult_trans
## 4   cult_fam  cult_jobs cult_house
## 5  cult_jobs  fam_trans   fam_jobs
## 6  fam_trans   cult_fam cult_house

A better way to visualise the pairwise comparisons is a bar chart:

cityahp %>%
  ahp.pwerror(atts) %>% 
  gather(top1, top2, top3, key = "max", value = "pair") %>%
  table() %>%
  as.data.frame() %>%
  ggplot(aes(x = pair, y = Freq, fill = max)) + 
  geom_bar(stat = 'identity') +
  scale_y_continuous("Frequency", breaks = c(seq(0,180,20))) +
  scale_fill_discrete(breaks = c("top1", "top2", "top3"), labels = c("1", "2", "3")) +
  scale_x_discrete("Pair") +
  guides(fill = guide_legend(title="Rank")) +
  theme(axis.text.x = element_text(angle = 20, hjust = 1),
        panel.background = element_rect(fill = NA),
        panel.grid.major.y = element_line(colour = "grey80"),
        panel.grid.major.x = element_blank(),
        panel.ontop = FALSE)

Pairwise comparison and its frequency as the most, second-most, and third most inconsistent pairwise comparsion Pairwise comparison and its frequency as the most, second-most, and third most inconsistent pairwise comparsion

The results are favorable – the frequency which a pairwise comparison is the most inconsistent for that decision-maker is reflective of the degree of randomness I have used to generate the dataset. The cult_fam, cult_jobs and fam_trans are assigned the highest standard deviations for the normal random draw, which partly contributes to its high frequency of being in the most inconsistent pairwise comparison in the chart.

Transforming inconsistent matrices Inconsistent pairwise matrices are problematic for AHP survey analysts. Harker (1987) described a method to replace inconsistent values: using the error matrix we have derived above, we can suggest a value that would reduce the inconsistency. Consider the below pairwise matrix found in Saaty’s explication of Harker’s method:

family <- c(1,1/5,1/3,1/7,1/6,1/6,3,4,
            5,1,3,1/5,1/3,1/3,5,7,
            3,1/3,1,1/6,1/3,1/4,1/6,5,
            7,5,6,1,3,4,7,8,
            6,3,3,1/3,1,2,5,6,
            6,3,4,1/4,1/2,1,5,6,
            1/3,1/5,6,1/7,1/5,1/5,1,2,
            1/4,1/7,1/5,1/8,1/6,1/6,1/2,1)

fam.mat <- list(matrix(family, nrow = 8 , ncol = 8))

atts <- c("size", "trans", "nbrhd", "age", "yard", "modern", "cond", "finance")

rownames(fam.mat[[1]]) <- colnames(fam.mat[[1]]) <- atts

fam.mat[[1]] %>% kable()
size trans nbrhd age yard modern cond finance
size 1.0000000 5.0000000 3.0000000 7 6.0000000 6.00 0.3333333 0.2500000
trans 0.2000000 1.0000000 0.3333333 5 3.0000000 3.00 0.2000000 0.1428571
nbrhd 0.3333333 3.0000000 1.0000000 6 3.0000000 4.00 6.0000000 0.2000000
age 0.1428571 0.2000000 0.1666667 1 0.3333333 0.25 0.1428571 0.1250000
yard 0.1666667 0.3333333 0.3333333 3 1.0000000 0.50 0.2000000 0.1666667
modern 0.1666667 0.3333333 0.2500000 4 2.0000000 1.00 0.2000000 0.1666667
cond 3.0000000 5.0000000 0.1666667 7 5.0000000 5.00 1.0000000 0.5000000
finance 4.0000000 7.0000000 5.0000000 8 6.0000000 6.00 2.0000000 1.0000000
ahp.cr(fam.mat, atts)
## [1] 0.1697071

The consistency ratio of the pairwise matrix is unsatisfactory. The procedure involved in Harker’s method is as follows:

Find the pairwise comparison with the maximum error (the \(i_{th}\) and \(j_{th}\) element)

Duplicate the matrix and replace the pairwise comparison in the new matrix with the maximum error with 0, and its two corresponding diagonal entries with 2 Compute new weights \(wi\) and \(wj\) (as in ahp.indpref with method = “eigen”)

Replace the pairwise comparison with \(w_i w_j\) and \(w_j w_i\) For an in-depth explication see Saaty (2003). Here I replicate the results in Saaty (2003) with the ahp.harker function.

edited <- ahp.harker(fam.mat, atts, iterations = 10, stopcr = 0.1)
## [1] "Ind 1 Iterations: 1"
## [1] "Ind 1 Iterations: 1"
edited[[1]]%>% kable() 
size trans nbrhd age yard modern cond finance
size 1.0000000 5.0000000 3.0000000 7 6.0000000 6.00 0.3333333 0.2500000
trans 0.2000000 1.0000000 0.3333333 5 3.0000000 3.00 0.2000000 0.1428571
nbrhd 0.3333333 3.0000000 1.0000000 6 3.0000000 4.00 0.4586717 0.2000000
age 0.1428571 0.2000000 0.1666667 1 0.3333333 0.25 0.1428571 0.1250000
yard 0.1666667 0.3333333 0.3333333 3 1.0000000 0.50 0.2000000 0.1666667
modern 0.1666667 0.3333333 0.2500000 4 2.0000000 1.00 0.2000000 0.1666667
cond 3.0000000 5.0000000 2.1802085 7 5.0000000 5.00 1.0000000 0.5000000
finance 4.0000000 7.0000000 5.0000000 8 6.0000000 6.00 2.0000000 1.0000000
ahp.cr(edited, atts)
## [1] 0.08248709

As seen here, element [3,7] is the most inconsistent pairwise comparison, thus it was replaced with a more consistent value 0.459.

ahp.harker takes five optional arguments:

printiter is a logical argument of whether the number of iterations taken for each pairwise matrix is reported or not. Generally it is not needed if stopcr is not specified. When stopcr is specified, this is a good way of identifying how many pairwise comparisons are actually replaced by the algorithm for each decision maker. The printout above shows “Ind 1 Iterations: 1”, which shows that although I specified iterations = 10, individual 1 (Ind 1) was only iterated one time before it reached the target consistency ratio, 0.1. Only one element was replaced.

I will demonstrate how ahp.harker improved the consistency of the decision-makers in our fictitious sample.

crmat <- matrix(NA, nrow = 200, ncol = 11)
colnames(crmat) <- 0:10

atts <- c("cult", "fam", "house", "jobs", "trans")

crmat[,1] <- city200 %>%
    ahp.mat(atts, negconvert = TRUE) %>%
    ahp.cr(atts)

for (it in 1:10){
  crmat[,it+1] <- city200 %>%
    ahp.mat(atts, negconvert = TRUE) %>%
    ahp.harker(atts, iterations = it, stopcr = 0.1, 
               limit = T, round = T, printiter = F) %>%
    ahp.cr(atts)
}

data.frame(table(crmat[,1] <= 0.1), 
           table(crmat[,3] <= 0.1),
           table(crmat[,5] <= 0.1)) %>% 
  select(Var1, Freq, Freq.1, Freq.2) %>%
  rename("Consistent?" = "Var1", "No Iteration" = "Freq",
         "2 Iterations" = "Freq.1", "4 Iterations" = "Freq.2")
##   Consistent? No Iteration 2 Iterations 4 Iterations
## 1       FALSE           70           13            2
## 2        TRUE          130          187          198

While using Harker’s method cannot completely lower the CR of all decision-makers to desired levels, it allows researchers to keep a lot more observations; whereas we would have to truncate 70 samples, now we only have to censor 22 samples with 1 iteration.

crmat %>% 
  as.data.frame() %>%
  gather(key = "iter", value = "cr", `0`, 1,2,3,4,5,6,7,8,9,10,11) %>%
  mutate(iter = as.integer(iter)) %>%
  ggplot(aes(x = iter, y = cr, group = iter)) +
  geom_hline(yintercept = 0.1, color = "red", linetype = "dashed")+
  geom_jitter(alpha = 0.2, width = 0.3, height = 0, color = "turquoise4") +
  geom_boxplot(fill = "transparent", color = "#808080", outlier.shape = NA) + 
  scale_x_continuous("Iterations", breaks = 0:10) +
  scale_y_continuous("Consistency Ratio") +
  theme_minimal()

Consistency Ratios under different number of iterations with Harker’s method

it <- 1
thres <- 0.1
cr.df1 <- data.frame(cr = city200 %>%
  ahp.mat(atts, negconvert = TRUE) %>%
  ahp.harker(atts, iterations = it, stopcr = 0.1, limit = T, round = T, printiter = F) %>%
  ahp.cr(atts))

cr.df2 <- cr.df1 %>%
  mutate(rowid = 1:nrow(city200), cr.dum = as.factor(ifelse(. <= thres, 1, 0))) %>%
  select(cr.dum, rowid)

city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.harker(atts, iterations = it, stopcr = 0.1, limit = T, round = T, printiter = F) %>%
  ahp.indpref(atts, method = "eigen") %>% 
  mutate(rowid = 1:nrow(city200)) %>%
  left_join(cr.df2, by = 'rowid') %>%
  gather(cult, fam, house, jobs, trans, key = "var", value = "pref") %>%
  ggplot(aes(x = var, y = pref)) + 
  geom_violin(alpha = 0.6, width = 0.8, color = "transparent", fill = "gray") +
  geom_jitter(alpha = 0.3, height = 0, width = 0.1, aes(color = cr.dum)) +
  geom_boxplot(alpha = 0, width = 0.3, color = "#808080") +
  scale_x_discrete("Attribute", label = dict) +
  scale_y_continuous("Weight (dominant eigenvalue)", 
                     labels = scales::percent, breaks = c(seq(0,0.7,0.1))) +
  guides(color=guide_legend(title=NULL))+
  scale_color_discrete(breaks = c(0,1), 
                       labels = c(paste("CR >", thres), 
                                  paste("CR <", thres))) +
  labs(NULL, caption =paste("n =",nrow(city200), ",", "Mean CR =",round(mean(cr),3)))+
  theme_minimal()

Individual preference weights with respect to goal (1 iteration)

Let’s take a look at how applying Harker’s method affects the overall aggregated priorities of the population.

options(scipen = 99)
inconsistent <- city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.aggpref(atts, method = "eigen")

consistent <- city200 %>%
  ahp.mat(atts = atts, negconvert = TRUE) %>% 
  ahp.harker(atts, iterations = 5, stopcr = 0.1, limit = T, round = T, printiter = F) %>%
  ahp.aggpref(atts, method = "eigen")

true <- t(ahp.indpref(sample_mat, atts, method = "eigen"))

aggpref.df <- data.frame(Attribute = atts, true,inconsistent,consistent) %>%
  mutate(error.incon = abs(true - inconsistent),
         error.con = abs(true - consistent))

aggpref.df
##       Attribute       true inconsistent consistent error.incon   error.con
## cult       cult 0.15216708   0.15261018 0.14271563 0.000443101 0.009451455
## fam         fam 0.43345437   0.44827276 0.43882283 0.014818395 0.005368459
## house     house 0.07155648   0.07052519 0.06977153 0.001031282 0.001784948
## jobs       jobs 0.30500643   0.27579123 0.28862212 0.029215199 0.016384312
## trans     trans 0.03781564   0.03965027 0.04235481 0.001834631 0.004539167

Here I present the aggregated weights of the pairwise matrices without and with treatment with Harker’s method, the aggregated priorities derived from the true weights of the sample, as well as the deviation of the priorities from the true weights. Because improving the consistency of the matrix does not necessarily increase the validity of the matrix, it is imperative that researchers consider other ways to improve consistency, ideally asking respondents to reconsider their choices, whenever inconsistency arises.

While there are strong arguments against replacing inconsistent values without the decision-maker’s consent for the sake of satisfying the consistency ratio criterion of CR < 0.1 (see Saaty and Tran (2007)), it is often not possible for survey executers to resolicit answers from their respondents after AHP analysis, whereas truncating inconsistent decisions may make the dataset unrepresentative of the population. Researchers should think carefully and explain fully the methods used to process AHP data.

Imputing missing pairwise comparison matrices

Missing data is a common feature in surveys. Harker’s method was originally developed to complete incomplete pairwise comparison matrices, and can be implemented here using the same strategy as ahp.harker.

missing.df <- city200[1:10,]
for (i in 1:10){
  missing.df[i, round(runif(1,1,10))] <- NA
  if (i > 7){
    missing.df[i, round(runif(1,2,10))] <- NA
  }
}
missing.df[,1:7]
##    cult_fam cult_house cult_jobs cult_trans fam_house fam_jobs fam_trans
## 1         2         -2         2         -6        -4       -4        -8
## 2         2         -4         1         NA        -4       -2        -8
## 3         4         -2         1         -3        NA       -3        -5
## 4         8         -4         3         NA        -8        1        -7
## 5         3         -3         5         -6        -8        1        -4
## 6         6         -4         2         -4        -7       -2        NA
## 7         7         -5        -3         -3        -8        1        NA
## 8         5         -4        NA         -5        -6       NA        -8
## 9         3         NA         2         -4        -6       NA        -6
## 10        7         -3        NA         NA        -8       -3        -5