Classifier

#Reformat the data
df1 <- read.table("Raw Data/AUC_AC discrimination_1.txt",sep="\t",header=TRUE)
df1.inv <- t(df1)
colnames(df1.inv) <- df1.inv[1,]
# df1.2 <- as.data.frame(df1.inv[-1,])
df1.2 <-df1.inv[-1,]
mode(df1.2) = "numeric"
# df1.2[,97]=rownames(df1.2)
#df1.2 <- cbind(df1.2,rownames(df1.2))
#colnames(df1.2)[97] <- "Odor"
#First two replicates are training set
df1.rep1 <- as.data.frame(df1.2[,seq(from=1,to=96,by=3)],stringsAsFactors = F)
df1.rep2 <- as.data.frame(df1.2[,seq(from=2,to=96,by=3)],stringsAsFactors = F)
df1.rep3 <- as.data.frame(df1.2[,seq(from=3,to=96,by=3)],stringsAsFactors = F)
df1.rep1[,33] <- rownames(df1.2)
colnames(df1.rep1)[33] <- "Odor"
df1.rep2[,33] <- rownames(df1.2)
colnames(df1.rep2)[33] <- "Odor"
df1.rep3[,33] <- rownames(df1.2)
colnames(df1.rep3)[33] <- "Odor"
trainSet <- rbind(df1.rep1,df1.rep2,df1.rep3)

#Now build the models.
model_rf<-train(trainSet[,-33],trainSet[,33],method='rf')
model_lda<-train(trainSet[,-33],trainSet[,33],method='lda')

#Now look at second set
df2 <- read.table("Raw Data/AUC_AC discrimination_2.txt",sep="\t",header=TRUE)
df2.inv <- t(df2)
colnames(df2.inv) <- df2.inv[1,]
# df1.2 <- as.data.frame(df1.inv[-1,])
df2.2 <-df2.inv[-1,]
mode(df2.2) = "numeric"
# df1.2[,97]=rownames(df1.2)
#df1.2 <- cbind(df1.2,rownames(df1.2))
#colnames(df1.2)[97] <- "Odor"
#First two replicates are training set
df2.rep1 <- as.data.frame(df2.2[,seq(from=1,to=96,by=3)],stringsAsFactors = F)
df2.rep2 <- as.data.frame(df2.2[,seq(from=2,to=96,by=3)],stringsAsFactors = F)
df2.rep3 <- as.data.frame(df2.2[,seq(from=3,to=96,by=3)],stringsAsFactors = F)
df2.rep1[,33] <- rownames(df2.2)
colnames(df2.rep1)[33] <- "Odor"
df2.rep2[,33] <- rownames(df2.2)
colnames(df2.rep2)[33] <- "Odor"
df2.rep3[,33] <- rownames(df2.2)
colnames(df2.rep3)[33] <- "Odor"
df2.full <- rbind(df2.rep1,df2.rep2,df2.rep3)
Now we test the predictions
predictions<-predict.train(object=model_rf,df2.full[,-33],type="raw")
confusionMatrix(predictions,as.factor(df2.full[,33]))

## Confusion Matrix and Statistics
## 
##           Reference
## Prediction AC BA HAC MAC ME MS PP
##        AC   3  0   0   0  0  0  0
##        BA   0  3   1   0  0  0  0
##        HAC  0  0   2   0  0  0  0
##        MAC  0  0   0   3  0  0  0
##        ME   0  0   0   0  3  0  0
##        MS   0  0   0   0  0  3  0
##        PP   0  0   0   0  0  0  3
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9524          
##                  95% CI : (0.7618, 0.9988)
##     No Information Rate : 0.1429          
##     P-Value [Acc > NIR] : 2.274e-16       
##                                           
##                   Kappa : 0.9444          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: AC Class: BA Class: HAC Class: MAC Class: ME
## Sensitivity             1.0000    1.0000    0.66667     1.0000    1.0000
## Specificity             1.0000    0.9444    1.00000     1.0000    1.0000
## Pos Pred Value          1.0000    0.7500    1.00000     1.0000    1.0000
## Neg Pred Value          1.0000    1.0000    0.94737     1.0000    1.0000
## Prevalence              0.1429    0.1429    0.14286     0.1429    0.1429
## Detection Rate          0.1429    0.1429    0.09524     0.1429    0.1429
## Detection Prevalence    0.1429    0.1905    0.09524     0.1429    0.1429
## Balanced Accuracy       1.0000    0.9722    0.83333     1.0000    1.0000
##                      Class: MS Class: PP
## Sensitivity             1.0000    1.0000
## Specificity             1.0000    1.0000
## Pos Pred Value          1.0000    1.0000
## Neg Pred Value          1.0000    1.0000
## Prevalence              0.1429    0.1429
## Detection Rate          0.1429    0.1429
## Detection Prevalence    0.1429    0.1429
## Balanced Accuracy       1.0000    1.0000

#Now LDA
predictions<-predict.train(object=model_lda,df2.full[,-33],type="raw")
confusionMatrix(predictions,as.factor(df2.full[,33]))

## Confusion Matrix and Statistics
## 
##           Reference
## Prediction AC BA HAC MAC ME MS PP
##        AC   2  0   0   0  0  0  0
##        BA   0  3   0   0  0  0  0
##        HAC  0  0   3   0  0  0  0
##        MAC  1  0   0   3  0  0  0
##        ME   0  0   0   0  3  0  0
##        MS   0  0   0   0  0  3  0
##        PP   0  0   0   0  0  0  3
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9524          
##                  95% CI : (0.7618, 0.9988)
##     No Information Rate : 0.1429          
##     P-Value [Acc > NIR] : 2.274e-16       
##                                           
##                   Kappa : 0.9444          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: AC Class: BA Class: HAC Class: MAC Class: ME
## Sensitivity            0.66667    1.0000     1.0000     1.0000    1.0000
## Specificity            1.00000    1.0000     1.0000     0.9444    1.0000
## Pos Pred Value         1.00000    1.0000     1.0000     0.7500    1.0000
## Neg Pred Value         0.94737    1.0000     1.0000     1.0000    1.0000
## Prevalence             0.14286    0.1429     0.1429     0.1429    0.1429
## Detection Rate         0.09524    0.1429     0.1429     0.1429    0.1429
## Detection Prevalence   0.09524    0.1429     0.1429     0.1905    0.1429
## Balanced Accuracy      0.83333    1.0000     1.0000     0.9722    1.0000
##                      Class: MS Class: PP
## Sensitivity             1.0000    1.0000
## Specificity             1.0000    1.0000
## Pos Pred Value          1.0000    1.0000
## Neg Pred Value          1.0000    1.0000
## Prevalence              0.1429    0.1429
## Detection Rate          0.1429    0.1429
## Detection Prevalence    0.1429    0.1429
## Balanced Accuracy       1.0000    1.0000

# use tSNE to visualize the clusters
allData <- rbind(df1.rep1,df1.rep2,df1.rep3,df2.rep1,df2.rep2,df2.rep3)
tsne10long = Rtsne(allData[,-33], check_duplicates=FALSE, pca=TRUE, perplexity=10, max_iter=5000,dims=2, theta=0)
#plot(as.data.frame(tsne10long$Y),col=as.factor(allData[,33]))
forTSNE <- cbind(as.data.frame(tsne10long$Y),allData[,33])
names(forTSNE)[3] <- "Odor"
ggplot(data=forTSNE,aes(x=V1, y=V2, color=Odor))+geom_point()