Package 'ahpsurvey'

Title: Analytic Hierarchy Process for Survey Data
Description: The Analytic Hierarchy Process is a versatile multi-criteria decision-making tool introduced by Saaty (1987) <doi:10.1016/0270-0255(87)90473-8> that allows decision-makers to weigh attributes and evaluate alternatives presented to them. This package provides a consistent methodology for researchers to reformat data and run analytic hierarchy process in R on data that are formatted using the survey data entry mode. It is optimized for performing the analytic hierarchy process with many decision-makers, and provides tools and options for researchers to aggregate individual preferences and test multiple options. It also allows researchers to quantify, visualize and correct for inconsistency in the decision-maker's comparisons.
Authors: Frankie Cho [aut, cre]
Maintainer: Frankie Cho <[email protected]>
License: MIT + file LICENSE
Version: 0.4.2
Built: 2025-02-21 04:15:54 UTC
Source: https://github.com/frankiecho/ahpsurvey

Help Index


Canned routine for AHP

Description

ahp is a canned routine that returns a data.frame of individuals' priorities (based on ahp.indpref), number of missing values, consistency ratios, and top three pairs of inconsistent pairwise comparisons. An optional argument agg allows the user to generate a data.frame of aggregated priorities alongside individual priorities.

Usage

ahp(
  df,
  atts,
  negconvert = FALSE,
  reciprocal = TRUE,
  method = "eigen",
  aggmethod = "geometric",
  qt = 0,
  censorcr = Inf,
  agg = FALSE,
  ID = NULL,
  col = NULL,
  suppress = "none"
)

Arguments

df

a dataframe, each row corresponding to one decision-maker, with columns ordered according to atts.

atts

a list of attributes in the correct order

negconvert

logical, whether to convert all positive values to negative. In the pairwise comparison A_B, if -6 denotes A is more important than B by 6 units, set negconvert = TRUE.

reciprocal

logical, whether to convert negative values (after negconvert) to its reciprocal. If the comparison A_B where B is more important than A was already entered in its reciprocal (e.g. 1/6), choose reciprocal = FALSE. When reciprocal = FALSE, do not set negconvert = TRUE.

method

if method = "eigen", the individual priority weights are computed using the Dominant Eigenvalues method described in Saaty (2003). Otherwise, then the priorities are computed based on the averages of normalized values. Basically it normalizes the matrices so that all of the columns add up to 1, and then computes the averages of the row as the priority weights of each attribute. Three modes of finding the averages are available: arithmetic: the arithmetic mean; geometric: the geometric mean (the default); rootmean: the square root of the sum of the squared value.

aggmethod

how to aggregate the individual priorities. By default aggmethod = method. Apart from the methods offered in method, aggmethod also permits three other options: tmean computes the trimmed arithmetic mean, tgmean computes the trimmed geometric mean (both with quantiles trimmed based on qt), and sd computes the standard deviation from the arithmetic mean. If method = "eigen" and aggmethod is not specified, aggmethod defaults to "geometric".

qt

specifies the quantile which the top and bottom priority weights are trimmed. Used only if aggmethod = 'tmean' or aggmethod = 'tgmean'. For example, qt = 0.25 specifies that the aggregation is the arithmetic mean of the values from the 25 to 75 percentile. By default qt = 0.

censorcr

the threshold of censoring observations. All observations with a consistency ratio higher than the specified CR will be dropped from the routine. By default, censorcr = Inf, i.e. drops no observations.

agg

by default, agg = FALSE, which suppresses the output of Aggregated priorities. If agg = TRUE, the output will be a list where ⁠$AggPref⁠ contains the Aggregated priorities and Standard Deviations.

ID

the column name, or a vector of column names, of variables in df, which are used to indicate specific observations. For example, if a survey dataset comes with an identifier/ multiple identified for each observation, use this so that the output dataframe would contain this identifier.

col

a list of column names which contains the columns for the pairwise comparison matrix in chronological order, as specified in atts. For example, if atts is c("A","B","C"), then col would be c("A_B","A_C","B_C"), with "A_B" being a pairwise comparison of attribute A and B. col = NULL by default, indicating that the dataframe specified already contained the pairwise comparisons in correct order and nothing else except of what is inside ID.

suppress

Suppresses the supplementary indices. Can take the values of "all" (suppresses all indices), "pwerror" (suppresses pwerror), or "cr" (suppresses consistency ratio).

Value

If agg = TRUE, returns a list of the results with two elements, indpref and aggpref. ⁠$indpref⁠ contains individual preference weight (same as ahp.indpref), consistency ratios, and the top three most inconsistent pairwise comparisons (same as ahp.pwerror). ⁠$aggpref⁠ contains the Aggregated Priorities and Standard Deviations. Otherwise (i.e. default setting), outputs a data.frame with only ⁠$indpref⁠.

Author(s)

Frankie Cho

References

Saaty TL (2004). “Decision making — the Analytic Hierarchy and Network Processes (AHP/ANP).” Journal of Systems Science and Systems Engineering, 13(1), 1–35. ISSN 1861-9576, doi:10.1007/s11518-006-0151-5.

Examples

data(city200)
ahp(df = city200, atts = c('cult', 'fam', 'house', 'jobs', 'trans'), negconvert = TRUE)

Aggregate individual judgments

Description

Aggregate individual judgments from pairwise comparison matrices

Usage

ahp.aggjudge(ahpmat, atts, aggmethod = "geometric", qt = 0)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order

aggmethod

The method of aggregating the judgments by all decision-makers. Five modes aggregation are available: arithmetic: the arithmetic mean; geometric: the geometric mean (the default); rootmean: the square root of the sum of the squared value, tmean: the trimmed mean, tgmean: trimmed geometric mean. The quantiles trimmed are based on qt. It can also be set to sd, where it reports the standard deviation from the arithmetic mean.

qt

specifies the quantile which the top and bottom priority weights are trimmed. Used only if aggmethod = 'tmean' or aggmethod = 'tgmean'. For example, qt = 0.25 specifies that the aggregation is the arithmetic mean of the values from the 25 to 75 percentile. By default qt = 0.

Value

A data.frame of the aggregated pairwise judgments of all the decision-makers.

Author(s)

Frankie Cho

Examples

## Computes individual judgments with geometric mean and aggregates them
## with a trimmed arithmetic mean

data(city200)
atts <- c('cult', 'fam', 'house', 'jobs', 'trans')

cityahp <- ahp.mat(df = city200, atts = atts, negconvert = TRUE)
ahp.aggjudge(cityahp, atts, aggmethod = 'tmean', qt = 0.1)

Aggregate priority weights

Description

Compute and aggregate individual priority weights from pairwise comparison matrices

Usage

ahp.aggpref(ahpmat, atts, method = "geometric", aggmethod = method, qt = 0)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order

method

if method = "eigen", the individual priority weights are computed using the Dominant Eigenvalues method described in Saaty (2003). Otherwise, then the priorities are computed based on the averages of normalized values. Basically it normalizes the matrices so that all of the columns add up to 1, and then computes the averages of the row as the priority weights of each attribute. Three modes of finding the averages are available: arithmetic: the arithmetic mean; geometric: the geometric mean (the default); rootmean: the square root of the sum of the squared value.

aggmethod

how to aggregate the individual priorities. By default aggmethod = method. Apart from the methods offered in method, aggmethod also permits three other options: tmean computes the trimmed arithmetic mean, tgmean computes the trimmed geometric mean (both with quantiles trimmed based on qt), and sd computes the standard deviation from the arithmetic mean. If method = "eigen" and aggmethod is not specified, aggmethod defaults to "geometric".

qt

specifies the quantile which the top and bottom priority weights are trimmed. Used only if aggmethod = 'tmean' or aggmethod = 'tgmean'. For example, qt = 0.25 specifies that the aggregation is the arithmetic mean of the values from the 25 to 75 percentile. By default qt = 0.

Value

A data.frame of the aggregated priorities of all the decision-makers.

Author(s)

Frankie Cho

References

Saaty TL (2003). “Decision-making with the AHP: Why is the principal eigenvector necessary.” European Journal of Operational Research, 145(1), 85 - 91. ISSN 0377-2217, http://www.sciencedirect.com/science/article/pii/S0377221702002278.

Examples

## Computes individual priorities with geometric mean and aggregates them
## with a trimmed arithmetic mean

library(magrittr)

data(city200)
atts <- c('cult', 'fam', 'house', 'jobs', 'trans')

cityahp <- ahp.mat(df = city200, atts = atts, negconvert = TRUE)
ahp.aggpref(cityahp, atts, method = 'geometric', aggmethod = 'tmean', qt = 0.1)

Calculates the consistency ratio of each decision-maker

Description

The ahp.cr function calculates the consistency ratio of each decision-maker, defined by the following equation:

CR=(λn)/((n1)(RI))CR = (\lambda-n)/((n-1)(RI))

Where λ\lambda is the maximum eigenvalue of the pairwise comparison matrix, nn is the number of attributes, and RI is the random index. Following Saaty and Tran (2007), the RI is a function of nn and is the consistency ratio of randomly generated pairwise comparison matrices.

Usage

ahp.cr(ahpmat, atts, ri = NULL)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order. The RI is asymptotic as it approaches n=15, thus it is set to be equal to 1.6 if the number of attributes exceeds 16.

ri

A user-supplied random index value, probably user generated using ahp.ri.

Value

A list of consistency ratios of each decision-maker.

Author(s)

Frankie Cho

References

Saaty TL, Tran LT (2007). “On the invalidity of fuzzifying numerical judgments in the Analytic Hierarchy Process.” Mathematical and Computer Modelling, 46(7), 962 - 975. ISSN 0895-7177, Decision Making with the Analytic Hierarchy Process and the Analytic Network Process, http://www.sciencedirect.com/science/article/pii/S0895717707000787.

Examples

data(city200)
atts <- c('cult', 'fam', 'house', 'jobs', 'trans')

cityahp <- ahp.mat(df = city200, atts = atts, negconvert = TRUE) 
ahp.cr(cityahp, atts)

Matrix of the product between the pairwise comparison value and pj/pi

Description

Consider the comparison matrix where element aija_{ij} contains the pairwise comparison between the attributes i and j. The weights of the matrix was constructed as in agg.indpref using the Perron eigenvector where pip_{i} and pjp_{j} are the weights of the ithi^{th} and the jthj^{th} element respectively. ahp.error constructs a matrix ϵij=aijpj/pi\epsilon_{ij} = a_{ij}p_{j}/p_{i}.

Usage

ahp.error(ahpmat, atts, reciprocal = FALSE)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order

reciprocal

whether to remove all numbers lower than 1 and put all numbers above 1 in the upper triangular matrix. Useful for visualizing the inconsistency rapidly.

Value

A list of matrices containing ϵij=aijpj/pi\epsilon_{ij} = a_{ij}p_{j}/p_{i} for each decision-maker, with elements from the lower triangle set as NA automatically (since it is essentially equal to the element in the upper triangle).

Author(s)

Frankie Cho

References

Saaty TL (2004). “Decision making — the Analytic Hierarchy and Network Processes (AHP/ANP).” Journal of Systems Science and Systems Engineering, 13(1), 1–35. ISSN 1861-9576, doi:10.1007/s11518-006-0151-5.

Examples

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

data(city200)

cityahp <-  ahp.mat(city200, atts, negconvert = TRUE)
ahp.error(cityahp, atts)

Replace inconsistent pairwise comparisons

Description

Deprecated in version 0.4.2. Use ahp.md instead, which contains exactly the same functionality but with the correct name of the algorithm.

Usage

ahp.harker(
  ahpmat,
  atts,
  round = FALSE,
  limit = FALSE,
  iterations = 1,
  stopcr = 0,
  printiter = TRUE
)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

A list of attributes in the correct order

round

Rounds the imputation values of the matrix to the nearest integer if TRUE. Defaults to FALSE.

limit

If set to TRUE, if the imputation value is larger than 9 or smaller than 1/9, the value is converted to 9 and 1/9 respectively. Defaults to FALSE.

iterations

The number of comparisons with the highest inconsistency to be changed. For example, if iterations = 3, ahp.harker changes the first, second, and third most inconsistent pairwise comparisons using that method. Defaults to 1.

stopcr

The stopping Consistency Ratio, which was calculated by ahp.cr. Complements iter by giving iterations a criteria to stop when a matrix is sufficiently consistent. The function will continue looping and replacing more elements of the pairwise comparison matrices until the consistency ratio of the new matrix is lower than stopcr, or the maximum number of iterations is reached, and will stop and move onto the next individual. When stopcr is set, the number of replaced elements will differ among each decision-maker. Defaults to 0 (i.e. the loop will not be stopped unless iterations is reached)

printiter

Whether the number of iterations taken for each pairwise matrix is reported or not. If printiter = TRUE, it prints out the final number of iterations that each individual decision-maker took to reach a value lower than stopcr. 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. Defaults to TRUE.

Value

A list of matrices with values replaced with consistent values.

Author(s)

Frankie Cho

References

There are no references for Rd macro ⁠\insertAllCites⁠ on this help page.

See Also

ahp.error ahp.cr

Examples

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

data(city200)

cityahp <- ahp.mat(city200, atts, negconvert = TRUE)
ahp.md(cityahp, atts)

Computes priority weights of individual decision-makers

Description

ahp.indpref computes the individual priorities of the decision-makers, and returns a data.frame containing the priority weights of the decision-makers.

Usage

ahp.indpref(ahpmat, atts, method = "geometric")

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order

method

if method = "eigen", the individual priority weights are computed using the Dominant Eigenvalues method described in Saaty (2003). Otherwise, then the priorities are computed based on the averages of normalized values. Basically it normalizes the matrices so that all of the columns add up to 1, and then computes the averages of the row as the priority weights of each attribute. Three other modes of finding the averages are available: arithmetic: the arithmetic mean; geometric: the geometric mean (the default); rootmean: the square root of the sum of the squared value.

Value

A data.frame of the individual priorities of all the decision-makers.

Author(s)

Frankie Cho

References

Saaty TL (2003). “Decision-making with the AHP: Why is the principal eigenvector necessary.” European Journal of Operational Research, 145(1), 85 - 91. ISSN 0377-2217, http://www.sciencedirect.com/science/article/pii/S0377221702002278.

Examples

data(city200)
atts <- c('cult', 'fam', 'house', 'jobs', 'trans')

cityahp <- ahp.mat(df = city200, atts = atts, negconvert = TRUE) 
ahp.indpref(cityahp, atts, method = "eigen")

Generate AHP pairwise matrices from survey data

Description

ahp.mat takes in paired comparisons from survey data with questions using the analytic hierarchy process and converts it into pairwise comparison matrices for each individual decision-maker. Examples based on Saaty (2004).

Usage

ahp.mat(df, atts, negconvert = FALSE, reciprocal = TRUE)

Arguments

df

a dataframe, each row corresponding to one decision-maker, with columns ordered according to atts.

atts

a list of attributes in the correct order

negconvert

logical, whether to convert all positive values to negative. In the pairwise comparison A_B, if -6 denotes A is more important than B by 6 units, set negconvert = TRUE.

reciprocal

logical, whether to convert negative values (after negconvert) to its reciprocal. If the comparison A_B where B is more important than A was already entered in its reciprocal (e.g. 1/6), choose reciprocal = FALSE. When reciprocal = FALSE, do not set negconvert = TRUE.

Value

A list of pairwise comparison matrices of each decision-maker.

Author(s)

Frankie Cho

References

Saaty TL (2004). “Decision making — the Analytic Hierarchy and Network Processes (AHP/ANP).” Journal of Systems Science and Systems Engineering, 13(1), 1–35. ISSN 1861-9576, doi:10.1007/s11518-006-0151-5.

Examples

data(city200)
atts <- c('cult', 'fam', 'house', 'jobs', 'trans')
ahp.mat(df = city200, atts = atts, negconvert = TRUE)

Replace inconsistent pairwise comparisons with the maximum deviation method

Description

Based on the matrix derived from ahp.error, replaces the top n number of inconsistent pairwise comparisons with a value calculated by from the maximum deviation method. For a full explication of the method see Saaty (2003). Replaces the old ahp.harker function before 0.4.2 but contains exactly the same functionality.

Usage

ahp.md(
  ahpmat,
  atts,
  round = FALSE,
  limit = FALSE,
  iterations = 1,
  stopcr = 0,
  printiter = TRUE
)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

A list of attributes in the correct order

round

Rounds the imputation values of the matrix to the nearest integer if TRUE. Defaults to FALSE.

limit

If set to TRUE, if the imputation value is larger than 9 or smaller than 1/9, the value is converted to 9 and 1/9 respectively. Defaults to FALSE.

iterations

The number of comparisons with the highest inconsistency to be changed. For example, if iterations = 3, ahp.md changes the first, second, and third most inconsistent pairwise comparisons using that method. Defaults to 1.

stopcr

The stopping Consistency Ratio, which was calculated by ahp.cr. Complements iter by giving iterations a attribute to stop when a matrix is sufficiently consistent. The function will continue looping and replacing more elements of the pairwise comparison matrices until the consistency ratio of the new matrix is lower than stopcr, or the maximum number of iterations is reached, and will stop and move onto the next individual. When stopcr is set, the number of replaced elements will differ among each decision-maker. Defaults to 0 (i.e. the loop will not be stopped unless iterations is reached)

printiter

Whether the number of iterations taken for each pairwise matrix is reported or not. If printiter = TRUE, it prints out the final number of iterations that each individual decision-maker took to reach a value lower than stopcr. 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. Defaults to TRUE.

Value

A list of matrices with values replaced with consistent values.

Author(s)

Frankie Cho

References

Saaty TL (2003). “Decision-making with the AHP: Why is the principal eigenvector necessary.” European Journal of Operational Research, 145(1), 85 - 91. ISSN 0377-2217, http://www.sciencedirect.com/science/article/pii/S0377221702002278.

See Also

ahp.error ahp.cr

Examples

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

data(city200)

cityahp <- ahp.mat(city200, atts, negconvert = TRUE)
ahp.md(cityahp, atts)

Impute missing observations using the maximum deviation method.

Description

Imputes the missing values of a list of matrices produced by ahp.mat using the maximum deviation method. Missing values must be coded as NA. A minimum of n-1 comparisons must be made, where n is the number of attributes (assuming that the decision-maker is perfectly consistent). Note that the algorithm assumes that the NA values will be imputed under perfect consistency with the other pairwise comparisons made.

Usage

ahp.missing(ahpmat, atts, round = FALSE, limit = FALSE)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

A list of attributes in the correct order

round

Rounds the imputation values of the matrix to the nearest integer if TRUE. Defaults to FALSE.

limit

If set to TRUE, if the imputation value is larger than 9 or smaller than 1/9, the value is converted to 9 and 1/9 respectively. Defaults to FALSE.

Value

A list of matrices with all NA values imputed.

Author(s)

Frankie Cho

References

There are no references for Rd macro ⁠\insertAllCites⁠ on this help page.

Examples

library(magrittr)

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

data(city200)

set.seed(42)
## Make a dataframe that is missing at random
missing.df <- city200[1:10,]
for (i in 1:10){
  missing.df[i, round(stats::runif(1,1,10))] <- NA
}
missingahp <- ahp.mat(missing.df, atts, negconvert = TRUE)
ahp.missing(missingahp, atts)

Finds the pairwise comparisons with the maximum amount of inconsistency

Description

After constructing a list of matrices with ϵij=aijwj/wi\epsilon_{ij} = a_{ij}w_{j}/w_{i} (following ahp.error), this algorithm extracts the top n numbers of pairwise comparison matrices with the highest ϵij\epsilon_{ij} for each decision-maker.

Usage

ahp.pwerror(ahpmat, atts, npw = 3)

Arguments

ahpmat

A list of pairwise comparison matrices of each decision maker generated by ahp.mat.

atts

a list of attributes in the correct order

npw

Number of pairwise comparisons

Value

A list of matrices containing ϵij=aijwj/wi\epsilon_{ij} = a_{ij}w_{j}/w_{i} for each decision-maker, with elements from the lower triangle set as NA automatically (since it is essentially equal to the element in the upper triangle).

Author(s)

Frankie Cho

References

Saaty TL (2004). “Decision making — the Analytic Hierarchy and Network Processes (AHP/ANP).” Journal of Systems Science and Systems Engineering, 13(1), 1–35. ISSN 1861-9576, doi:10.1007/s11518-006-0151-5.

Examples

library(magrittr)

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

data(city200)

cityahp <- ahp.mat(city200, atts, negconvert = TRUE)
ahp.error(cityahp, atts)

Generate random indices

Description

The ahp.ri function calculates the mean consistency indices of a specific numbers of random number pairwise comparison matrices.

The random index of one pairwise comparison matrix is given as below, where lambdalambda is the maximum eigenvalue and nn is the number of attributes.

RI=(λn)/((n1)RI = (\lambda-n)/((n-1)

ahp.ri creates nsims number of pairwise comparison matrices with number of dimensions=dim, and returns its average.

Usage

ahp.ri(nsims, dim, seed = 42)

Arguments

nsims

Number of random pairwise comparison matrices to be generated. Processing time increases substantially with higher nsims.

dim

Number of dimensions of the matrix.

seed

The random number generator seed for reproducibility, which is same as set.seed. By default, seed = 42.

Value

The generated random index, which is numeric.

Author(s)

Frankie Cho

Examples

ahp.ri(nsims = 10000, dim = 5, seed = 42)

AHP Survey package

Description

The ahpsurvey package provides a workflow for researchers to reformat data and run analytic hierarchy process on data that are originally formatted using the survey data entry mode. A diversified toolbox is available for researchers to explore ways to impute missing pairwise comparisons or to adjust comparisons based on consistency, and to conveniently customize the methods which weights derived from surveys are extracted and aggregated.

Author(s)

Frankie Cho [email protected]


Dataframe of one decision-maker based on Saaty (2004)

Description

A data.frame of one decision-maker with weights the same as Saaty (2004), with the pairwise comparisons of atts: c('cult', 'fam', 'house', 'jobs', 'trans'), about a choosing the best city to live in based on five attributes: Culture, Family, House, Jobs, and Transportation. Negative values in the data denote that the attribute on the left is more important than the right, thus if used with ahp.mat, negconvert must be set to TRUE.

Usage

data(city1)

Format

A data frame with 1 row and 10 variables, which are pairwise comparisons of atts.

Source

Saaty (2004)


A dataframe of 200 simulated decision-makers based on Saaty (2004)

Description

A data.frame of one decision-maker with weights the same as Saaty (2004), with the pairwise comparisons of atts: c('cult', 'fam', 'house', 'jobs', 'trans'), about a choosing the best city to live in based on five attributes: Culture, Family, House, Jobs, and Transportation. The choices of 200 decision-makers are simulated based on the underlying weights and randomness generated by a normal random distribution. The true weights are stored in data(city1). The normal random distribution was set with a mean at the true weight value and a standard deviation of ⁠2,1,2,1.5,2,1.5,2.5,0.5,0.5,1⁠ in chronological order of the columns in city200.

Usage

data(city200)

Format

A data.frame with 200 row and 10 variables, which are pairwise comparisons of atts.

Source

Saaty TL (2004). “Decision making — the Analytic Hierarchy and Network Processes (AHP/ANP).” Journal of Systems Science and Systems Engineering, 13(1), 1–35. ISSN 1861-9576, doi:10.1007/s11518-006-0151-5.