---
title: "Visualisation: cars from three continents"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Visualisation: cars from three continents}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---





The same dataset drawn eight ways, and what each view is able to show that the others are
not.

The other vignettes are listed by `vignette (package = "fdm2id")`; they use the same
handful of functions on other data, and can be read in any order.


``` r
library (fdm2id)
```

# The data

392 car models built on three continents (America, Asia and Europe). Beyond its origin, each
car is described by six numeric attributes: fuel consumption, number of cylinders,
displacement, horsepower, weight and acceleration.


``` r
data (autompg)
autompg = autompg [, -7]
summary (autompg)
#>       mpg          cylinders      displacement     horsepower        weight    
#>  Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613  
#>  1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225  
#>  Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804  
#>  Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978  
#>  3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615  
#>  Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140  
#>   acceleration       origin   
#>  Min.   : 8.00   America:245  
#>  1st Qu.:13.78   Europe : 68  
#>  Median :15.50   Asia   : 79  
#>  Mean   :15.54                
#>  3rd Qu.:17.02                
#>  Max.   :24.80
```

# Question 1. Should the data be centred and scaled?


``` r
apply (autompg [, -7], 2, sd)
#>          mpg    cylinders displacement   horsepower       weight acceleration 
#>     7.805007     1.705783   104.644004    38.491160   849.402560     2.758864
```

**Answer.** *The scales are very different -- `weight` against `acceleration` -- so yes.*


``` r
autompg [, -7] = scale (autompg [, -7])
```

# Question 2. What does each view of the data show?

The same dataset, drawn eight ways. `plotdata` is the single entry point; `type` picks the
view, and the origin of the cars colours the points throughout.


``` r
plotdata (autompg, type = "pairs", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-6-1.png" alt="plot of chunk unnamed-chunk-6"  />
<p class="caption">plot of chunk unnamed-chunk-6</p>
</div>

**Answer.** *Hard to see much in panels that small. It does look as though Europe and Asia
overlap, and that America is separated from the other two.*


``` r
plotdata (autompg, type = "boxplot", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-7-1.png" alt="plot of chunk unnamed-chunk-7"  />
<p class="caption">plot of chunk unnamed-chunk-7</p>
</div>

**Answer.** *For most variables American cars cover a wider range of values than European and
Asian ones. Except for the first and the last variable, their values are also higher: `mpg` is
lower for American cars, and `acceleration` slightly lower.*


``` r
plotdata (autompg, type = "parallel", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-8-1.png" alt="plot of chunk unnamed-chunk-8"  />
<p class="caption">plot of chunk unnamed-chunk-8</p>
</div>

**Answer.** *The same observations, read along the lines instead of across the boxes.*


``` r
plotdata (autompg, type = "histogram", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-9-1.png" alt="plot of chunk unnamed-chunk-9"  />
<p class="caption">plot of chunk unnamed-chunk-9</p>
</div>

**Answer.** *For every variable but the last (`acceleration`), small values are more frequent
than large ones.*


``` r
plotdata (autompg, type = "pca", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-10-1.png" alt="plot of chunk unnamed-chunk-10"  />
<p class="caption">plot of chunk unnamed-chunk-10</p>
</div>

**Answer.** *The factorial plane shows three homogeneous groups. The leftmost one mixes the
three origins; so does the middle one, though it is mostly American cars; the rightmost one
contains American cars only.*


``` r
plotdata (autompg, type = "cda", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-11-1.png" alt="plot of chunk unnamed-chunk-11"  />
<p class="caption">plot of chunk unnamed-chunk-11</p>
</div>

**Answer.** *Two groups this time -- one mixing the three origins, one of American cars only.
Note the difference in what is being asked: PCA looks for the axes of greatest variance and
ignores the origin of the cars, while discriminant analysis looks for the axes that separate
the three origins best. Being told the answer does not make the picture show more structure;
here it shows less.*


``` r
plotdata (autompg, type = "svd", labels = FALSE)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-12-1.png" alt="plot of chunk unnamed-chunk-12"  />
<p class="caption">plot of chunk unnamed-chunk-12</p>
</div>

**Answer.** *The same information as the factorial plane, which is no accident: on centred and
scaled data, an SVD and a PCA are the same decomposition.*


``` r
# Variable, and visibly so: t-SNE starts from a random embedding and optimises it. Two runs
# without 'seed' give two different pictures -- same groups, different positions and shapes.
plotdata (autompg, type = "tsne", labels = FALSE, perplexity = 50, seed = 0)
```

<div class="figure" style="text-align: center">
<img src="fig/v05-visualisation-cars-unnamed-chunk-13-1.png" alt="plot of chunk unnamed-chunk-13"  />
<p class="caption">plot of chunk unnamed-chunk-13</p>
</div>

**Answer.** *The same three groups as the factorial plane, but pulled apart: American cars
only in two of them, the third holding the European and Asian cars together with the American
ones that resemble them. This is what a non-linear embedding buys, at the price of distances
between the groups that can no longer be read quantitatively.*
