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


We will use following packages for today's exercise. And we will study quality of wine and its predictors. Make sure you have all these packages installed via ``install.packages()``
```{r}
library(rpart)
library(rpart.plot)
library(caret)
library(randomForest)
wine <- read.csv(url("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"), header = TRUE, sep = ";")
```
We can check the structure of the dataset by:
```{r}
str(wine)
```

First, let's convert the quality variable into factor format. Tree and forest model will work better with this input format. 
```{r}
wine$quality <- as.factor(wine$quality)
str(wine$quality)
```


Before we split the data, let’s first look at a histogram of the frequency of wine quality ratings. You can do so either by histogram, or a density plot. 
```{r}
 hist(as.numeric(wine$quality))
plot(density(wine$fixed.acidity))
```
The majority of ratings are levels 3 and 4, which would be ratings 5 and 6 in the data frame.

For this project, we’ll first use the decision tree classification method for classifying wine into the 6 levels based on its properties. We’ll use the ``rpart()`` library to classify. The first step is to split the data into training and testing sets. To be safe, we’ll randomize these samples. Let’s use 80% of the data for training and 20% for testing.


```{r}
set.seed(02138)
s <- sample(1:nrow(wine), 0.8*nrow(wine),replace = FALSE)
wine_train <- wine[s,]
wine_test <- wine[-s,]
dim(wine_train)

```

 

## Linear Regresson
We can start by applying this train test split logic to a linear regression model. First, run a ``lm`` on training data. Then predict it on the test data.
```{r}
 m1 <- lm(quality ~., wine)
plot(x=wine$quality,predict(m1,wine))
mean( (predict(m1,wine) - wine$quality)^2 )
```


## Tree model

Now, let's try using ``rpart`` to run a tree model and visualize the result of it.
```{r}
tm <- rpart(quality ~., data = wine_train, method = "class")
prp(tm)
```

We could also plot a fancier one with `rpart.plot`. Each number in the plot indicates how well we predict each class of the outcome variable. As you can see, very unfortunately the tree model does not deal well with smaller class.
```{r}
 rpart.plot(tm,tweak = 1.6)

```
Now, let's predict the outcomes for testing dataset using the tree. And we can check how well the model is doing, since we know the true outcomes of the testing data.
```{r}
pred <- predict(tm, wine_test, type = "class")
 table(wine_test$quality, pred)
```
## Random forest model
As you have seen, the accuracy of tree model is a bit better than linear regression, but not super ideal. Let us try random forest with the same data and see if we can improve the accuracy.
```{r}
wine2 <- read.csv(url("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"), header = TRUE, sep = ";")
 
```

Let's first recode the quality variable so that we have fewer categories (learning from tree result).
```{r}
wine2$taste <- ifelse(wine2$quality < 5, "bad", "good")
wine2$taste <- ifelse(wine2$quality == 5, "normal", wine2$taste)
wine2$taste <- ifelse(wine2$quality == 6, "normal", wine2$taste)
wine2$taste <- as.factor(wine2$taste)
table(wine2$taste)

```


Show a barplot of the new variable
```{r}
 str(wine2$taste)
barplot(table(wine2$taste))
```

 

Let's try the same train and test split exercise.
```{r}
set.seed(02138)
s <- sample(1:nrow(wine2), 0.8*nrow(wine2),replace = FALSE)
wine_train2 <- wine2[s,]
wine_test2 <- wine2[-s,]
dim(wine_train2)
```

Now let's run a random forest model on the training set.
```{r}

 model <- randomForest(taste ~, data = wine_train2, ntree =500)
```


Let's predict the model on the testing set.
```{r}
 
```

 
## how many trees do we want for a forest? we could check model performance by plotting the error term. 
```{r}
 
``` 

Further more, we can inspect individual trees. 
```{r}
 
```

And here's variable importance (voted by majority of trees in the forest)
```{r}
 
 

```


