En mi lista de tareas pendientes de diciembre del año pasado , tenía que “escribir un paquete R para facilitar el proceso de jerarquía analítica (AHP)”, pero afortunadamente el grupo gluc se me adelantó y me ahorró toneladas de tiempo que dediqué a usar AHP para realizar una investigación real. problema. En primer lugar, ¡gracias por escribir el nuevo paquete ahp! A continuación, me gustaría mostrarles a todos lo fácil que este paquete hace que realice AHP y muestre los resultados. Usaremos el ejemplo de Tom, Dick y Harry que se describe en Wikipedia. – el objetivo es elegir un nuevo empleado y puedes elegir a Tom, Dick o Harry. Lea el planteamiento del problema en Wikipedia antes de continuar.
AHP (Analitical Hierarchical Process) es un método para la toma de decisiones con criterios múltiples que desglosa el problema en función de criterios de decisión, subcriterios y alternativas que podrían satisfacer un objetivo particular. Los criterios se comparan entre sí, las alternativas se comparan entre sí basándose en qué tan bien satisfacen comparativamente los subcriterios, y luego los subcriterios se examinan en términos de qué tan bien satisfacen los criterios de nivel superior. El problema de Tom-Dick-Harry es una jerarquía simple: sólo un nivel de criterios separa la meta (“Elegir al líder más adecuado”) de las alternativas (Tom, Dick o Harry):
Para utilizar el paquete ahp, la parte más desafiante consiste en configurar el archivo YAML con su jerarquía y sus clasificaciones. LO MÁS IMPORTANTE A RECORDAR ES QUE LA PRIMERA COLUMNA EN LA QUE APARECE UNA PALABRA ES IMPORTANTE. Esto se siente como FORTRAN. Los expertos de YAML pueden estar consternados porque yo no sabía esto, pero no lo sabía. Así que la mayor parte de las primeras 20 horas que pasé explorando el paquete ahp implicaron llegar a esta conclusión tan crítica. El archivo de entrada YAML AHP requiere que especifiques 1) las alternativas (junto con algunas variables que describen las alternativas; no las usé en este ejemplo, pero publicaré un segundo ejemplo que las usa) y 2) el jerarquía de objetivos, que incluye 2A) comparaciones de todos los criterios entre sí PRIMERO, y luego 2B) comparaciones de los criterios con las alternativas. Guardé mi archivo YAML como tomdickharry.txt y lo puse en mi directorio C:/AHP/tdh:
Para instalarla debes seguir los pasos siguientes (revisa si ya has instalado devtools)
devtools::install_github("gluc/ahp", build_vignettes = TRUE)
## Skipping install of 'ahp' from a github remote, the SHA1 (4a85f6e8) has not changed since last install.
## Use `force = TRUE` to force installation
install.packages("data.tree")
## Installing package into '/home/rpalma/R/x86_64-pc-linux-gnu-library/4.1'
## (as 'lib' is unspecified)
library(ahp)
library(data.tree)
Para generar la salida del procesamiento los pasos son ridiculamente sencillos.
## Seleccion de nueve gerente
# setwd("C:/AHP/tdh" o la carpeta que hayas creado tu)
#getwd()
#list.files()
download.file("https://themys.sid.uncu.edu.ar/rpalma/R-cran/AHP/AHP_Simple/tom_dick_harry.ahp", "candidatos.ahp")
myAhp <- Load("candidatos.ahp")
Calculate(myAhp)
Analyze(myAhp)
## Weight Dick Tom Harry Inconsistency
## 1 Choose the Most Suitable Leader 100.0% 48.1% 38.5% 13.4% 4.5%
## 2 ¦--Experience 54.8% 34.9% 14.1% 5.7% 3.7%
## 3 ¦--Charisma 27.0% 5.2% 20.1% 1.7% 6.8%
## 4 ¦--Education 12.7% 4.2% 2.8% 5.6% NA
## 5 °--Age 5.6% 3.8% 1.5% 0.4% 2.8%
AnalyzeTable(myAhp)
Weight | Dick | Tom | Harry | Inconsistency | |
---|---|---|---|---|---|
Choose the Most Suitable Leader | 100.0% | 48.1% | 38.5% | 13.4% | 4.5% |
Experience | 54.8% | 34.9% | 14.1% | 5.7% | 3.7% |
Charisma | 27.0% | 5.2% | 20.1% | 1.7% | 6.8% |
Education | 12.7% | 4.2% | 2.8% | 5.6% | NA |
Age | 5.6% | 3.8% | 1.5% | 0.4% | 2.8% |
Para ver otros ejemplo de cómo formar el archivo ahp busca en tu disco estos archivos
library(ahp)
#list example files provided by the package
list.files(system.file("extdata", package="ahp"))
## [1] "car.ahp" "tom_dick_harry.ahp" "vacation.ahp"
#load a specific example
# En este caso tomamos los archivos directamente de la bibloteca
# Si Windows 11 no carga estos datos es por la actualización de seguridad que está # instalando Microsoft en estos dias
# ahpFile <- system.file("extdata", "car.ahp", package="ahp")
download.file("https://themys.sid.uncu.edu.ar/rpalma/R-cran/AHP/AHP_Simple/car.ahp", "autos.ahp")
carAhp <- Load("autos.ahp")
Calculate(carAhp)
Analyze(carAhp)
## Weight Odyssey Accord Sedan CR-V Element
## 1 Buy Car 100.0% 22.0% 21.0% 16.3% 15.1%
## 2 ¦--Cost 51.0% 5.9% 11.7% 11.7% 12.9%
## 3 ¦ ¦--Purchase Price 24.9% 2.3% 6.1% 6.1% 9.1%
## 4 ¦ ¦--Fuel Cost 12.8% 2.2% 1.9% 2.1% 2.3%
## 5 ¦ ¦--Resale Value 8.2% 1.1% 1.9% 2.9% 1.1%
## 6 ¦ °--Maintenance Cost 5.1% 0.3% 1.8% 0.5% 0.4%
## 7 ¦--Safety 23.4% 10.2% 5.1% 0.8% 0.5%
## 8 ¦--Capacity 21.5% 5.7% 2.8% 3.1% 1.5%
## 9 ¦ ¦--Passenger Capacity 17.9% 4.9% 2.4% 2.4% 0.8%
## 10 ¦ °--Cargo Capacity 3.6% 0.8% 0.3% 0.7% 0.7%
## 11 °--Style 4.1% 0.3% 1.5% 0.6% 0.1%
## Accord Hybrid Pilot Inconsistency
## 1 14.1% 11.5% 7.5%
## 2 4.8% 4.0% 1.5%
## 3 0.6% 0.6% 6.8%
## 4 1.7% 2.6% 0.0%
## 5 0.9% 0.3% 3.2%
## 6 1.6% 0.4% 2.3%
## 7 5.1% 1.8% 8.0%
## 8 2.8% 5.6% 0.0%
## 9 2.4% 4.9% 0.0%
## 10 0.3% 0.7% 0.4%
## 11 1.5% 0.2% 10.1%
AnalyzeTable(carAhp)
Weight | Odyssey | Accord Sedan | CR-V | Element | Accord Hybrid | Pilot | Inconsistency | |
---|---|---|---|---|---|---|---|---|
Buy Car | 100.0% | 22.0% | 21.0% | 16.3% | 15.1% | 14.1% | 11.5% | 7.5% |
Cost | 51.0% | 5.9% | 11.7% | 11.7% | 12.9% | 4.8% | 4.0% | 1.5% |
Purchase Price | 24.9% | 2.3% | 6.1% | 6.1% | 9.1% | 0.6% | 0.6% | 6.8% |
Fuel Cost | 12.8% | 2.2% | 1.9% | 2.1% | 2.3% | 1.7% | 2.6% | 0.0% |
Resale Value | 8.2% | 1.1% | 1.9% | 2.9% | 1.1% | 0.9% | 0.3% | 3.2% |
Maintenance Cost | 5.1% | 0.3% | 1.8% | 0.5% | 0.4% | 1.6% | 0.4% | 2.3% |
Safety | 23.4% | 10.2% | 5.1% | 0.8% | 0.5% | 5.1% | 1.8% | 8.0% |
Capacity | 21.5% | 5.7% | 2.8% | 3.1% | 1.5% | 2.8% | 5.6% | 0.0% |
Passenger Capacity | 17.9% | 4.9% | 2.4% | 2.4% | 0.8% | 2.4% | 4.9% | 0.0% |
Cargo Capacity | 3.6% | 0.8% | 0.3% | 0.7% | 0.7% | 0.3% | 0.7% | 0.4% |
Style | 4.1% | 0.3% | 1.5% | 0.6% | 0.1% | 1.5% | 0.2% | 10.1% |
Visualize(carAhp)
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
carAhp
## levelName
## 1 Buy Car
## 2 ¦--Cost
## 3 ¦ ¦--Purchase Price
## 4 ¦ ¦ ¦--Accord Sedan
## 5 ¦ ¦ ¦--Accord Hybrid
## 6 ¦ ¦ ¦--Pilot
## 7 ¦ ¦ ¦--CR-V
## 8 ¦ ¦ ¦--Element
## 9 ¦ ¦ °--Odyssey
## 10 ¦ ¦--Fuel Cost
## 11 ¦ ¦ ¦--Accord Sedan
## 12 ¦ ¦ ¦--Accord Hybrid
## 13 ¦ ¦ ¦--Pilot
## 14 ¦ ¦ ¦--CR-V
## 15 ¦ ¦ ¦--Element
## 16 ¦ ¦ °--Odyssey
## 17 ¦ ¦--Maintenance Cost
## 18 ¦ ¦ ¦--Accord Sedan
## 19 ¦ ¦ ¦--Accord Hybrid
## 20 ¦ ¦ ¦--Pilot
## 21 ¦ ¦ ¦--CR-V
## 22 ¦ ¦ ¦--Element
## 23 ¦ ¦ °--Odyssey
## 24 ¦ °--Resale Value
## 25 ¦ ¦--Accord Sedan
## 26 ¦ ¦--Accord Hybrid
## 27 ¦ ¦--Pilot
## 28 ¦ ¦--CR-V
## 29 ¦ ¦--Element
## 30 ¦ °--Odyssey
## 31 ¦--Safety
## 32 ¦ ¦--Accord Sedan
## 33 ¦ ¦--Accord Hybrid
## 34 ¦ ¦--Pilot
## 35 ¦ ¦--CR-V
## 36 ¦ ¦--Element
## 37 ¦ °--Odyssey
## 38 ¦--Style
## 39 ¦ ¦--Accord Sedan
## 40 ¦ ¦--Accord Hybrid
## 41 ¦ ¦--Pilot
## 42 ¦ ¦--CR-V
## 43 ¦ ¦--Element
## 44 ¦ °--Odyssey
## 45 °--Capacity
## 46 ¦--Cargo Capacity
## 47 ¦ ¦--Accord Sedan
## 48 ¦ ¦--Accord Hybrid
## 49 ¦ ¦--Pilot
## 50 ¦ ¦--CR-V
## 51 ¦ ¦--Element
## 52 ¦ °--Odyssey
## 53 °--Passenger Capacity
## 54 ¦--Accord Sedan
## 55 ¦--Accord Hybrid
## 56 ¦--Pilot
## 57 ¦--CR-V
## 58 ¦--Element
## 59 °--Odyssey
#the vacation.ahp file provides an example with multiple decision makers
ahpFile <- system.file("extdata", "vacation.ahp", package="ahp")
#download.file("https://themys.sid.uncu.edu.ar/rpalma/R-cran/AHP/AHP_Simple/vacation.ahp", "ahpFile.ahp")
vacationAhp <- Load(ahpFile)
Calculate(vacationAhp)
Visualize(vacationAhp)
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
## Warning: 'Node$fieldsAll' is deprecated.
## Use 'Node$attributesAll' instead.
## See help("Deprecated")
## Warning: 'Node$fields' is deprecated.
## Use 'Node$attributes' instead.
## See help("Deprecated")
Analyze(vacationAhp, decisionMaker = "Dad")
## Weight Mountains Beach Inconsistency
## 1 Vacation 100.0% 68.6% 31.4% 3.5%
## 2 ¦--Costs 71.7% 47.8% 23.9% 0.0%
## 3 ¦--Fun 21.7% 18.6% 3.1% 0.0%
## 4 °--Spa 6.6% 2.2% 4.4% 0.0%
AnalyzeTable(vacationAhp, decisionMaker = "Mom")
Weight | Beach | Mountains | Inconsistency | |
---|---|---|---|---|
Vacation | 100.0% | 78.7% | 21.3% | 6.8% |
Spa | 74.3% | 63.7% | 10.6% | 0.0% |
Fun | 19.4% | 12.9% | 6.5% | 0.0% |
Costs | 6.3% | 2.1% | 4.2% | NA |
AnalyzeTable(vacationAhp,
decisionMaker = "Kid",
variable = "priority",
sort = "orig",
pruneFun = function(node, dm) PruneByCutoff(node, dm, minWeight = 0.1))
Priority | Beach | Mountains | Inconsistency | |
---|---|---|---|---|
Vacation | 100.0% | 0.0% | ||
Fun | 81.8% | 100.0% | 0.0% | NA |