---
title: "A quick tour of ppgmmga"
author: 
- Alessio Serafini, Luca Scrucca
date: "`r format(Sys.time(), '%d %b %Y')`"
output: 
  rmarkdown::html_vignette:
    toc: true
    number_sections: false
    # highlight: "monochrome"
    css: "vignette.css"
vignette: >
  %\VignetteIndexEntry{A quick tour of ppgmmga}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(fig.align = "center", 
               out.width = "80%",
               fig.width = 6, fig.height = 5,
               dev.args = list(pointsize = 10),
               par = TRUE, # needed for setting hook 
               collapse = TRUE, # collapse input & ouput code in chunks
               message = FALSE,
               warning = FALSE)

knit_hooks$set(par = function(before, options, envir)
  { if(before && options$fig.show != "none") 
       par(family = "sans", mar=c(4.1,4.1,1.1,1.1), mgp=c(3,1,0), tcl=-0.5)
})
```

# Introduction 

An R package implementing a Projection Pursuit algorithm based on finite Gaussian Mixtures Models for density estimation using Genetic Algorithms (PPGMMGA) to maximise a Negentropy index. 
The **PPGMMGA** algorithm provides a method to visualise high-dimensional data in a lower-dimensional space, with special reference to reveal clustering structures.

```{r, message = FALSE, echo=1}
library(ppgmmga)
cat(ppgmmga:::ppgmmgaStartupMessage(), sep="")
```

# Banknote data

```{r, fig.width = 7, fig.height = 6, out.width = "100%"}
library(mclust)
data("banknote")
X <- banknote[,-1]
Class <- banknote$Status
table(Class)
clPairs(X, classification = Class, 
        symbols = ppgmmga.options("classPlotSymbols"),
        colors = ppgmmga.options("classPlotColors"))
```

# 1-dimensional PPGMMGA

```{r}
PP1D <- ppgmmga(data = X, d = 1, seed = 1)
PP1D
summary(PP1D)
```

```{r, out.width="60%", fig.width=6, fig.height=4}
plot(PP1D)
```

```{r, out.width="70%", fig.width=7, fig.height=4}
plot(PP1D, class = Class)
```


# 2-dimensional PPGMMGA

```{r}
PP2D <- ppgmmga(data = X, d = 2, seed = 1)
summary(PP2D)
summary(PP2D$GMM)
```

```{r}
plot(PP2D$GA)
```

```{r}
plot(PP2D)
```

```{r, fig.width = 7, fig.height = 5}
plot(PP2D, class = Class, drawAxis = FALSE)
```

# 3-dimensional PPGMMGA

```{r}
PP3D <- ppgmmga(data = X, d = 3, 
                center = TRUE, scale = FALSE, 
                gatype = "gaisl", 
                options = ppgmmga.options(numIslands = 2),
                seed = 1)
summary(PP3D)
```

```{r}
plot(PP3D$GA)
```

```{r, fig.width = 7, fig.height = 6, out.width = "100%"}
plot(PP3D)
```

```{r, fig.width = 7, fig.height = 6, out.width = "100%"}
plot(PP3D, class = Class)
```

```{r, fig.width = 6, fig.height = 5}
plot(PP3D, dim = c(1,2))
```

```{r, fig.width = 7, fig.height = 5, out.width = "90%"}
plot(PP3D, dim = c(1,3), class = Class)
```

```{r, eval=FALSE}
# A rotating 3D plot can be obtained using
if(!require("msir")) install.packages("msir")
msir::spinplot(PP3D$Z, markby = Class, 
               pch.points = c(20,17),
               col.points = ppgmmga.options("classPlotColors")[1:2])
```

<br>

# References

Scrucca L, Serafini A (2019). 
"Projection pursuit based on Gaussian mixtures and evolutionary algorithms." 
*Journal of Computational and Graphical Statistics*, 28(4), 847–860.
<https://doi.org/10.1080/10618600.2019.1598871>.


----

```{r}
sessionInfo()
```
