---
title: "Penalized Discriminant Analysis on Music Dataset"
output: html_document
date: '2022-07-09'
---

```{r}
source("fun.R")
library(classPP)
library(tidyr)
library(DiscriMiner)
library(caret)
```

# Music

```{r}
#read in dataset & train-test-split
df = read.csv("../data/music.csv")
df$name = substring(df$name, 19)
temp = separate(df, name, 
                into = c("class", "artist", "rm"), sep="_") 
df = temp[, -3]
music = as.matrix(df[,-c(1, 2)])
music = scale(music)
class = df$class

ind = sample(1:52, 13, replace=FALSE)
test = music[ind,]
train = music[-ind,]
cls_test = class[ind]
cls_train = class[-ind]
cls_tot = levels(as.factor(class))
```

### genre

```{r}
#implement lda & pda
par(mfrow = c(2, 2)) 
PP.opt = PP.optimize.anneal("LDA",2,train,cls_train)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("LDA")
acc(PP.opt$proj.best, t(train), t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.4)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.4")
acc(PP.opt$proj.best, t(train), t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.6)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.4")
acc(PP.opt$proj.best, t(train), t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.8)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.8")
acc(PP.opt$proj.best,t(train),t(test), cls_train, cls_test)
```

```{r}
#implement svm
train2 = cbind(as.data.frame(train), cls_train)
train_ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
svm_classifier <- train(cls_train ~., data = train2, 
                               method = "svmLinear", 
                               trControl=train_ctrl,
                               tuneLength = 10)
print(sum(predict(svm_classifier, test) == cls_test)/nrow(test))

svm_classifier <- train(cls_train ~., data = train2, 
                               method = "svmRadial", 
                               trControl=train_ctrl,
                               tuneLength = 10)
print(sum(predict(svm_classifier, test) == cls_test)/nrow(test))
```

### artist

```{r}
class_artist = df$artist
cls_test = class_artist[ind]
cls_train = class_artist[-ind]
cls_tot = levels(as.factor(class_artist))

par(mfrow = c(2, 2)) 
PP.opt = PP.optimize.anneal("LDA",2,train,cls_train)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("LDA")
acc(PP.opt$proj.best, t(train), t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.4)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.4")
acc(PP.opt$proj.best, t(train), t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.6)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.8")
acc(PP.opt$proj.best,t(train),t(test), cls_train, cls_test)

PP.opt = PP.optimize.anneal("PDA",2,train,cls_train,lambda = 0.8)
proj.data.test = as.matrix(test)%*%PP.opt$proj.best
proj.data.train = as.matrix(train)%*%PP.opt$proj.best
plot_test_train(proj.data.test, proj.data.train, cls_test, cls_train, cls_tot)
title("PDA, lambda = 0.8")
acc(PP.opt$proj.best,t(train),t(test), cls_train, cls_test)
```

```{r}
#implement svm
train2 = cbind(as.data.frame(train), cls_train)
train_ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
svm_classifier <- train(cls_train ~., data = train2, 
                               method = "svmLinear", 
                               trControl=train_ctrl,
                               tuneLength = 10)
print(sum(predict(svm_classifier, test) == cls_test)/nrow(test))

svm_classifier <- train(cls_train ~., data = train2, 
                               method = "svmRadial", 
                               trControl=train_ctrl,
                               tuneLength = 10)
print(sum(predict(svm_classifier, test) == cls_test)/nrow(test))
```


