---
title: "Network based discrimanent analysis"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Network based discrimanent analysis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(NetDA)
```

## Real data example
We demonstrate standard analysis of classification and prediction based on two functions in the NetDA package. 

In this study, we take the wine dataset as an example, which is available in https://archive.ics.uci.edu/ml/datasets/wine. These data were collected based on a chemical analysis of wines grown in the same region in Italy but derived from three different cultivars. In this dataset, there are three types of wines and 13 constituents, including Alcohol (Alcohol), Malic acid (Malic acid), Ash (Ash), Alcalinity of ash (Alcalinity), Magnesium (Magnesium), Total phenols (phenols), Flavanoids (Flavanoids), Nonflavanoid phenols (Nonflavanoid), Proanthocyanins (Proanthocyanins), Color intensity (Color), Hue (Hue), OD280/OD315 of diluted wines (OD280), and Proline (Proline).
```{r}
data(WineData)
Y = WineData[,1] # the response
X = WineData[,2:14] # the predictors
```

In the following analysis, the response is types of wines that are labeled as 1, 2, and 3; constituents are treated as predictors that are continuous. The goal is to adopt the information of constituents to construct predictive models, and then use them to classify type of wines for a given subject.
```{r}
D1 = WineData[which(Y==1),]
D2 = WineData[which(Y==2),]
D3 = WineData[which(Y==3),]
```

To demonstrate the functions and perform classification and prediction, we first split the full data into the training data and the validation data. In our example, we take the first 45 samples in each class to obtain the training data, and use the remaining samples in each class to form the validation data.
```{r}
Train = rbind(D1[1:45,], D2[1:45,],D3[1:45,]) # user-specific training data
Test = rbind(D1[45:dim(D1)[1],],D2[45:dim(D2)[1],],D3[45:dim(D3)[1],]) # user-specific testing data
```

The response (Y) and predictors (X) in the training data:
```{r}
X = Train[,2:14]
Y = Train[,1]
```

The response (Y_test) and predictors (X_test) in the validation data:
```{r}
X_test = Test[,2:14]
Y_test = Test[,1]
```

When the training data and the validation data are determined, we employ the function NetDA to perform classification. We insert X, Y, and X_test to the function NetDA, and we denote “NetLDA” and “NetQDA” as the argument method=1 and method=2, respectively. The resulting vectors of predicted classes and estimated precision matrices are given by “\$yhat” and “\$Network”, respectively.
```{r cars}
NetDA(X,Y,method=1,X_test) -> NetLDA
yhat_lda = NetLDA$yhat
Net_lda = NetLDA$Network

NetDA(X,Y,method=2,X_test) -> NetQDA
yhat_qda = NetQDA$yhat
Net_qda = NetQDA$Network

yhat_lda
round(Net_lda,3)

yhat_qda
round(Net_qda[[1]],3)

round(Net_qda[[2]],3)

round(Net_qda[[3]],3)
```



Finally, to assess the performance of prediction, we input predicted values (yhat_lda or yhat_qda) and responses in the validation data (Y_test) to the function Metrics, and the resulting values are displayed below.
```{r}
Metrics(yhat_lda,Y_test)
Metrics(yhat_qda,Y_test)
```

.
