\documentclass[english]{article}
\usepackage[]{graphicx}
\usepackage[]{color}
% The line below tells R to use knitr on this.
%\VignetteEngine{knitr::knitr_notangle}
 %\VignetteIndexEntry{Shiny Interface to RobStatTM}
%\usepackage[utf8]{inputenc}

%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
%%\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
%%\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
%%\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
%%\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
%%\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
%%\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
%%\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
%%\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
%%\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
%%\newenvironment{kframe}{%
%% \def\at@end@of@kframe{}%
%%  \ifinner\ifhmode%
%%   \def\at@end@of@kframe{\end{minipage}}%
%%   \begin{minipage}{\columnwidth}%
%%  \fi\fi%
%%  \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
%%  \colorbox{shadecolor}{##1}\hskip-\fboxsep
%%      % There is no \\@totalrightmargin, so:
%%      \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
%%  \MakeFramed {\advance\hsize-\width
%%    \@totalleftmargin\z@ \linewidth\hsize
%%    \@setminipage}}%
%%  {\par\unskip\endMakeFramed%
%%  \at@end@of@kframe}
%% \makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
%\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[margin=0.5in]{geometry}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\setlength{\parskip}{\smallskipamount}
\setlength{\parindent}{0pt}
\usepackage{babel}
\usepackage{float}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{courier}
\onehalfspacing
\usepackage[unicode=true,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\usepackage[all]{hypcap}
\hypersetup{pdftitle={RobStatTM Shiny User Interface}}



\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
%%%%%%%%%%%%%%%%%%%% book.tex %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% sample root file for the chapters of your "monograph"
%
% Use this file as a template for your own input.
%
%%%%%%%%%%%%%%%% Springer-Verlag %%%%%%%%%%%%%%%%%%%%%%%%%%


% RECOMMENDED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% choose options for [] as required from the list
% in the Reference Guide


\usepackage[bottom]{footmisc}% places footnotes at page bottom

% see the list of further useful packages
% in the Reference Guide


%\usepackage[style=authoryear,natbib=true,firstinits=true,backend=biber]{biblatex}
%\addbibresource{C:/Rprojects/bookportopt/msybook.bib}

%\renewcommand*{\nameyeardelim}{\addspace}
%\renewbibmacro{in:}{}

\usepackage{txfonts}
\usepackage{upgreek}

\makeatother
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
%\SweaveOpts{concordance=TRUE}

<<include=FALSE, echo=FALSE>>=
  library(knitr)
  opts_chunk$set(concordance=TRUE)
@

\title{Shiny Interface to the RobStatTM Package}
\author{Gregory Brownson}
\maketitle

\section{Introduction}

This document describes a user interface (Shiny UI) to the
\textbf{RobStatTM} R package companion to the \textit{Robust
Statistics: Theory and Methods} book (Maronna et al. 2019 second edition). 
The Shiny UI supports use of functionality in \textbf{RobStatTM}
without needing to set many R function arguments or understanding
the complexities of the underlying functions. The simplicity of the Shiny
UI will allow students and practitioners who are new to robust statistical methods
to easily use them and compare robust results with classical methods.
With the Shiny UI, you will be able to load a data set either
from an existing R package or a local .csv file and then run the data
through different methods of analysis. Currently, the Shiny UI provides
an interface to robust location and scale, robust linear regression,
robust covariance estimation, and robust principal component analysis.

\newpage{}

\subsection*{Package Dependencies}

The Shiny UI depends on the following packages:
\begin{itemize}
\item \textbf{DT}
\item \textbf{fit.models}
\item \textbf{ggplot2}
\item \textbf{grid}
\item \textbf{gridExtra}
\item \textbf{gtools}
\item \textbf{shiny}
\item \textbf{shinyjs}
\item \textbf{xts}
\end{itemize}
With the above packages all installed, start the Shiny UI interface with the single line of code:

<<include=TRUE, echo=TRUE, eval=FALSE>>=
ShinyUI()
@

This opens the Shiny user interface, enabling you to import data, and use functions in RobStatTM to fit models and analyze results.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Add image of the Shiny UI initial screen here %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage{}

Figure \ref{fig:home} displays the initial form of the Shiny user interface window. The dialog in this window is where you would select or upload a data set, and then navigate to one of the model tabs for further analysis. The subsequent sections contain detailed instructions for the functionality of each component of the Shiny UI.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{img/Home.png}
\caption{Initial screen after starting the Shiny UI}
\label{fig:home}
\end{figure}

\section{Loading Data}

You may load the data into the Shiny UI from an existing R package, or upload a csv file, by selecting from an ``R Package'',
or ``Upload'' as shown in Figure \ref{f1}. Once you select
a package from the list under \textbf{Library Name}, the \textbf{Select
Dataset} input will be populated by the names of existing of datasets in that
package. After selecting a dataset, you should click on the \textbf{Load
Data} button to load the data into the application for further analysis,
and the data will be displayed in a table on the right. Select the data set \textbf{mineral} in the dropdown \textbf{Select Dataset} and click on \textbf{Load Data}. Then you will get the data display result shown in Figure \ref{f1}.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{img/DataLibrary.png}
\caption{Interface to load a data set from an existing R package}
\label{f1}
\end{figure}

Figure \ref{f2} shows the options for uploading a csv file for analysis with RobStatTM. Since some csv files do not have the default format
(comma-delimited values, double quote escape character), advanced
options are available to specify the separator or escape character.
If the data is a time series, then you
must check the corresponding box as shown in the example. The
first column of data must contain the date or time indices to be read
as a time series. For this tutorial, we use the data set \textbf{hfunds.ts.csv}
which contains returns for five hedge funds, and upload it as a time
series (Instructions to find this dataset are provided below). The window in the right-hand panel displays the data in
a table like format, where you may view the returns for each date.
Each column corresponds to one of the five different types of hedge funds:
emerging markets (\textbf{EM}), Private Equity (\textbf{PE}), U.S.
High Yield (\textbf{USHY}), Alternative Investments (\textbf{AI}),
and Bond (\textbf{BND}). To find the \textbf{hfunds} dataset, first you need to type
\texttt{find.package('RobStatTM')} in the command line to get the path to the \textbf{RobStatTM} package.
From that location, the file is located in \textit{etc} subdirectory. For data sets with a large number of variables, you can view the data in full screen mode.

\begin{figure}[th]
\centering
\includegraphics[scale=0.5]{img/Data_upload.png}
\caption{Interface to upload a csv file to the application}
\label{f2}
\end{figure}

\section{Robust Location and Scale Estimators}

Click on the \textbf{Location-Scale} tab to open the dialog which allows you
to compute classical estimates for mean
and standard deviation and/or robust estimates for location and scale
for a single variable, and compare results when you choose to compute both classical and robust estimates.
The Shiny UI interfaces with the \texttt{locScaleM()} function in
\textbf{RobStatTM} to compute robust estimates of location and scale.

\subsection*{Computing Location and Scale}

To use the Location-Scale dialog, you first select a \textbf{Variable} for whatever dataset you loaded, and then choose a \textbf{Method} from
the options of \emph{Classical}, \emph{Robust}, or \emph{Both}. The
classical and robust methods are both computed and then compared when \emph{Both} is chosen.
Furthermore, if either \emph{Robust} or \emph{Both} are selected,
then drop-down lists for $\rho$ and $\psi$ functions (rho and psi), and asymptotic efficiency choices appear, with defaults "mopt" (modified optimal) and 0.95, respectively. To compute the estimates
and display the summary, you will click on the \textbf{Results} button. Figure
\ref{f3} shows an example where you first load the data set \textbf{flour} using the method explained in Section 2, select the variable V1, and click on Results, thereby using the default values of rho and psi, and asymptotic efficiency.

\begin{figure}[H]

\centering
\includegraphics[scale=0.6]{img/Location-Scale.png}\caption{Comparison of Classical and robust estimates for location and scale
for the \textbf{flour} dataset}
\label{f3}
\end{figure}

Note that the results are displayed in the main panel on the right.
This is the layout for each model summary in the Shiny UI. For location
and scale, the summary displays estimates for location, the standard
error of the location estimate, and the estimate for scale.

\section{Robust Linear Regression}

Click on the \textbf{Robust Linear Regression} tab, located under the \textbf{Models}
tab in the menu as shown in Figure \ref{f4} (all subsequent models
are located under the \textbf{Models} tab). We now show how to do a computation and comparison of classical and robust linear regression model fits, using the \textbf{mineral} data set in the RobStatTM package that we showed you how to load in Section 2.

\begin{figure}[H]
\centering
\includegraphics[width=4in]{img/RobustLR_select.png}\caption{Navigation to \textbf{Robust Linear Regression} menu}
\label{f4}
\end{figure}

\subsection*{Fitting Linear Models}

There are four linear model fitting methods to choose from in the UI: least-squares (LS),
M, MM, and distance constrained maximum-likelihood (DCML). The Shiny UI provides an interface to the implementation of these methods through the \texttt{lm()} function in the \textbf{stats} package and the \texttt{lmrobM()}, \texttt{lmrobdetMM()}, and \texttt{lmrobdetDCML()} functions in \textbf{RobStatTM}. You have
the option to compare any two models by checking the \textbf{Add Second Method}
box and selecting inputs for a second model. In Figure \ref{f5},
the classical least-squares (LS) and robust MM regression methods are being
compared. You must select dependent and independent variables for
the model, and then the regression formula will automatically populate
the text box. When comparing regressions, the formulas do not need
to be identical. You fit the models and view the summary by pressing the
\textbf{Results} button at the bottom of the model panel, and then the results
will be displayed in the panel that appears at the right.

\begin{figure}[H]
\centering
\includegraphics[scale=0.5]{img/RobustLR_summary.png}
\caption{Least-squares and robust MM regression comparison using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f5}
\end{figure}

The standard errors, t-statistics, and p-values for the robust coefficients
for robust fits are computed using a robust covariance matrix for
the independent variables, as an important step to ensure that those
quantities are robust themselves (see the RobStatTM book Section 5.6 for details). Furthermore, the \emph{proportion
of variance explained by the model}, or R\textsuperscript{2}, for
robust fits is a robust version of classical least-squares R\textsuperscript{2}. 
To run this example, you have to make zinc is the dependent variable and copper is the independent variable
for the LS and MM estimators. The default "mopt" choice
for rho and psi, with the default asymptotic efficiency of 95\% is used for the MM estimate. The equivalent command-line code for obtaining the results in the right hand side of Figure \ref{f5} is provided below.

<<include=TRUE, echo=TRUE, eval=FALSE>>=
# Load mineral data set
data(mineral)
  
# Send fits to fit.models
fm <- fit.models(LS = lm(zinc ~ copper, data = mineral),
                 MM = lmrobdetMM(zinc ~ copper, data = mineral, 
                                 control = lmrobdet.control(family = "mopt",
                                                            eff    = 0.95)))

# Comparison of results
summary(fm)
@

\subsection*{Plots}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_PlotOpts.png}
\caption{Linear Regression plotting options}
\label{linRegPlotOpts}
\end{figure}

After fitting a linear regression model, or a pair of such models, you
may graphically analyze the results using the \textbf{Plots}
tab. In the dialog above, you may choose from several different plots. Note that the \emph{Scatter with Overlaid Fits} plot
is only available in the case of two univariate regressions. The following
figures show each of the plots selected in the dialog of Figure \ref{linRegPlotOpts}.

\subsubsection*{Residuals versus Fitted Values}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_ResVsFit.png}
\caption{Scatterplot of LS and robust MM residuals versus fitted values using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f6}
\end{figure}

\subsubsection*{Response versus Fitted Values}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_RespVsFit.png}
\caption{Scatterplot of response values versus LS and robust MM fitted values using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f7}
\end{figure}

\subsubsection*{Normal QQ Plots of Residuals}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_NormQQ.png}
\caption{LS and robust MM normal QQ plots of residuals using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f8}
\end{figure}

\subsubsection*{Standardized Residuals versus Robust Distances}

\begin{figure}[H]
\centering\includegraphics[scale=0.6]{img/LR_ResVsDist.png}
\caption{LS and robust MM Plots of standardized residuals versus square-root Mahalanobis
distances using \textbf{mineral} data from \textbf{RobStatTM}}
\label{f9}
\end{figure}

\subsubsection*{Probability Density Estimates of Residuals}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_ResDens.png}
\caption{Probability density estimates of LS and robust MM residuals using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f10}
\end{figure}

\subsubsection*{Standardized Residuals versus Index (Time)}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_ResVsIndex.png}
\caption{LS and robust MM standardized residuals using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f11}
\end{figure}

\subsubsection*{Scatterplots with Overlaid Fits}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/LR_OverScat.png}
\caption{Scatterplot of data with overlaid LS and robust MM fits using \textbf{mineral}
data from \textbf{RobStatTM}}
\label{f12}
\end{figure}

\section{Robust Covariance}

The Shiny UI allows you to use the \textbf{RobStatTM} functions \textbf{covClassic()} and \textbf{covRob()}.
Since these two methods only work numeric data, the options for \textbf{Variables} are limited to
those with numeric values only. Note that the options in the \textbf{Method} group allows you to choose either the classical
method alone, the robust method alone, or use the default of both methods,
which is highly recommended. If \emph{Robust}, or \emph{Both}
is chosen, then you use the Robust Covariance Estimator drop-down list to make
an estimator choice. You may select \emph{MM}, \emph{Rocke},
or \emph{Auto} as the estimator. If you use the default choice \emph{Auto}, then
a Rocke-estimator is used if the number of variables is greater than
or equal to 10, and an MM-estimator is used otherwise. For details concerning these estimators, see Sections 6.4.4 and 6.5, 
respectively in the book Robust Statistics: Theory and Methods, 2nd editio Figure \ref{f13} displays the comparison
of classical and robust covariance matrix and location estimates along with classical and robust
eigenvalues associated with those covariance matrix estimate for variables in the \textbf{wine}
dataset.

\begin{figure}[H]
\centering
\includegraphics[width=6in]{img/CovarianceSummary.png}
\caption{Classical and robust covariance estimates for selected variables in \textbf{wine}
data}
\label{f13}
\end{figure}

A command-line equivalent of the Shiny UI interface in Figure
\ref{f13} is shown below

<<include=TRUE, echo=TRUE, eval=FALSE>>=
# Load wine data
data(wine)

# Use fit.models to estimate centers and covariance matrices
cov.fm <- fit.models(Classic = covClassic(wine),
                     Robust  = covRob(wine, type = "auto"))

# Comparison of results
summary(cov.fm)
@

\subsection*{Plots}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/Covariance_PlotOpts.png}
\caption{Covariance plotting options}
\label{fig:covPlotOpts}
\end{figure}

The Shiny UI provides the plot types displayed in Figure \ref{fig:covPlotOpts} for classical and robust covariance matrix estimates.
For the wine data set, the plot types selected in Figure \ref{fig:covPlotOpts} are shown below.

\subsubsection*{Mahalanobis Distances}

\begin{figure}[H]
\centering
\includegraphics[width=6.5in]{img/CovarianceMd.png}\caption{Square-root Mahalanobis distances for classical and robust covariance estimates for the
\textbf{wine} data.}
\label{f14}
\end{figure}

\subsubsection*{Distances Chi-Squared QQ Plot}

\begin{figure}[H]
\centering
\includegraphics[width=6.5in]{img/CovarianceChi.png}
\caption{Classical and robust square-root Mahalanobis distances chi-squared QQ plots for the \textbf{wine} data.}
\label{f15}
\end{figure}

\subsubsection*{Distance-Distance scatterplot}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/CovarianceDistDist.png}
\caption{Scatterplot of square-root classical and robust Mahalanobis distances for the \textbf{wine} data.}
\label{f16}
\end{figure}

Figures \ref{f13}, \ref{f14}, \ref{f15}, and \ref{f16} above correspond to Example 6.2 and Figure 6.3 in Maronna et al. You may find it of interest run the above example with the choice "Covariances" changed to "Correlations". Figures \ref{f17} and \ref{f18} below use the variables V2, V4, V8, V11, and V12 in the \textbf{wine} dataset, which were chosen due to their low correlation with each other and to reduce the dimensionality of the dataset.

\subsubsection*{Eigenvalues}

\begin{figure}[H]
\centering
\includegraphics[scale = 0.6]{img/CovarianceEigen.png}
\caption{Scree plot for classical and robust covariance estimates for the \textbf{wine} data.}
\label{f17}
\end{figure}

\subsubsection*{Ellipses Matrix}

\begin{figure}[H]
\centering
\includegraphics[scale=0.6]{img/CovarianceEllipses.png}
\caption{Ellipses matrices with classical and robust correlation estimatesfor the \textbf{wine} data.. If you repeat the analysis with just three (or four) variables,
then you will get a 3 by 3 (or 4 by 4) display.}
\label{f18}
\end{figure}

\section{Robust PCA}

This section allows a simple computation and comparison of classical
and robust principal component analysis.

\subsection*{Computing Robust Principal Components}

The options for computing principal components are similar to the
options for computing covariance. Classical principal components are
computed using \textbf{prcomp()} and robust principal components are
computed using the \textbf{pcaRobS()} function in \textbf{RobStatTM}.
You must select at least three \textbf{Variables} for analysis.
If the option ``Both'' is selected for \textbf{Method}, then the
classical and robust principal components will be computed and compared.
Figure \ref{f19} contains an example comparing classical and robust
principal components of the \textbf{bus} data.

\begin{figure}[H]
  \centering
    \includegraphics[width=6.5in]{img/PcaSummary2.png}
  \caption{Classical and robust estimates for principal components for first ten variables in the \textbf{bus}
           data set}
  \label{f19}
\end{figure}

The command-line equivalent to the example in Figure \ref{f19}
is:

<<include=TRUE, echo=TRUE, eval=FALSE>>=
# Load bus data
data(bus)

# Use fit.models to estimate principal components
pca.fm <- fit.models(Classic = prcomp(bus),
                     Robust  = prcompRob(bus))

# Comparison of results
summary(pca.fm)
@


\subsection*{Plots}

Plots for principal component analysis are not available in the current
version, and we hope to add them in a future update.

\section{What's Next?}

\subsection*{Improvements to the User Interface}

We hope to evolve the Shiny UI as we receive feedback on
the interface and consider new ideas for it.

\end{document}
