On my December to-do list, I had “write an R package to make analytic hierarchy process (AHP) easier” — but fortunately gluc beat me to it, and saved me tons of time that I spent using AHP to do an actual research problem. First of all, thank you for writing the new ahp package! Next, I’d like to show everyone just how easy this package makes performing AHP and displaying the results. We will use the Tom, Dick, and Harry example that is described on Wikipedia. – the goal is to choose a new employee, and you can pick either Tom, Dick, or Harry. Read the problem statement on Wikipedia before proceeding.

AHP is a method for multi-criteria decision making that breaks the problem down based on decision criteria, subcriteria, and alternatives that could satisfy a particular goal. The criteria are compared to one another, the alternatives are compared to one another based on how well they comparatively satisfy the subcriteria, and then the subcriteria are examined in terms of how well they satisfy the higher-level criteria. The Tom-Dick-Harry problem is a simple hierarchy: only one level of criteria separates the goal (“Choose the Most Suitable Leader”) from the alternatives (Tom, Dick, or Harry):

tom-dick-harry

To use the ahp package, the most challenging part involves setting up the YAML file with your hierarchy and your rankings. THE MOST IMPORTANT THING TO REMEMBER IS THAT THE FIRST COLUMN IN WHICH A WORD APPEARS IS IMPORTANT. This feels like FORTRAN. YAML experts may be appalled that I just didn’t know this, but I didn’t. So most of the first 20 hours I spent stumbling through the ahp package involved coming to this very critical conclusion. The YAML AHP input file requires you to specify 1) the alternatives (along with some variables that describe the alternatives; I didn’t use them in this example, but I’ll post a second example that does use them) and 2) the goal hierarchy, which includes 2A) comparisons of all the criteria against one another FIRST, and then 2B) comparisons of the criteria against the alternatives. I saved my YAML file as tomdickharry.txt and put it in my C:/AHP/artifacts directory:

#########################
# Alternatives Section
# THIS IS FOR The Tom, Dick, & Harry problem at
# https://en.wikipedia.org/wiki/Analytic_hierarchy_process_%E2%80%93_leader_example
#
Alternatives: &alternatives
# 1= not well; 10 = best possible
# Your assessment based on the paragraph descriptions may be different.
  Tom:
    age: 50
    experience: 7
    education: 4
    leadership: 10
  Dick:
    age: 60
    experience: 10
    education: 6
    leadership: 6
  Harry:
    age: 30
    experience: 5
    education: 8
    leadership: 6
#
# End of Alternatives Section
#####################################
# Goal Section
#
Goal:
# A Goal HAS preferences (within-level comparison) and HAS Children (items in level)
  name: Choose the Most Suitable Leader
  preferences:
    # preferences are defined pairwise
    # 1 means: A is equal to B
    # 9 means: A is highly preferable to B
    # 1/9 means: B is highly preferable to A
    - [Experience, Education, 4]
    - [Experience, Charisma, 3]
    - [Experience, Age, 7]
    - [Education, Charisma, 1/3]
    - [Education, Age, 3]
    - [Age, Charisma, 1/5]
  children: 
    Experience:
      preferences:
        - [Tom, Dick, 1/4]
        - [Tom, Harry, 4]
        - [Dick, Harry, 9]
      children: *alternatives
    Education:
      preferences:
        - [Tom, Dick, 3]
        - [Tom, Harry, 1/5]
        - [Dick, Harry, 1/7]
      children: *alternatives
    Charisma:
      preferences:
        - [Tom, Dick, 5]
        - [Tom, Harry, 9]
        - [Dick, Harry, 4]
      children: *alternatives
    Age:
      preferences:
        - [Tom, Dick, 1/3]
        - [Tom, Harry, 5]
        - [Dick, Harry, 9]
      children: *alternatives
#
# End of Goal Section
#####################################

Next, I installed gluc’s ahp package and a helper package, data.tree, then loaded them into R:

devtools::install_github("gluc/ahp", build_vignettes = TRUE)
install.packages("data.tree")

library(ahp)
library(data.tree)

Running the calculations was ridiculously easy:

setwd("C:/AHP/artifacts")
myAhp <- LoadFile("tomdickharry.txt")
Calculate(myAhp)

And then generating the output was also ridiculously easy:

> GetDataFrame(myAhp)
                                  Weight  Dick   Tom Harry Consistency
1 Choose the Most Suitable Leader 100.0% 49.3% 35.8% 14.9%        4.4%
2  ¦--Experience                   54.8% 39.3% 11.9%  3.6%        3.2%
3  ¦--Education                    12.7%  1.0%  2.4%  9.2%        5.6%
4  ¦--Charisma                     27.0%  5.2% 20.1%  1.7%        6.1%
5  °--Age                           5.6%  3.8%  1.5%  0.4%        2.5%
> 
> print(myAhp, "weight", filterFun = isNotLeaf)
                        levelName     weight
1 Choose the Most Suitable Leader 1.00000000
2  ¦--Experience                  0.54756924
3  ¦--Education                   0.12655528
4  ¦--Charisma                    0.26994992
5  °--Age                         0.05592555
> print(myAhp, "weight")
                         levelName     weight
1  Choose the Most Suitable Leader 1.00000000
2   ¦--Experience                  0.54756924
3   ¦   ¦--Tom                     0.21716561
4   ¦   ¦--Dick                    0.71706504
5   ¦   °--Harry                   0.06576935
6   ¦--Education                   0.12655528
7   ¦   ¦--Tom                     0.18839410
8   ¦   ¦--Dick                    0.08096123
9   ¦   °--Harry                   0.73064467
10  ¦--Charisma                    0.26994992
11  ¦   ¦--Tom                     0.74286662
12  ¦   ¦--Dick                    0.19388163
13  ¦   °--Harry                   0.06325174
14  °--Age                         0.05592555
15      ¦--Tom                     0.26543334
16      ¦--Dick                    0.67162545
17      °--Harry                   0.06294121

You can also generate very beautiful output with the command below (but you’ll have to run the example yourself if you want to see how fantastically it turns out — maybe that will provide some motivation!)

ShowTable(myAhp)

I’ll post soon with an example of how to use AHP preference functions in the Tom, Dick, & Harry problem.

11 responses to “Analytic Hierarchy Process (AHP) with the ahp Package”

  1. […] article was first published on Quality and Innovation » R, and kindly contributed to […]

  2. […] Analytic Hierarchy Process (AHP) with the ahp Package On my December to-do list, I had “write an R package to make analytic hierarchy process (AHP) easier” — but fortunately gluc beat me to it, and saved me tons of time that I spent using AHP to do an actual research problem. First of all, thank you for writing the new ahp package! Next, I’d like to show everyone just how easy this package makes performing AHP and displaying the results. We will use the Tom, Dick, and Harry example that is described on Wikipedia. – the goal is to choose a new employee, and you can pick either Tom, Dick, or Harry. Read the problem statement on Wikipedia before proceeding. […]

  3. Analytic Hierarchy Process (AHP) using preferenceFunction in ahp | Quality and Innovation Avatar

    […] Yesterday, I wrote about how to use gluc‘s new ahp package on a simple Tom-Dick-Harry one level decision making problem using Analytic Hierarchy Process (AHP). One of the cool things about that package is that in addition to specifying the pairwise comparisons directly using Saaty’s scale (below, from https://kristalaace2014.wordpress.com/2014/05/14/w12_al_vendor-evaluation/)… […]

  4. Analytic Hierarchy Process (AHP) using preferenceFunction in ahp | Quality and Innovation Avatar

    […] Yesterday, I wrote about how to use gluc‘s new ahp package on a simple Tom-Dick-Harry one level decision making problem using Analytic Hierarchy Process (AHP). One of the cool things about that package is that in addition to specifying the pairwise comparisons directly using Saaty’s scale (below, from https://kristalaace2014.wordpress.com/2014/05/14/w12_al_vendor-evaluation/)… […]

  5. Christoph Glur Avatar

    Hi Nicole,

    thanks for using my package! I am working hard on the next version, and you can peak-preview on the dev branch on github.

    The main improvements are here: https://github.com/gluc/ahp/blob/dev/NEWS

    The file format will change. As I didn’t expect anyone to do anything serious with the package yet, I didn’t plan backward compatibility. But now that I know I’m wrong, I might do it anyway if it’s not too complicated. Anyway, it would be great if you could add your example to the package. Ideally, you could do a pull request on the dev branch.

    Anyway, thanks again for being an early adopter!

    Best,

    Chris

  6. Suda Avatar
    Suda

    Hi Glur,
    It is wonderful package, I it is pretty much easy.
    As in example, I use for two decision maker, but making same for number of decision maker is really cumbersome. is there any other way to input for number of decision makers. like thorugh decision matrix

  7. Marcelo Carvalho Fernandes Avatar

    For those who want to run the Tom, Dick and Harry example here goes the new file format required for the current version (v0.3.0 candidate) of gluc/ahp:

    #########################
    Version: 2.0
    #########################
    # Alternatives Section
    # THIS IS FOR The Tom, Dick, & Harry problem at
    # https://en.wikipedia.org/wiki/Analytic_hierarchy_process_%E2%80%93_leader_example
    #
    Alternatives: &alternatives
    # 1= not well; 10 = best possible
    # Your assessment based on the paragraph descriptions may be different.
    Tom:
    age: 50
    experience: 7
    education: 4
    leadership: 10
    Dick:
    age: 60
    experience: 10
    education: 6
    leadership: 6
    Harry:
    age: 30
    experience: 5
    education: 8
    leadership: 6
    #
    # End of Alternatives Section
    #####################################
    # Goal Section
    #
    Goal:
    # A Goal HAS preferences (within-level comparison) and HAS Children (items in level)
    name: Choose the Most Suitable Leader
    preferences:
    pairwise:
    # preferences are defined pairwise
    # 1 means: A is equal to B
    # 9 means: A is highly preferable to B
    # 1/9 means: B is highly preferable to A
    – [Experience, Education, 4]
    – [Experience, Charisma, 3]
    – [Experience, Age, 7]
    – [Education, Charisma, 1/3]
    – [Education, Age, 3]
    – [Age, Charisma, 1/5]
    children:
    Experience:
    preferences:
    pairwise:
    – [Tom, Dick, 1/4]
    – [Tom, Harry, 4]
    – [Dick, Harry, 9]
    children: *alternatives
    Education:
    preferences:
    pairwise:
    – [Tom, Dick, 3]
    – [Tom, Harry, 1/5]
    – [Dick, Harry, 1/7]
    children: *alternatives
    Charisma:
    preferences:
    pairwise:
    – [Tom, Dick, 5]
    – [Tom, Harry, 9]
    – [Dick, Harry, 4]
    children: *alternatives
    Age:
    preferences:
    pairwise:
    – [Tom, Dick, 1/3]
    – [Tom, Harry, 5]
    – [Dick, Harry, 9]
    children: *alternatives
    #
    # End of Goal Section
    #####################################

  8. Vineet Avatar
    Vineet

    Hello Mr Glur and Ms. Nicole. I am writing a research paper on online retailing and trying to prioritize my factors using the package “ahp”. But I am trying hard to load my ahp file to the package which I couldn’t. Your help can do Godly things for me.

    > LoadFile() showing this error. Error in LoadFile() : could not find function “LoadFile”
    I tried to make file and saved in .txt also I tried in .yaml but nothing is working.

    Using the latest R version on MacOsX.

    1. Nicole Radziwill Avatar
      Nicole Radziwill

      Hi Vineet — would not be surprised if some of the code changed in the package since I originally wrote this post a while back. I have to do some AHP this week myself so I’ll post any changes I discover. Thanks!

  9. lili Avatar
    lili

    Hi Nicole,
    Thank you for this great package. My question is exactly what Vineet asked as the last comment.

    I got this error message too:
    > LoadFile() showing this error. Error in LoadFile() : could not find function “LoadFile”

    How can I fix it?

  10. Jonas Avatar
    Jonas

    Hey! I love the AHP package for R by gluc and thanks for the writeup nicole! I was wondering whether it is possible to define a function once in the YAML file and use it for several different pairwiseFunctions.
    Thanks!

Leave a Reply

I’m Nicole

Since 2008, I’ve been reflecting on Digital Transformation & Data Science for Performance Excellence here. As a CxO, I’ve helped orgs build empowered teams, robust programs, and elegant strategies bridging data, analytics, and artificial intelligence (AI)/machine learning (ML)… while building models in R and Python on the side. In 2024, I help leaders navigate the complex market of data/AI vendors & professional services. Need help sifting through it all? Reach out to inquire.

More About Me or Inquire @ Engaging a Team

Let’s connect