---
title: "Explore mtcars"
author: "Roland Krasser"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Explore mtcars}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## How to explore the mtcars dataset using the explore package.

The explore package simplifies Exploratory Data Analysis (EDA). Get faster insights with less code!

The mtcars dataset comes with the dplyr package. We use the packages explore and dplyr (for `mtcars`, `select()`, `mutate()` and the` %>%` operator).

```{r message=FALSE, warning=FALSE}
library(dplyr)
library(explore)
```

### Explore dataset

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=3}
mtcars %>% explore_tbl()
```

```{r message=FALSE, warning=FALSE}
mtcars %>% describe()
```

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

variable | description
---------|------------------------------------------
mpg      | Miles/(US) gallon
cyl      | Number of cylinders
disp     | Displacement (cu.in.)
hp       | Gross horsepower
drat     | Rear axle ratio
wt       | Weight (lb/1000)
qsec     | 1/4 mile time
vs       | V/S
am       | Transmission (0 = automatic, 1 = manual)
gear     | Number of forward gears
carb     | Number of carburetors

### Explore variables

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=total_fig_height(mtcars, size = 2.5)}
mtcars %>% 
  explore_all()
```

### Number of gears?

Is there a difference between cars with 3,4 and 5 gears?

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
mtcars %>% 
  explore(gear)
```

Most of the cars in the dataset have 3 or 4 gears. 15.6% have 5 gears.

Now check relation between some of the variables and gear:

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=4}
mtcars %>% 
  select(gear, mpg, hp, cyl, am) %>% 
  explore_all(target = gear)
```

We see that 100% of cars with am = 0 (automatic) have 3 gears. All cars with am = 1 (manual) have 5 gears.

### High miles per gallon?

Let's define an interesting target: 
Cars that have mpg (miles per gallon) > 25

We copy the data and create a new target variable 

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=3}
data <- mtcars %>% 
  mutate(highmpg = if_else(mpg > 25, 1, 0, 0)) %>% 
  select(-mpg)

data %>% explore(highmpg)
```

So, about 19% of all cars have mpg > 25.
What else is special about them?

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=4}
data %>% 
  select(highmpg, cyl, disp, hp) %>% 
  explore_all(target = highmpg)
```

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=4}
data %>% 
  select(highmpg, drat, wt, qsec, vs) %>% 
  explore_all(target = highmpg)
```

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=4}
data %>% 
  select(highmpg, am, gear, carb) %>% 
  explore_all(target = highmpg)
```

There are some strong differences between cars with / without "high mpg".

Now let's grow a decision tree:

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
data %>% 
  explain_tree(target = highmpg)
```

Growing a decision tree, shows that there seems to be a very strong correlation between wt (weight) and "high mpg". Cars with a low weight are much more likely to have "high mpg".

Let's take a closer look to wt:

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
data %>% explore(wt, target = highmpg)
```

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
data %>% explore(wt, target = highmpg, split = FALSE)
```

The plot shows the percentage of cars with high mpg. Cars with high mpg have a weight < 2.5. So wt (weight) is a good predictor for high mpg. 

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
mtcars %>% explore(wt, mpg)
```

There is a strong correlation between wt and mpg.

If you want to have high miles per gallon (mpg), buy a car with low weight (wt)!

### Horsepower?

Is there a relation between horsepower and other variables like number of cylinder?

Let's build a decision tree with horsepower as target:

```{r message=FALSE, warning=FALSE, fig.width=6, fig.height=4}
mtcars %>% 
  explain_tree(target = hp, minsplit=15)
```

All cars have an average hp of 147 (shown in the top node). Then the data is split by cyl. Cars with cyl<7 (56% of all cars) have an average hp of 98 and cars with `cp>=7` (44% of all cars) have an average hp of 209. 

The variables cyl and mpg can explain hp. The bottom nodes are showing an average hp of 80, 121 and 209.

In the tree rounded values are shown. If you want to know the exact values for each split, you can use the parameter ```out = "model"``` to get the model returned and take a look.

```{r fig.height=4, fig.width=6, message=FALSE, warning=FALSE, include=FALSE}
model <- mtcars %>% 
  explain_tree(target = hp, minsplit=15, out = "model")
```

```{r echo=TRUE, fig.height=4, fig.width=6, message=FALSE, warning=FALSE}
model
```

You see that the split for mpg is done at exactly 21.45

Now let's take a look at the direct correlation the variables used in the tree and hp:

```{r message=FALSE, warning=FALSE, fig.width=8, fig.height=4}
mtcars %>% 
  select(hp, cyl, mpg) %>% 
  explore_all(target = hp)
```

Cars with 8 cylinders have higher horsepower.

Cars with low miles per gallon (mpg) have higher horsepower!
