---
title: "LDA: Banknotes"
author: "Gustavo Cepparo and Milica Cudina"
output:
  pdf_document: default
---


We consider another **banknote** data set. It exists in the `MixGHD` library. 

```{r}
#install.packages("MixGHD")
library(MixGHD)
```

The data set `banknote` is used to predict the `Status` of Swiss Franc bills based on measurements of the bills. 

```{r}
data(banknote)
names(banknote)
attach(banknote)
data<-banknote
```

We can start with visualization (as usual). 

```{r}
pairs(data, 
      col="lightblue", pch=19)
```

Our aim is to perform the linear discriminant analysis here. First, we will partition our data into training and testing. 

```{r}
set.seed(1)
ind.train <- sample(nrow(data), floor(nrow(data)*0.6))
training<-data[ind.train,]
testing<-data[-ind.train,]
```

My goal with this example is to ultimately demonstrate how to plot a partition in the plane. So, I will "arbitrarily" choose two explanatory variables: `Top` and `Diagonal`.

```{r}
library(MASS)
linear <- lda(Status~Top+Diagonal, data=training)
#summary(linear)
linear
```
Now, we try to create a partition plot. 

```{r}
#install.packages("klaR")
library(klaR)
partimat(Status~Top+Diagonal, data = training, method = "lda")
partimat(Status~Top+Diagonal, data = training, method = "qda")
```

Finally, let's consider the confusion matrix and accuracy, both on the training set and the testing set. 

```{r}
#the training set
p.tr <- predict(linear, training)$class
tab.tr <- table(Predicted = p.tr, Actual = training$Status)
tab.tr
sum(diag(tab.tr))/sum(tab.tr)
#this is the error rate indicated in the label on the plot above
1-sum(diag(tab.tr))/sum(tab.tr)

#the testing set
p.te <- predict(linear, testing)$class
tab.te <- table(Predicted = p.te, Actual = testing$Status)
tab.te
sum(diag(tab.te))/sum(tab.te)
```




