0

I would like to perform Analytical Hierarchy Analysis in R using ahpsurvey.

I will use the table below as an example of my dataset.

Location Var1
A 8
B 3
C 7
D 6
E 4

This is the Satty rating I would like to incorporate :

Rating Explanation
1/9 The preferred characteristics are absolutely less important
1/7 The preferred characteristics are strongly less important
1/5 The preferred characteristics are moderately less important
1/3 The preferred characteristics are slightly less important
1 Two characteristics are equally important
3 The preferred characteristics are slightly more important
5 The preferred characteristics are moderately more important
7 The preferred characteristics are strongly more important
9 The preferred characteristics are absolutely more important

Step 3. This is where I am stuck. I would like to create a loop which compares the values of the variable against one another, make a rating using the Satty ranking above.

num_alternatives <- length(location)
  
mat <- matrix(0, nrow = num_alternatives, ncol = num_alternatives)

colnames(comparison_matrix) <- location
rownames(comparison_matrix) <- location 
  
#making comparisons between the value of Var1 of location A and the others
   
  x_dta$a_diff <- 8 - x_dta$Var1
  x_dta$a_diff <- as.character(x_dta$a_diff)
  
  #assigned Satty ranking to the differences 
  diff_lookup <- c(`0` = 1, `5` = 7, `1` = 3, `2` = 5, `4` =7) 
  
  #location A comparison with other locations.
  mat["A","B"] <- 7 
  mat["A","C"] <- 3
  mat["A","D"] <- 5 
  mat["A","E"] <- 7

#followed the same process until I had a full matrix. The problem is that I have over 100 locations in my dataset and filling the matrix one by one is time consuming. 
Mizzlwe
  • 1
  • 1
  • You need at a minimum to show `str(x_dta)` AND explain the connections between those two datasets, but preferably to show output of dput(head(x_dta)). At the moment I do not see any effort to incorporate the "Satty" rating. It might also help to offer a link to an online explanation of "Analytical Hierarchy Analysis" since it's not a statistical method I recognize and I'd hazard a guess that I know more statistics than 95% of the SO contributors. – IRTFM Mar 18 '23 at 19:10
  • How do you want to use the pairwise matrix to connect to the table of satty rating? Maybe you can provide, how the result should look (e.g. doing the calculation manually instead of using R) - then we can help you to create the code. – JKupzig Mar 20 '23 at 08:30
  • @IRTFM I meant the Analytic Hierarchy Process (AHP) https://cran.r-project.org/web/packages/ahpsurvey/vignettes/my-vignette.html – Mizzlwe Mar 20 '23 at 18:08
  • @JKupzig, the intent is to compare the locations based on the value of var1. I will update the question to reflect what I have done so far. – Mizzlwe Mar 20 '23 at 18:16

1 Answers1

0

Generate 100 location names

(location <- apply(expand.grid(LETTERS, LETTERS), 1, function(x) paste0(x, collapse = ""))[1:100])

IT'S IMPORTANT. Now you should decide about your variable range. Saaty's scale has only 9 values, so differences of the variable values for locations must be only 4 classes. You must aggregate differences into 9 classes or initial values into 4 classes. In your example I see the lowest value is 3 and the highest value is 8. You don't have any 5-s. But if you have them, you will get possible differences

-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5

You can't aggregate it into 9 classes. So your variable's range can be only 4 (e.g. from 3 to 7), not 5 (from 3 to 8).

Generate 100 random values

values <- floor(runif(length(location), 3, 8)) # values from 3 to 7!
table(values)

Make the template matrix

num_alternatives <- length(location)

mat <- matrix(NA, nrow = num_alternatives, ncol = num_alternatives)

colnames(mat) <- location
rownames(mat) <- location 

Make vector of values for Saaty's scores

(saaty_scores <- c(1/seq(9,3,-2), 1, seq(3,9,2))

You have small amount of data, so why you ignore simple loops?

A loop to fill the matrix

for (i in 1:nrow(mat)) {
  for (j in 1:ncol(mat)) {
    mat[i,j] <- saaty_scores[values[i] - values[j] + 5] # 5 - for shifting differences from the range [-4,4] to the range [1:9] to use it as an index
  }
}

You should make (for the ahpsurvey package) a list of some matrices from each decision-maker (even if you have only one maker), so:

my_pairwise_mats <- list()
my_pairwise_mats[[1]] <-  mat