R語言層次分析法小實(shí)例

原文地址
https://www.r-bloggers.com/analytic-hierarchy-process-ahp-with-the-ahp-package/
Tom Dick and Harry problem

問題描述

以下內(nèi)容來自維基百科

This example describes the use of the AHP in choosing a leader for a company whose founder is about to retire. There are several competing candidates and several competing criteria for choosing the most suitable one. By using the AHP, the board of directors is able to choose the best candidate in a rational, transparent way that can be examined and understood by all concerned.

簡單理解:公司領(lǐng)導(dǎo)人退休太惠,需要選舉新的領(lǐng)導(dǎo)人淮蜈。為了使選舉結(jié)果公開透明、使人信服哼丈,需要一定的方法窃款。這里選擇的是AHP(analytic hierarchy process層次分析法)踩寇。

  • 目標(biāo)是選擇公司領(lǐng)導(dǎo)人监徘;
  • 評(píng)價(jià)的標(biāo)準(zhǔn)教育背景Education晋修,Experience經(jīng)驗(yàn),Charisma領(lǐng)導(dǎo)力凰盔,Age
  • 3個(gè)候選人Tom墓卦,DickHarry
AHP層次分析法

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):

上面這段話自己還看不太明白户敬,反正大體的意思就是運(yùn)用這個(gè)方法可以幫你選出最合適的領(lǐng)導(dǎo)人落剪。
R語言里有一個(gè)現(xiàn)成的包可以來做這個(gè)事情ahp
安裝方法
devtools::install_github("gluc/ahp", build_vignettes = TRUE)
安裝過程有時(shí)候會(huì)報(bào)錯(cuò)cannot remove prior installation of package ‘Rcpp’,刪掉舊的包,手動(dòng)安裝一遍就可以了
但是準(zhǔn)備輸入文件太麻煩尿庐,要求的是YAML格式忠怖。下面是ahp包中提供的示例文件

Version: 2.0

#########################
# Alternatives Section
#

Alternatives: &alternatives
# Here, we list all the alternatives, together with their attributes. 
# We can use these attributes later in the file when defining 
# preferenceFunctions. The attributes can be quantitative or 
# qualitative.
  Accord Sedan:
    price: 20360
    mpg: 31
    passengers: 5
    cargo: 14
    curb weight: 3289
    safety class: Midsize Car
    crash rating: 4 in Side Impact Front
    residual value: 0.52
  Accord Hybrid:
    price: 31090
    mpg: 35
    passengers: 5
    cargo: 14
    curb weight: 3501
    safety class: Midsize Car
    crash rating: 4 in Side Impact Front
    residual value: 0.46
  Pilot:
    price: 27595
    mpg: 22
    passengers: 8
    cargo: 87.6
    curb weight: 4264
    safety class: Midsize SUV
    crash rating: 4 in Rollover
    residual value: 0.4
  CR-V:
    price: 20700
    mpg: 27
    passengers: 5
    cargo: 72.9
    curb weight: 3389
    safety class: Small SUV
    crash rating: 4 in Rollover
    residual value: 0.55
  Element:
    price: 18980
    mpg: 25
    passengers: 4
    cargo: 74.6
    curb weight: 3433
    safety class: Small SUV
    crash rating: 3 in Rollover
    residual value: 0.48
  Odyssey:
    price: 25645
    mpg: 26
    passengers: 8
    cargo: 147.4
    curb weight: 4385
    safety class: Minivan
    crash rating: All 5 Stars
    residual value: 0.48

#
# End of Alternatives Section
#####################################

#####################################
# Goal Section
#


Goal:
# The goal spans a tree of criteria and the alternatives
  name: Buy Car
  description: >
    This is a classic single decision maker problem. It models
    the situation facing by a family that wants to buy a new car.
  author: unknown
  preferences:
    # preferences are typically defined pairwise
    # 1 means: A is equal to B
    # 9 means: A is highly preferrable to B
    # 1/9 means: B is highly preferrable to A
    pairwise:
      - [Cost, Safety, 3]
      - [Cost, Style, 7]
      - [Cost, Capacity, 3]
      - [Safety, Style, 9]
      - [Safety, Capacity, 1]
      - [Style, Capacity, 1/7]
  children: 
    Cost:
      preferences:
        pairwise:
          - [Purchase Price, Fuel Cost, 2]
          - [Purchase Price, Maintenance Cost, 5]
          - [Purchase Price, Resale Value, 3]
          - [Fuel Cost, Maintenance Cost, 2]
          - [Fuel Cost, Resale Value, 2]
          - [Maintenance Cost, Resale Value, 1/2]
      children:
        Purchase Price:
          preferences: 
            pairwise:
              - [Accord Sedan, Accord Hybrid, 9]
              - [Accord Sedan, Pilot, 9]
              - [Accord Sedan, CR-V, 1]
              - [Accord Sedan, Element, 1/2]
              - [Accord Sedan, Odyssey, 5]
              - [Accord Hybrid, Pilot, 1]
              - [Accord Hybrid, CR-V, 1/9]
              - [Accord Hybrid, Element, 1/9]
              - [Accord Hybrid, Odyssey, 1/7]
              - [Pilot, CR-V, 1/9]
              - [Pilot, Element, 1/9]
              - [Pilot, Odyssey, 1/7]
              - [CR-V, Element, 1/2]
              - [CR-V, Odyssey, 5]
              - [Element, Odyssey, 6]
          children: *alternatives
          # We don't need to retype the alternatives here. Instead
          # we can simply make a reference to the alternatives anchor
          # defined in the alternatives section of the file.
        Fuel Cost:
          # Alternatively to the pairwise preferences, you 
          # can define a preference function. This function
          # is in R syntax, and needs to have two arguments.
          # The Calculate method will pass all combinations
          # of alternatives to this function, and the function
          # is expected to return the pairwise preference, i.e.
          # a number between 1/9 and 9.
          preferences: 
            pairwiseFunction:
              function(a1, a2) min(9, max(1/9, a2$mpg/a1$mpg))
          children: *alternatives
        Maintenance Cost: 
          preferences:
            pairwise:
              - [Accord Sedan, Accord Hybrid, 1.5]
              - [Accord Sedan, Pilot, 4]
              - [Accord Sedan, CR-V, 4]
              - [Accord Sedan, Element, 4]
              - [Accord Sedan, Odyssey, 5]
              - [Accord Hybrid, Pilot, 4]
              - [Accord Hybrid, CR-V, 4]
              - [Accord Hybrid, Element, 4]
              - [Accord Hybrid, Odyssey, 5]
              - [Pilot, CR-V, 1]
              - [Pilot, Element, 1.2]
              - [Pilot, Odyssey, 1]
              - [CR-V, Element, 1]
              - [CR-V, Odyssey, 3]
              - [Element, Odyssey, 2]
          children: *alternatives
        Resale Value:
          preferences:
            pairwiseFunction: >
              GetResalePreference <- function(a1, a2) {
                if (a1$`residual value` < a2$`residual value`) return (1/GetResalePreference(a2, a1))
                ratio <- a1$`residual value` / a2$`residual value`
                if (ratio < 1.05) return (1)
                if (ratio < 1.1) return (2)
                if (ratio < 1.15) return (3)
                if (ratio < 1.2) return (4)
                if (ratio < 1.25) return (5)
                return (5)
              }
          children: *alternatives 
    Safety: 
      preferences:
        pairwise:
          - [Accord Sedan, Accord Hybrid, 1]
          - [Accord Sedan, Pilot, 5]
          - [Accord Sedan, CR-V, 7]
          - [Accord Sedan, Element, 9]
          - [Accord Sedan, Odyssey, 1/3]
          - [Accord Hybrid, Pilot, 5]
          - [Accord Hybrid, CR-V, 7]
          - [Accord Hybrid, Element, 9]
          - [Accord Hybrid, Odyssey, 1/3]
          - [Pilot, CR-V, 2]
          - [Pilot, Element, 9]
          - [Pilot, Odyssey, 1/8]
          - [CR-V, Element, 2]
          - [CR-V, Odyssey, 1/8]
          - [Element, Odyssey, 1/9]
      children: *alternatives
    Style: 
      preferences: 
        pairwise:
          - [Accord Sedan, Accord Hybrid, 1]
          - [Accord Sedan, Pilot, 7]
          - [Accord Sedan, CR-V, 5]
          - [Accord Sedan, Element, 9]
          - [Accord Sedan, Odyssey, 6]
          - [Accord Hybrid, Pilot, 7]
          - [Accord Hybrid, CR-V, 5]
          - [Accord Hybrid, Element, 9]
          - [Accord Hybrid, Odyssey, 6]
          - [Pilot, CR-V, 1/6]
          - [Pilot, Element, 3]
          - [Pilot, Odyssey, 1/3]
          - [CR-V, Element, 7]
          - [CR-V, Odyssey, 5]
          - [Element, Odyssey, 1/5]
      children: *alternatives
    Capacity:
      preferences:
        pairwise:
          - [Cargo Capacity, Passenger Capacity, 1/5]
      children:
        Cargo Capacity: 
          preferences:
            pairwiseFunction: >
              CargoPreference <- function(a1, a2) {
                if (a1$cargo < a2$cargo) return (1/CargoPreference(a2, a1))
                ratio <- a1$cargo / a2$cargo
                if (ratio < 3) return (1)
                if (ratio < 8) return (2)
                return (3)
              }
          children: *alternatives
        Passenger Capacity: 
          preferences:
            pairwise:
              - [Accord Sedan, Accord Hybrid, 1]
              - [Accord Sedan, Pilot, 1/2]
              - [Accord Sedan, CR-V, 1]
              - [Accord Sedan, Element, 3]
              - [Accord Sedan, Odyssey, 1/2]
              - [Accord Hybrid, Pilot, 1/2]
              - [Accord Hybrid, CR-V, 1]
              - [Accord Hybrid, Element, 3]
              - [Accord Hybrid, Odyssey, 1/2]
              - [Pilot, CR-V, 2]
              - [Pilot, Element, 6]
              - [Pilot, Odyssey, 1]
              - [CR-V, Element, 3]
              - [CR-V, Odyssey, 1/2]
              - [Element, Odyssey, 1/6]
          children: *alternatives

#
# End of Goal Section
#####################################

準(zhǔn)備這個(gè)輸入文件應(yīng)該是非常麻煩,如何將我們自己的數(shù)據(jù)整理成這種格式可能得費(fèi)一番功夫了屁倔!

#########################
# Alternatives Section
# THIS IS FOR The Tom, Dick, & Harry problem at
#
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
#####################################
接下來用這個(gè)文件來做AHP分析

myAhp<-Load("../Desktop/AHP_practice.txt.txt")
遇到了報(bào)錯(cuò)

Error in value[[3L]](cond) : 
  Could not load ahp model. Exception caught when converting into a data.tree: Error in doTryCatch(return(expr), name, parentenv, handler): Could not load ahp model. Could not find Version
In addition: Warning message:
In readChar(ahpFile, file.info(ahpFile)$size) :
  can only read in bytes in a non-UTF-8 MBCS locale

暫時(shí)還不知道如何解決

重復(fù)ahp報(bào)自帶的例子
devtools::install_github("gluc/ahp")

library(ahp)
myAhp<-Load("../Desktop/AHP_practice.txt.txt")
??LoadFile
ahp::Load()

ahpFile<-system.file("extdata","car.ahp",package="ahp")
carAhp<-Load(ahpFile)
Calculate(carAhp)
Visualize(carAhp)
Analyze(carAhp)
AnalyzeTable(carAhp)
輸出結(jié)果
image.png

image.png

輸出結(jié)果竟然是圖片形式展示表格D杂帧D弘省锐借!

結(jié)果里好像也沒有給出最終的決策結(jié)果呀!可能是有其他函數(shù)實(shí)現(xiàn)的嗎往衷?

結(jié)果里給出了各個(gè)變量的權(quán)重

本篇文章的目的:知道有這么個(gè)方法钞翔,知道有這個(gè)R包可以實(shí)現(xiàn)!研究研究如何將自己的數(shù)據(jù)轉(zhuǎn)化為輸入文件的格式席舍!

歡迎大家關(guān)注我的公眾號(hào) 小明的數(shù)據(jù)分析筆記本

公眾號(hào)二維碼.jpg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末布轿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子来颤,更是在濱河造成了極大的恐慌汰扭,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件福铅,死亡現(xiàn)場離奇詭異萝毛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)滑黔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門笆包,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人略荡,你說我怎么就攤上這事庵佣。” “怎么了汛兜?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵巴粪,是天一觀的道長。 經(jīng)常有香客問我,道長肛根,這世上最難降的妖魔是什么衡创? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮晶通,結(jié)果婚禮上璃氢,老公的妹妹穿的比我還像新娘。我一直安慰自己狮辽,他們只是感情好一也,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著喉脖,像睡著了一般椰苟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上树叽,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天舆蝴,我揣著相機(jī)與錄音,去河邊找鬼题诵。 笑死洁仗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的性锭。 我是一名探鬼主播赠潦,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼草冈!你這毒婦竟也來了她奥?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤怎棱,失蹤者是張志新(化名)和其女友劉穎哩俭,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拳恋,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凡资,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了诅岩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片讳苦。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖吩谦,靈堂內(nèi)的尸體忽然破棺而出鸳谜,到底是詐尸還是另有隱情,我是刑警寧澤式廷,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布咐扭,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蝗肪。R本人自食惡果不足惜袜爪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望薛闪。 院中可真熱鬧辛馆,春花似錦、人聲如沸豁延。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诱咏。三九已至苔可,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間袋狞,已是汗流浹背焚辅。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留苟鸯,地道東北人同蜻。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像倔毙,于是被迫代替她去往敵國和親埃仪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子乙濒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345