---
title: "Purchase Target"
author: "Philippe J.S. De Brouwer"
date: "`r Sys.Date()`"
output: 
 beamer_presentation:
    theme: "metropolis"
    fonttheme: "structurebold"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)

library(tidyverse)
require(diagram)
#library(knitr)

source("mcda_functions.R")
```

```{r loadData}
fl <- "/home/philippe/Documents/teaching/courses/r-book/excercises/MCDA/company_purchase/company_purchase.csv"
d <- read_csv(fl) %>%
  as.data.frame()
```

# The Decision Matrix

## The Data {.shrink}

We won't use the logo. So, here is our decision matrix:
```{r theData}
rownames(d) <- d$company
M0 <- d %>%
  select(-c(company, logo))
knitr::kable(M0)
```


## Normalised decision matrix
All values are between 0 (worst) and 1 (best)

```{r normalise}
min_max <- function(x, .reverse = FALSE) {
  xx <- (x - min(x, na.rm = T)) / (max(x, na.rm = T) - min(x, na.rm = T))
  if(.reverse) xx <- 1 - xx
  xx
}

Mn <- data.frame(price = min_max(M0$price, .reverse = TRUE),
              sales = if_else(M0$sales_potential == "low", 0, 
                              if_else(M0$sales_potential == "medium", 0.5, 1)),
              eng   = if_else(M0$engineering == "low", 0, 
                              if_else(M0$engineering == "good", 0.5, 1)),
              team  = if_else(M0$sales_potential == "low", 0, 
                              if_else(M0$sales_potential == "good", 0.5, 1)),
              loan = if_else(M0$loan == "no", 1, 0),
              score = min_max(M0$score)
                )
rownames(Mn) <- d$company
knitr::kable(round(Mn, 2))
```

# MCDA Methods

## Dominance
We only want to purchase **one** company, so we can leave out the dominated solutions. An arrow from $X$ to $Y$ means "$X$ dominates $Y$".
```{r}
Dom <- mcda_get_dominants(Mn)
plot(Dom, curve = 0)
```

## After deleting the dominated rows {.shrink}

```{r dominance}

M1 <- mcda_del_dominated(Mn)
knitr::kable(round(M1,2))
```

Then we re-scale again:
```{r}
M1 <- M1 %>%
  mcda_rescale_dm()
knitr::kable(round(M1,2))
```

## Weighted Sum Method: the weights
We decide on weights that reflect our preference for `price`, `sales capacity`, and to some extend the `engineering` capability of the purchase target:
```{r WSM1}

# The critia: "price" "sales" "eng" "team" "loan" "score"
w <- c(        1,      0.75,   0.5,   0.2,  0.3,  0.1)
w <- w / sum(w)  # the sum was 1 already, but just to be sure.

x <- as.data.frame(cbind(colnames(Mn), round(w, 2)))
colnames(x) <- c("criterion", "weight")
knitr::kable(x)
```

## Weighted Sum Method: the result
```{r wsm2}
# Now we can execute our function mcda_wsm():
wsmScore <- mcda_wsm_score(as.matrix(M1), w)
# ---------------------------------------------------------------------

# Whith the normalised decision matrix M1 and the weights w, we calculate the score matrix:
sM <- mcda_wsm_score(wsmScore, w)

# Then we plot the result:
plot(sM)
```

## WSM with all alternatives

```{r wsmwithall}
# use the function for wsm on the normalised matrix Mn
wsmScore <- mcda_wsm_score(as.matrix(Mn), w)
sM <- mcda_wsm_score(wsmScore, w)
# Then we plot the result:
plot(sM)
```   

## WSM: conslusions

#### Conclusions WSM
The target company `D` is the best, followed by `E`. `A` is not a suitable target since it is the worst for Engineering and Sales.

Notes:

- `score` has too small weights in order to influence the choice presented here.
- The dominated solution `G` is actually the third best.


## PCA and 3 clusters
Note that we included all alternatives (also the dominated ones)
```{r pca}
pca1 <- prcomp(Mn)
library(ggplot2)
library(ggfortify)
library(cluster)

# Autoplot with labels colored
#autoplot(pca1, data = Mn, label = TRUE, shape = FALSE, colour='price', label.size = 6,
#         loadings = TRUE, loadings.colour = 'blue',
#         loadings.label = TRUE, loadings.label.size = 6
#         )

# Autoplot with visualization of 2 clusters

#autoplot(fanny(Mn,3), label=TRUE, frame=TRUE,  label.size = 16,
#         loadings = TRUE,  loadings.colour = 'blue',
#         loadings.label = TRUE, loadings.label.size = 10)

# Use the weights as defined above for the decision vector:
#w

# Calculate coordinates
dv1 <- sum( w * pca1$rotation[,1])  # decision vector PC1 component
dv2 <- sum( w * pca1$rotation[,2])  # decision vector PC2 component

p <- autoplot(pam(Mn,3), frame=TRUE, #frame.type='norm',
              label=TRUE, 
         shape=FALSE,
         #label.colour='blue',
         label.face='bold', label.size=12,
         loadings=TRUE,  loadings.colour = 'dodgerblue4',
         loadings.label = TRUE, loadings.label.size = 9, 
 	       loadings.label.colour='dodgerblue4',
         loadings.label.vjust = -0.60, loadings.label.hjust = 0.54
         )
p <- p + scale_y_continuous(breaks = 
                        round(seq(from = -1, to = +1, by = 0.2), 2))
p <- p + scale_x_continuous(breaks = 
                        round(seq(from = -1, to = +1, by = 0.2), 2))
p <- p + geom_segment(aes(x=0, y=0, xend=dv1, yend=dv2), size = 2,
                      arrow = arrow(length = unit(0.5, "cm")))
p <- p + ggplot2::annotate("text", x = dv1+0.0, y = dv2+0.05, 
                  label = "decision vector",
                  colour = "black", fontface = 2)
p

```

## Conclusions PCA

The projection in the plane $(PCA1, PCA2)$ maximizes the distances between the different alternatives. We note that

- D is the most in the direction of the decision vector and hence the best target.
- G and E are close to the direction of the decision vector and hence acceptable, but not as good as D. Therefore the blue cluster is good to retain (in case something goes wrong with D, G might be an acceptable backup)
- the green cluster (F, H, and C) are solutions that score good on `team` and `sales`, but remember that these were the attributes that interested us least.
- A, B and C are opposite to the decision vector and hence really bad solutions.

# Conclusions

## Decission

 - A: against (only price is good)
 - B: against (good price, acceptable engineering, but no sales)
 - C: against (certainly not - dominated by H)
 - **D**: in favor (my preferred solution -- note that we agree with the strategy team for solution `D`)
 - E: in favor (if D and G didn't pass)
 - F: against (is dominated by D -- plus we don't need the team -- note that we disagree with the strategy team for this target)
 - G: against (is dominated by D, but without D is a good choice -- it is second best)
 - H: in favor (if we need sales, and sales alone)