# Visualisation: cars from three continents

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.

    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.

    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?

    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.

    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.

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

plot of chunk unnamed-chunk-6

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.

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

plot of chunk unnamed-chunk-7

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.

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

plot of chunk unnamed-chunk-8

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

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

plot of chunk unnamed-chunk-9

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

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

plot of chunk unnamed-chunk-10

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.

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

plot of chunk unnamed-chunk-11

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.

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

plot of chunk unnamed-chunk-12

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.

    # 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)

plot of chunk unnamed-chunk-13

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.