PCA is mathematically defined as an orthogonal linear transformation that transforms the data to a new coordinate system such that the greatest variance by some projection of the data comes to lie on the first coordinate (called the first principal component), the second greatest variance on the second coordinate, and so on. In other words, we convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. To sum up, the aim is to explain as much data variation as possible while discarding highly correlated variables. An interesting application of PCA is image compression. We are going to reconstruct an image by increasing amounts of principal components using the R shiny app.
Installing R Shiny
Installing Shiny is like installing any other package in R. Go to R Console and run the below command to install the Shiny package.
Once you have installed it, load the Shiny package to create Shiny apps.
Structure of a Shiny app
Shiny consists of 3 components:
1. User Interface Function
User Interface (UI) function defines the layout and appearance of the app. You can add CSS and HTML tags within the app to make the app more presentable. The function contains all inputs and outputs to be displayed in the app.
a. Shiny Layout Functions
Here we will be using a sidebar layout for our application
Your layout is ready, It’s time to add widgets into the app. Shiny provides various user input and output elements for user interaction. Let us discuss a few input and output functions.
b.Shiny Input Functions
Each input widget has a label, Id, other parameters such as choice, value, selected, min, max, etc.
c.Shiny Output functions
Shiny provides various output functions that display R outputs such as plots, images, tables, etc which display the corresponding R objects.
The server function defines the server-side logic of the Shiny app. It involves creating functions and outputs that use inputs to produce various kinds of output. Each output stores the return value from the render functions. We access input widgets using input$[widget-id]. These input variables are reactive values.
Let’s write a function to store the image file in a variable-
a.Splitimage function- will extract the individual color value matrices to perform PCA on each. The principal component analysis is performed on each color value matrix. As this example is focused on image compression and not description or interpretation of the variables, the data does not require centering (subtracting the variable means from the respective observation vectors), and the center argument is set to FALSE.
b.ProjectImage- The following loop reconstructs the original image using the projections of the data using increasing amounts of principal components. We will see that as the number of principal components increases, the more representative of the original image the reconstruction becomes. This sequential improvement in quality is because as more principal components are used, the more the variance (information) is described.
c.Output$img will display the original image on the main panel
d.Output$compressimg will display a compressed image on the main panel
e.Output$dl- will download the compressed image by taking the default filename as the current date.
shinyApp() function is the heart of the app which calls UI and server functions to create a Shiny App.
The below image shows the outline of the Shiny app.
Link of the application- https://shrutinair.shinyapps.io/ImageCompressionApp/
Github link- https://github.com/ShrutiNair5/RShiny
2) Wilmar Hernandez and Alfredo Mendez, Application of Principal Component Analysis to Image Compression (2018),https://www.intechopen.com/books/statistics-growing-data-sets-and-g...