\documentclass[]{beamer}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{color}

\usepackage{graphicx}
\usepackage{pifont}
\usepackage{psfrag}
\usepackage{skull}
\usepackage{tikz}
\usepackage{bm}
\usepackage{listings}
\usepackage{xcolor}

\usepackage{helvet}

\usecolortheme{ncl}
\usefonttheme[onlymath]{serif}
\newcommand{\penaltymark}[1]{\colorbox{nclRed}{\color{white}(-#1)}}
\newcommand{\sol}{\setbeamercolor{structure}{fg=purple!80!black}}
\newcommand{\normal}{\setbeamercolor{structure}{fg=blue!60!green}}
\newcommand{\cover}{\setbeamercolor{structure}{fg=blue!30!black}}
\definecolor{dark-gray}{gray}{0.425}
\definecolor{light-gray}{gray}{0.125}

%Lablinks-specific stuff hre:
\definecolor{lablinks1}{RGB}{239, 243, 244}       	%Light grey
\definecolor{lablinks2}{RGB}{203,207,209}		%A somewhat darker grey
\definecolor{lablinkshi}{RGB}{232,61,37}
\newcommand{\shout}[1]{\textbf{\color{lablinkshi} #1}}   %For highlighting key words
\newcommand{\vsp}{\vspace{0.3cm}}		%Large vertical space
\newcommand{\vspl}{\vspace{0.15cm}}		%Smaller vertical space

%\setbeamertemplate{navigation symbols}{}

\title[]{        \\
A Look at Multivariate Techniques}  
\author{Dr. Lee Fawcett}
\date{\includegraphics[width=3cm, height=1.2cm]{lablinks_logo_light} \hspace{1cm} \includegraphics[width=3cm, height=1.01cm]{NU_logo_light}}

\begin{document}
%%From web for code/highlighting
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\definecolor{light-gray}{gray}{0.95}
\definecolor{light-blue}{rgb}{0.7, 0.9, 1}
\lstset{ 
  backgroundcolor=\color{light-blue},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
  basicstyle=\tiny,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
   escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=false,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,	                   % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=R,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{red},     % string literal style
  tabsize=2,	                   % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}
\setbeamercolor{background canvas}{bg=lablinks1}


\begin{frame}
\titlepage
\end{frame}


%% OK.  START THE MAIN CONTENT SLIDES.  
%%%% MAKE SURE TO USE THE FOLLOWING TEMPLATE BEFORE EACH AND EVERY SLIDE - TO PLACE THE LOGOS IN THE FOOTER OF EACH SLIDE
%% START SLIDE 1
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{}
\begin{center}
\huge
\textbf{Introduction}
\end{center}
\end{frame}
%% END SLIDE 1

%%%% MAKE SURE TO USE THE FOLLOWING TEMPLATE BEFORE EACH AND EVERY SLIDE - TO PLACE THE LOGOS IN THE FOOTER OF EACH SLIDE
%% START SLIDE 2
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{\color{lablinks1} Introduction}
\shout{Multivariate Data Analysis}, often abbreviated to MVA, provides the foundations of some of the buzz phrases being used in modern data applications:

\vsp

\begin{itemize}
\item Data Science 
\item Big Data Analytics 
\item Machine Learning 
\item Artificial Intelligence
\end{itemize}

\vspl

Over the course of the next 60 minutes we will have a whistle-stop tour of MVA, focusing primarily on the `workhorse' of modern MVA techniques: \shout{Principal Components Analysis}.  
\end{frame}
%% END SLIDE 2

%% START SLIDE 3
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{Background}
With today's data collection systems and databases, a wealth of information is available to analysts at most organisations: \pause
\begin{itemize} 
\item  \textit{Amazon}: World leader in collecting, storing, processing and analysing personal information from you and every other customer \pause
\item 2020 US election campaign \pause
\item 2019 UK General Election \pause
\item My own experience: impact work with road safety engineers
\end{itemize} \pause
\end{frame}

%% START SLIDE 3a
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{Background}
Multivariate Analysis -- often \shout{two steps}:
\begin{itemize}
\item Exploratory Data Analysis (building upon material in lecture 1)
\item Modelling (which we'll cover in later lectures)
\end{itemize}
\end{frame}



%% START SLIDE 4
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{Why `multivariate'?}

If you could solve all problems by taking a single measurement on a system, the world would be a much simpler place!

\vsp

However, complex systems often require multiple measurements, on many variables, to understand them.

\vsp

Example: \shout{Predicting the weather}.  Meteorologists require information on pressure, wind conditions, humidity, season, $\ldots$  
\end{frame}


%% START SLIDE 5
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{Why `multivariate'?}
\shout{Multivariate Data Analysis}, often abbreviated to MVA, is the investigation of many variables, simultaneously, in order to understand the relationships that might exist between them.  

\vsp

An MVA might be as simple as looking at the relationship between two variables through a scatterplot, right up to analysing \shout{millions of variables together}.  
\end{frame}

%% START SLIDE 6
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{Why `multivariate'?}
An MVA can be \shout{highly graphical}, although the usual output graphs might not be those you are used to seeing.  \pause

\vsp

For example, you might be interested in the association between height and weight for a cohort of students; this could easily be explored through a 2-D scatterplot.  \pause

\vsp

Throw a third variable into the mix -- perhaps shoe size -- and we're into the realms of 3-D scatterplots.
\pause

\vsp

Graphically displaying the results for more than three variables \shout{requires some ingenuity}, since we live in a 3-D world! 
\end{frame}


%% START SLIDE 7
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{Example: A look at the heptathlon dataset}

\vspace{-0.3cm}

{\tiny
\centering
\begin{tabular}{r  l l l l l l l}
Name		&100M hurdles	&High jump	&Shot put	&200M	&Long jump	&Javelin	&800M \\ \hline
               Jessica Ennis  &12.54 &1.86 &14.28 &22.83 &6.48 &47.49 &128.65 \\
           Lilli Schwarzkopf  &13.26 &1.83 &14.77 &24.77 &6.30 &51.73 &130.50\\
              Austra Skujyte  &14.00 &1.92 &17.31 &25.43 &6.25 &51.13 &140.59\\
  Antoinette Nana Djimou Ida  &12.96 &1.80 &14.26 &24.72 &6.13 &55.87 &133.62\\
             Jessica Zelinka  &12.65 &1.68 &14.81 &23.32 &5.91 &45.75 &129.15\\
         Kristina Savitskaya  &13.37 &1.83 &14.77 &24.46 &6.21 &43.70 &132.27\\
             Laura Ikauniece  &13.71 &1.83 &12.64 &24.16 &6.13 &51.27 &132.13\\
           Hanna Melnychenko  &13.32 &1.80 &12.96 &24.09 &6.40 &43.86 &132.90\\
             Brianne Theisen  &13.30 &1.83 &12.89 &24.35 &6.01 &46.47 &129.27\\
            Dafne Schippers   &13.48 &1.80 &13.67 &22.83 &6.28 &36.63 &135.52\\
           Nadine Broersen    &13.64 &1.86 &13.57 &25.13 &5.94 &51.98 &136.98\\
         Jessica Samuelsson   &13.58 &1.77 &14.18 &24.25 &6.18 &42.02 &131.31\\
  Katarina Johnson-Thompson   &13.48 &1.89 &11.32 &23.73 &6.19 &38.37 &130.76\\
                 Sharon Day   &13.57 &1.77 &14.28 &24.36 &5.85 &43.90 &131.31\\
             Yana Maksimava   &13.97 &1.89 &14.09 &25.43 &5.99 &42.33 &133.37\\
           Eliska Klucinova   &14.01 &1.80 &12.93 &25.00 &6.13 &45.65 &136.08\\
             Ellen Sprunger   &13.35 &1.71 &12.62 &23.59 &5.88 &45.63 &137.54\\
                Olga Kurban   &13.46 &1.80 &13.71 &23.88 &5.83 &40.36 &139.82\\
          Marisa De Aniceto   &13.74 &1.71 &13.09 &25.26 &5.76 &51.98 &136.20\\
             Gyorgyi Farkas  &14.33 &1.80 &13.55 &25.72 &6.07 &46.52 &137.83\\
               Grit Sadeiko  &13.50 &1.74 &12.43 &24.25 &6.11 &44.12 &143.01\\
             Sofia Ifadidou  &13.82 &1.68 &12.96 &25.91 &5.81 &56.96 &142.03\\
                Ivona Dadic  &14.58 &1.80 &12.19 &24.29 &6.00 &41.82 &135.90\\
               Sarah Cowley &13.95 &1.80 &12.37 &25.60 &6.00 &41.90 &139.01\\
               Louise Hazel  &13.48 &1.59 &12.81 &24.48 &5.77 &47.38 &138.78\\
              Ida Marcussen &14.08 &1.68 &14.26 &25.15 &5.82 &42.26 &133.62\\
           Chantae McMillan  &13.49 &1.68 &14.92 &25.25 &5.37 &49.78 &160.55\\
              Julia Machtig  &14.54 &1.68 &14.99 &25.38 &4.06 &44.40 &140.34\\
              Irina Karpova  &14.21 &1.68 &11.68 &25.42 &5.70 &35.75 &148.93\\ \hline
\end{tabular}
}
}

%% START SLIDE 8
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
{\tiny
\begin{lstlisting}
> head(heptathlon)
                                 H100M   HJ    SP R200M   LJ    JT  R800M
Jessica Ennis (GBR)              12.54 1.86 14.28 22.83 6.48 47.49 128.65
Lilli Schwarzkopf (GER)          13.26 1.83 14.77 24.77 6.30 51.73 130.50
Austra Skujyte (LTU)             14.00 1.92 17.31 25.43 6.25 51.13 140.59
Antoinette Nana Djimou Ida (FRA) 12.96 1.80 14.26 24.72 6.13 55.87 133.62
Jessica Zelinka (CAN)            12.65 1.68 14.81 23.32 5.91 45.75 129.15
Kristina Savitskaya (RUS)        13.37 1.83 14.77 24.46 6.21 43.70 132.27

> heptathlon[,1]
 [1] 12.54 13.26 14.00 12.96 12.65 13.37 13.71 13.32 13.30 13.48 13.64 13.58
[13] 13.48 13.57 13.97 14.01 13.35 13.46 13.74 14.33 13.50 13.82 14.58 13.95
[25] 13.48 14.08 13.49 14.54 14.21

> heptathlon$H100M
 [1] 12.54 13.26 14.00 12.96 12.65 13.37 13.71 13.32 13.30 13.48 13.64 13.58
[13] 13.48 13.57 13.97 14.01 13.35 13.46 13.74 14.33 13.50 13.82 14.58 13.95
[25] 13.48 14.08 13.49 14.54 14.21

> heptathlon[1,]
                    H100M   HJ    SP R200M   LJ    JT  R800M
Jessica Ennis (GBR) 12.54 1.86 14.28 22.83 6.48 47.49 128.65

> apply(heptathlon, 2, mean)
     H100M         HJ         SP      R200M         LJ         JT      R800M 
 13.633448   1.776207  13.596897  24.587586   5.950345  45.758966 136.481724 
\end{lstlisting}
}
\end{frame}

%% START SLIDE 8
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
\begin{lstlisting}
> par(mfrow=c(2,4))
> apply(heptathlon, 2, boxplot)

> par(mfrow=c(1,1))
> plot(heptathlon$HJ, heptathlon$LJ, xlab="High jump height (m)", ylab="Long jump distance (m)", main="Scatterplot to show long jump distances versus high jump heights")
\end{lstlisting}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
\begin{center}
\includegraphics[width=8.2cm]{boxplothep.pdf} 
\end{center}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
\begin{center}
\includegraphics[width=8.2cm]{ljhjscatter.pdf} 
\end{center}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
\begin{lstlisting}
plot(heptathlon$HJ, heptathlon$LJ, xlab="High jump height (m)", ylab="Long jump distance (m)", main="Scatterplot to show long jump distances versus high jump heights", pch=19, col=rainbow(29))
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{Example: A look at the heptathlon dataset}
\begin{center}
\includegraphics[width=8.2cm]{ljhjscatterv2.pdf} 
\end{center}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{Example: A look at the heptathlon dataset}
\begin{center}
\includegraphics[width=8.2cm]{ljhjscatterv3.pdf} 
\end{center}
}


%% START SLIDE 9
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{Example: A look at the heptathlon dataset}
\begin{center}
\includegraphics[width=8.2cm]{ljhjscatterv4.pdf} 
\end{center}
}



\frame{
\frametitle{Introduction}
We will now break for a problem session covering this section.  

\vsp

Please pause the video here and go to \shout{Problem Session 1} of \shout{Lesson 2}. 

\vsp

See you in a bit!
}



%% START SLIDE 10
%\setbeamertemplate{footline}[text line]{%
%  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
%\frame{
%\frametitle{Example: A hidden outlier!}

%\begin{center}
%\includegraphics[width=5cm, height=5cm]{../fig/Fig2anew-crop.pdf} \hspace{0.4cm} \includegraphics[width=5cm, height=5cm]{../fig/Fig2b1new-crop.pdf} 
%end{center}
%}

%% START SLIDE 11
%\setbeamertemplate{footline}[text line]{%
%  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
%\frame{
%\frametitle{Example: A hidden outlier!}
%\begin{center}
%\includegraphics[width=6.5cm, height=6.5cm]{../fig/Fig2cnew-crop.pdf}
%\end{center}
%}


%% START SLIDE 12
%\setbeamertemplate{footline}[text line]{%
%  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
%\frame{
%\frametitle{How to proceed?}
%The first step of any multivariate analysis should be \shout{exploration}:\pause
%\begin{itemize}
%\item Are there any dependencies between our variables?  
%\item Are there any outliers, or groupings in our data, that cannot be seen by looking at each of the variables individually?  
%\end{itemize}\pause

%\vspl

%If there \textit{are} patterns in our data, we might then want to \shout{exploit} these patterns.  

%\vsp

%That will lead us on to what is often the second step of any multivariate analysis: \shout{modelling}.  
%}


%\end{document}

%% START SLIDE 13
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{}
\begin{center}

\huge
\textbf{Simple exploration}

\end{center}
\end{frame}


%% START SLIDE 14
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Simple methods for summarising multivariate datasets}
Before embarking on any rigorous analysis of a multivariate dataset (or \textit{any} dataset, for that matter!), \shout{plot it}!  \pause
\begin{itemize}
\item Univariate datasets: histograms, bar charts/pie charts, ... \pause
\item Bivariate datasets: histograms for the `margins', \shout{scatterplots} for the dependence
\item Multivariate datasets...?
\end{itemize}
\begin{lstlisting}
> pairs(heptathlon)
\end{lstlisting}
\end{frame}


%% START SLIDE 15
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Scatterplot matrices}

\vspace{-0.6cm}

\begin{center}
\includegraphics[width=8.25cm]{pairs.pdf}
\end{center}
\end{frame}


%% START SLIDE 16

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Covariance and Correlation matrices}

\begin{lstlisting}
> cov(heptathlon)
             H100M           HJ          SP        R200M          LJ
H100M  0.233480542 -0.003236453 -0.08428534  0.264269335 -0.09268695
HJ    -0.003236453  0.006324384  0.01539495 -0.008963054  0.01890850
SP    -0.084285345  0.015394951  1.51165074  0.110874384 -0.06231675
R200M  0.264269335 -0.008963054  0.11087438  0.693497537 -0.13611342
LJ    -0.092686946  0.018908498 -0.06231675 -0.136113424  0.18704631
JT    -0.564292734 -0.007568350  2.11291453  1.423186700  0.02329680
R800M  1.150536700 -0.225578941 -0.01903017  2.544207882 -1.28646490
               JT       R800M
H100M -0.56429273  1.15053670
HJ    -0.00756835 -0.22557894
SP     2.11291453 -0.01903017
R200M  1.42318670  2.54420788
LJ     0.02329680 -1.28646490
JT    27.65580246  1.06724828
R800M  1.06724828 43.98550764
\end{lstlisting}

Note that the \shout{total variation} is:
\begin{lstlisting}
> sum(diag(cov(heptathlon)))
[1] 74.27331
\end{lstlisting}

\end{frame}



\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Covariance and Correlation matrices}

\begin{lstlisting}
> cor(heptathlon)
            H100M          HJ           SP      R200M          LJ          JT
H100M  1.00000000 -0.08422381 -0.141873405  0.6567483 -0.44352557 -0.22206800
HJ    -0.08422381  1.00000000  0.157450284 -0.1353395  0.54976024 -0.01809668
SP    -0.14187340  0.15745028  1.000000000  0.1082887 -0.11719376  0.32678572
R200M  0.65674831 -0.13533946  0.108288672  1.0000000 -0.37792353  0.32497225
LJ    -0.44352557  0.54976024 -0.117193760 -0.3779235  1.00000000  0.01024303
JT    -0.22206800 -0.01809668  0.326785718  0.3249723  0.01024303  1.00000000
R800M  0.35902139 -0.42769545 -0.002333792  0.4606545 -0.44850632  0.03059973
             R800M
H100M  0.359021391
HJ    -0.427695445
SP    -0.002333792
R200M  0.460654511
LJ    -0.448506323
JT     0.030599725
R800M  1.000000000
\end{lstlisting}

\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Covariance and Correlation matrices}
And what if you want to check the \shout{significance} of these correlations?

\begin{lstlisting}
> install.packages("Hmisc")	#Install
> library(Hmisc)	          #Load
> rcorr(as.matrix(heptathlon))
      H100M    HJ    SP R200M    LJ    JT R800M
H100M  1.00 -0.08 -0.14  0.66 -0.44 -0.22  0.36
HJ    -0.08  1.00  0.16 -0.14  0.55 -0.02 -0.43
SP    -0.14  0.16  1.00  0.11 -0.12  0.33  0.00
R200M  0.66 -0.14  0.11  1.00 -0.38  0.32  0.46
LJ    -0.44  0.55 -0.12 -0.38  1.00  0.01 -0.45
JT    -0.22 -0.02  0.33  0.32  0.01  1.00  0.03
R800M  0.36 -0.43  0.00  0.46 -0.45  0.03  1.00

n= 29 

P
      H100M  HJ     SP     R200M  LJ     JT     R800M 
H100M        0.6640 0.4629 0.0001 0.0160 0.2469 0.0558
HJ    0.6640        0.4147 0.4839 0.0020 0.9258 0.0206
SP    0.4629 0.4147        0.5761 0.5449 0.0836 0.9904
R200M 0.0001 0.4839 0.5761        0.0432 0.0854 0.0119
LJ    0.0160 0.0020 0.5449 0.0432        0.9579 0.0147
JT    0.2469 0.9258 0.0836 0.0854 0.9579        0.8748
R800M 0.0558 0.0206 0.9904 0.0119 0.0147 0.8748   
\end{lstlisting}
\end{frame}

%% START SLIDE 17
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{3-D Scatterplots}
\begin{lstlisting}
install.packages("scatterplot3d")	#Install
library(scatterplot3d)	          #Load
scatterplot3d(heptathlon$H100M, heptathlon$R200M, heptathlon$LJ)
\end{lstlisting}
\end{frame}


%% START SLIDE 18
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{3-D Scatterplots}


\begin{center}
\includegraphics[width=8.2cm]{3d.pdf} 
\end{center}

}


%% START SLIDE 19
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Other methods}
There are a variety of other methods available for summarising multivariate datasets both graphically and numerically:
\begin{itemize}
\item \shout{Chernoff's Faces}: Each individual is represented by a face, the size/colour of its features being proportional to the measurements made on different variables \pause 
\item \shout{Star symbol plots}: Each individual is represented by a star, these stars having the same number of rays as we have variables.  The length of the rays are proportional to the measurements made on each variable \pause 
\item \shout{Mahalanobis distance}:  Put simply, this is the distance between two points in multivariate space.  It is a numerical summary often used to detect outliers in multivariate space
\end{itemize}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Other methods}
\begin{lstlisting}
> install.packages("aplpack")	#Install
> library("aplpack")	        #Load
> faces(heptathlon)
effect of variables:
 modified item       Var    
 "height of face   " "H100M"
 "width of face    " "HJ"   
 "structure of face" "SP"   
 "height of mouth  " "R200M"
 "width of mouth   " "LJ"   
 "smiling          " "JT"   
 "height of eyes   " "R800M"
 "width of eyes    " "H100M"
 "height of hair   " "HJ"   
 "width of hair   "  "SP"   
 "style of hair   "  "R200M"
 "height of nose  "  "LJ"   
 "width of nose   "  "JT"   
 "width of ear    "  "R800M"
 "height of ear   "  "H100M"

> install.packages(graphics)	#Install
> library(graphics)	          #Load
> stars(heptathlon)
\end{lstlisting}
\end{frame}


\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Other methods}

%\vspace{-0.6cm}

%\begin{center}
%\includegraphics[]{faces2.pdf} 
%\includegraphics[width=11cm, height=6.845cm]{faces.png} 
\includegraphics[width=11.8cm]{faces99.pdf} 
%\end{center}
\end{frame}


\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Other methods}
\begin{center}

\vspace{-0.42cm}

\includegraphics[width=8.2cm]{stars.pdf} 
\end{center}
\end{frame}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{Other methods}

\vspace{-0.2875cm}

\begin{lstlisting}
> mahalanobis(heptathlon, apply(heptathlon, 2, mean), cov(heptathlon))
             Jessica Ennis (GBR)          Lilli Schwarzkopf (GER) 
                        6.790333                         3.213907 
            Austra Skujyte (LTU) Antoinette Nana Djimou Ida (FRA) 
                       15.581773                         5.234548 
           Jessica Zelinka (CAN)        Kristina Savitskaya (RUS) 
                        9.033625                         2.874082 
           Laura Ikauniece (LAT)          Hanna Melnychenko (UKR) 
                        6.676844                         1.528953 
           Brianne Theisen (CAN)            Dafne Schippers (NED) 
                        3.652364                         8.590237 
           Nadine Broersen (NED)         Jessica Samuelsson (SWE) 
                        4.105886                         2.820154 
 Katarina Johnson-Thompson (GBR)                 Sharon Day (USA) 
                        9.014777                         1.692851 
            Yana Maksimava (BLR)           Eliska Klucinova (CZE) 
                        6.026769                         1.533981 
            Ellen Sprunger (SUI)                Olga Kurban (RUS) 
                        3.685668                         3.269338 
         Marisa De Aniceto (FRA)             Gyorgyi Farkas (HUN) 
                        3.180311                         3.792612 
              Grit Sadeiko (EST)             Sofia Ifadidou (GRE) 
                        3.046474                         8.056892 
               Ivona Dadic (AUT)               Sarah Cowley (NZL) 
                       11.559556                         5.705398 
              Louise Hazel (GBR)              Ida Marcussen (NOR) 
                        6.892786                         7.733176 
          Chantae McMillan (USA)              Julia Machtig (GER) 
                       17.140631                        23.099988 
             Irina Karpova (KAZ) 
                       10.466088 
\end{lstlisting}
\end{frame}


\frame{
\frametitle{Simple exploration}
We will now break for a problem session covering this section.  

\vsp

Please pause the video here and go to \shout{Problem Session 2} of \shout{Lesson 2}. 

\vsp

See you in a bit!
}







%% START SLIDE 21
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}
\frametitle{}
\begin{center}

\huge
\textbf{Principal Components Analysis}

\end{center}
\end{frame}




%% START SLIDE 22
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{Principal Components Analysis}
\begin{itemize}
\item  \shout{Principal Components Analysis} (PCA) is the analysis of variability in a multivariate dataset 

\item PCA is often referred to as the ``\shout{workhorse of multivariate methods}'' 
\item PCA provides some powerful graphical tools for exploratory analysis, and enables datasets with many variables to be displayed graphically in 2-D space \pause
\item Thus, PCA is also referred to as a \shout{dimension reduction} technique
\end{itemize} 
}


%% START SLIDE 23
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{Principal Components Analysis}
\begin{itemize}
\item The main aim is to find new variables which are linear combinations of the original variables \pause
\item The new variables are such that a few of them will explain most of the variability in the data, hence effectively \shout{reducing the number of variables} \pause
\item Unlike \shout{stepwise regression}, PCA includes information on \textit{all} variables through these new variables  
\end{itemize}
}


%% START SLIDE 24
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{PCA of the heptathlon dataset}
\noindent  Recall we have seven variables, which we will now label:
\begin{eqnarray*}
X_1	&=&	\text{Time in 100 metre hurdles} \\
X_2	&=&	\text{Height in high jump} \\
X_3	&=&	\text{Distance in shot put}\\
X_4	&=&	\text{Time in 200 metres}	\\
X_5	&=&	\text{Distance in long jump}	\\
X_6	&=&	\text{Distance thrown in javelin}\\
X_7	&=&	\text{Time in 800 metres}
\end{eqnarray*}
}

%% START SLIDE 25
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{PCA of the heptathlon dataset}
The aim of a PCA is to \shout{construct new variables}:
{\small
\begin{eqnarray*}
Y_1	&=&a_{1,1}X_{1}+a_{2,1}X_{2}+a_{3,1}X_{3}+a_{4,1}X_{4}+a_{5,1}X_{5}+a_{6,1}X_{6}+a_{7,1}X_{7}\\
Y_2	&=&a_{1,2}X_{1}+a_{2,2}X_{2}+a_{3,2}X_{3}+a_{4,2}X_{4}+a_{5,2}X_{5}+a_{6,2}X_{6}+a_{7,2}X_{7}\\
Y_3	&=&a_{1,3}X_{1}+a_{2,3}X_{2}+a_{3,3}X_{3}+a_{4,3}X_{4}+a_{5,3}X_{5}+a_{6,3}X_{6}+a_{7,3}X_{7}\\
Y_4	&=&a_{1,4}X_{1}+a_{2,4}X_{2}+a_{3,4}X_{3}+a_{4,4}X_{4}+a_{5,4}X_{5}+a_{6,4}X_{6}+a_{7,4}X_{7}\\
\vdots	&& \vdots
\end{eqnarray*}}
}

%% START SLIDE 26
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{PCA of the heptathlon dataset}
Why would we want to do all of this?
\begin{itemize}
\item $Y_1$ is known as the \shout{first principal component} in our analysis 
\item Notice it contains information on all of our original variables $X_1$ through to $X_7$  \pause
\item $Y_2$ is the \shout{second principal component}, and it too contains information on all of our original variables \pause
\item $Y_3$ is the \shout{third principal component}, and so on and so forth
\end{itemize} \pause

\vspl

Each of our principal components is a function of all of our original $X$ variables, and so each one will \shout{capture the overall variability} of our multivariate dataset.  
}

%% START SLIDE 27
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{PCA of the heptathlon dataset}
The mathematics underpinning the estimation of the `$a$'s is such that
\[
\text{Var}(Y_1) > \text{Var}(Y_2) > \text{Var}(Y_3) > \cdots,
\]
and $\sum \text{Var}(Y_{i}) = \text{total variation} \quad \text{(See last section)}$.  \pause

\vsp

It's often the case that the first two principal components, considered together, \shout{capture most of the variability} present in our original multivariate dataset.   


\vsp

Thus, our aim is to work with -- perhaps -- just $Y_1$ and $Y_2$, rather than $X_{1}, X_{2}, \ldots, X_{7}$. \pause

\vsp

Both $Y_1$ and $Y_2$ will likely capture most of the variability in our multivariate dataset \textit{and} then we've `\shout{reduced the dimension}' of our analysis from 7-D to just 2-D.  
}



%% START SLIDE 28
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\frame{
\frametitle{PCA of the heptathlon dataset}
\begin{itemize}
\item In the jumping events large numbers are indicative of good performances \pause
\item In the running events, small numbers are indicative of good performances \pause
\item  It is good practice, when performing a PCA, to \shout{re-scale variables} so that the `direction' is interpreted in the same way for all variables \pause
\item  Thus, from this point forwards, we now work with a new version of the heptathlon dataset in which the running events have been adjusted so that big numbers indicate good performances \pause
\item Specifically, for each athlete $j$ we now have
\begin{eqnarray*}
X_{1_{j}}^{*} &=& \max{(X_{1})}-X_{1_{j}},\\
X_{4_{j}}^{*} &=& \max{(X_{4})}-X_{4_{j}} \quad \text{and} \\
X_{7_{j}}^{*} &=& \max{(X_{7})}-X_{7_{j}}.
\end{eqnarray*}
\end{itemize}
}

\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
\begin{lstlisting}
> heptathlon2 = heptathlon
> heptathlon2$H100M = max(heptathlon2$H100M) - heptathlon2$H100M
> heptathlon2$R200M = max(heptathlon2$R200M) - heptathlon2$R200M
> heptathlon2$R800M = max(heptathlon2$R800M) - heptathlon2$R800M
> head(heptathlon)
                                 H100M   HJ    SP R200M   LJ    JT  R800M
Jessica Ennis (GBR)              12.54 1.86 14.28 22.83 6.48 47.49 128.65
Lilli Schwarzkopf (GER)          13.26 1.83 14.77 24.77 6.30 51.73 130.50
Austra Skujyte (LTU)             14.00 1.92 17.31 25.43 6.25 51.13 140.59
Antoinette Nana Djimou Ida (FRA) 12.96 1.80 14.26 24.72 6.13 55.87 133.62
Jessica Zelinka (CAN)            12.65 1.68 14.81 23.32 5.91 45.75 129.15
Kristina Savitskaya (RUS)        13.37 1.83 14.77 24.46 6.21 43.70 132.27

> head(heptathlon2)
                                 H100M   HJ    SP R200M   LJ    JT R800M
Jessica Ennis (GBR)               2.04 1.86 14.28  3.08 6.48 47.49 31.90
Lilli Schwarzkopf (GER)           1.32 1.83 14.77  1.14 6.30 51.73 30.05
Austra Skujyte (LTU)              0.58 1.92 17.31  0.48 6.25 51.13 19.96
Antoinette Nana Djimou Ida (FRA)  1.62 1.80 14.26  1.19 6.13 55.87 26.93
Jessica Zelinka (CAN)             1.93 1.68 14.81  2.59 5.91 45.75 31.40
Kristina Savitskaya (RUS)         1.21 1.83 14.77  1.45 6.21 43.70 28.28
\end{lstlisting}
\end{frame}

%% START SLIDE 29
\setbeamertemplate{footline}[text line]{%
  \parbox{\linewidth}{\vspace*{-25pt}  \includegraphics[width=2cm, height=0.8cm]{lablinks_logo_light}   \hfill  \includegraphics[width=2cm, height=0.67333337cm]{NU_logo_light}    }}
\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
\begin{lstlisting}
> (pca1 = prcomp(x=heptathlon2))
Standard deviations (1, .., p=7):
[1] 6.65449663 5.27527745 1.16042524 0.78461166 0.37146157 0.24271694 0.05774546
#%* \textcolor{mygreen}{Notice that $\sum \text{Var}(Y_i) = 6.654^2+\ldots+0.058^2 = 74.27331$, the total variation (as found earlier)}*) 
Rotation (n x k) = (7 x 7):
               PC1           PC2           PC3          PC4         PC5
H100M -0.025531133  0.0230747007  0.0283836192  0.479533759 -0.16226365
HJ    -0.005106786  0.0003381213  0.0109047147 -0.004792405 -0.09779270
SP     0.003090843  0.0800303918  0.9948011149 -0.001825383 -0.04390316
R200M -0.060571386 -0.0455319386  0.0003525911  0.856545544  0.28288157
LJ    -0.029240732  0.0038010184 -0.0544354626  0.175398972 -0.93881291
JT     0.068543052  0.9929624023 -0.0804990437  0.032406717  0.02273520
R800M -0.995032897  0.0707152225 -0.0016611050 -0.067348425  0.01646365
               PC6           PC7
H100M -0.859374071 -0.0566054449
HJ     0.081637617 -0.9917680240
SP     0.040955621  0.0186585712
R200M  0.424917692  0.0032450898
LJ     0.266850000  0.1132426862
JT     0.035780059 -0.0003526592
R800M -0.009484905  0.0030507417
\end{lstlisting} \pause

\vspace{-1.2cm}

{\scriptsize
\begin{eqnarray*}
Y_{1}	&=&-0.026X_{1}^{*}-0.005X_2+0.003X_3-0.061X_{4}^{*}-0.029X_5+0.069X_6 -0.995X_{7}^{*} \\
Y_{2}	&=&0.023X_{1}^{*}+0.0003X_2+0.080X_3-0.0461X_{4}^{*}+0.004X_5+0.993X_6 +0.071X_{7}^{*} \\
Y_{3}	&=&0.028X_{1}^{*}+0.011X_2+0.995X_3+0.00031X_{4}^{*}-0.054X_5-0.080X_6 -0.002X_{7}^{*} \\
\vdots	&\vdots	& \vdots	
\end{eqnarray*}
}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
\begin{lstlisting}
> (pca1 = prcomp(x=heptathlon2))
Standard deviations (1, .., p=7):
[1] 6.65449663 5.27527745 1.16042524 0.78461166 0.37146157 0.24271694 0.05774546
#%* \textcolor{mygreen}{Notice that $\sum \text{Var}(Y_i) = 6.654^2+\ldots+0.058^2 = 74.27331$, the total variation (as found earlier)}*) 
Rotation (n x k) = (7 x 7):
               PC1           PC2           PC3          PC4         PC5
H100M -0.025531133  0.0230747007  0.0283836192  0.479533759 -0.16226365
HJ    -0.005106786  0.0003381213  0.0109047147 -0.004792405 -0.09779270
SP     0.003090843  0.0800303918  0.9948011149 -0.001825383 -0.04390316
R200M -0.060571386 -0.0455319386  0.0003525911  0.856545544  0.28288157
LJ    -0.029240732  0.0038010184 -0.0544354626  0.175398972 -0.93881291
JT     0.068543052  0.9929624023 -0.0804990437  0.032406717  0.02273520
R800M -0.995032897  0.0707152225 -0.0016611050 -0.067348425  0.01646365
               PC6           PC7
H100M -0.859374071 -0.0566054449
HJ     0.081637617 -0.9917680240
SP     0.040955621  0.0186585712
R200M  0.424917692  0.0032450898
LJ     0.266850000  0.1132426862
JT     0.035780059 -0.0003526592
R800M -0.009484905  0.0030507417
\end{lstlisting} 

\vspace{-1.2cm}

{\scriptsize
\begin{eqnarray*}
Y_{1}	&=&{\color{lablinks2}-0.026X_{1}^{*}-0.005X_2+0.003X_3-0.061X_{4}^{*}-0.029X_5+0.069X_6 -}{\color{lablinkshi} 0.995X_{7}^{*}} \\
Y_{2}	&=&{\color{lablinks2}0.023X_{1}^{*}+0.0003X_2+0.080X_3-0.0461X_{4}^{*}+0.004X_5+{\color{lablinkshi} 0.993X_6} + 0.071X_{7}^{*}} \\
Y_{3}	&=&{\color{lablinks2}0.028X_{1}^{*}+0.011X_2+{\color{lablinkshi} 0.995X_3}+0.00031X_{4}^{*}-0.054X_5-0.080X_6 -0.002X_{7}^{*}} \\
\vdots	&\vdots	& \vdots	
\end{eqnarray*}
}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
PCA is \shout{not scale invariant}!\pause

\begin{itemize}
\item $Y_1$ is completely dominated by the 800m \pause
\item $Y_2$ is dominated by the javelin throw \pause
\item $Y_3$ is dominated by the shot put
\end{itemize} \pause

This is no surprise:

\begin{lstlisting}
> apply(heptathlon2, 2, var)
       H100M           HJ           SP        R200M           LJ           JT 
 0.233480542  0.006324384  1.511650739  0.693497537  0.187046305 27.655802463 
       R800M 
43.985507635 
\end{lstlisting} \pause

\vspace{-0.5cm}
And what if the 800m had been recorded in minutes (not seconds)?  Or the javelin throw in feet (not metres)?  \shout{Changing the scales would change the whole analysis!}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
PCA is \shout{not scale invariant}!\pause

\begin{itemize}
\item Not usually an issue if the original variables all have the same scale of measurement 

\item If several variables have considerably larger means and/or variances then they can \shout{dominate a PCA} based on the covariance matrix (as is the default method in R) \pause

\item Solution: First \shout{standardise} all variables (subtract means and divide by standard deviations) \pause

\item Note that this will \shout{adjust the total variation} to give
\[
\text{Total variation} = \sum_{i=1}^{p} 1 = p,
\]
where $p$ is the number of variables 
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
\begin{lstlisting}
> (pca2 = prcomp(x=heptathlon2, scale=TRUE))
Standard deviations (1, .., p=7):
[1] 1.6130032 1.2183228 1.0703309 0.8877202 0.7463326 0.5265001 0.3821523
#%* \textcolor{mygreen}{Check: $\sum \text{Var}(Y_i) = 1.613^2+\ldots+0.382^2 = 7$,}*) 
Rotation (n x k) = (7 x 7):
               PC1         PC2         PC3        PC4         PC5        PC6
H100M -0.444307758 -0.18648492  0.54748391 -0.1006979  0.18873506  0.1195608
HJ    -0.354825381 -0.16941702 -0.65972442  0.1312607  0.19560448  0.5826990
SP     0.001373487 -0.62701911  0.04473236  0.6872318  0.15204295 -0.3160055
R200M -0.469979456  0.27204215  0.39768748  0.2519808  0.07615658  0.2929824
LJ    -0.486122948 -0.02282651 -0.25423876 -0.3911557  0.39416618 -0.5937738
JT     0.043843375 -0.68464607  0.14172860 -0.5227754 -0.20973167  0.2519431
R800M -0.466443166 -0.01000331 -0.14193097  0.1027218 -0.83537221 -0.2107755
              PC7
H100M -0.63876411
HJ    -0.12301165
SP     0.09787771
R200M  0.62595867
LJ     0.19387886
JT     0.35846022
R800M -0.09675483
\end{lstlisting} 

\vspace{-1.2cm}

{\scriptsize
\begin{eqnarray*}
Y_{1}	&=&-0.444X_{1}^{*}-0.355X_2+0.001X_3-0.470X_{4}^{*}-0.486X_5+0.044X_6 -0.466X_{7}^{*} \\
Y_{2}	&=&-0.186X_{1}^{*}-0.169X_2-0.627X_3+0.2721X_{4}^{*}-0.023X_5-0.685X_6 -0.010X_{7}^{*} \\
Y_{3}	&=&0.547X_{1}^{*}-0.660X_2+0.045X_3+0.3981X_{4}^{*}-0.254X_5+0.142X_6 -0.1422X_{7}^{*} \\
\vdots	&\vdots	& \vdots	
\end{eqnarray*}
}
\end{frame}


\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset}
\begin{lstlisting}
> (pca2 = prcomp(x=heptathlon2, scale=TRUE))
Standard deviations (1, .., p=7):
[1] 1.6130032 1.2183228 1.0703309 0.8877202 0.7463326 0.5265001 0.3821523
#%* \textcolor{mygreen}{Check: $\sum \text{Var}(Y_i) = 1.613^2+\ldots+0.382^2 = 7$,}*) 
Rotation (n x k) = (7 x 7):
               PC1         PC2         PC3        PC4         PC5        PC6
H100M -0.444307758 -0.18648492  0.54748391 -0.1006979  0.18873506  0.1195608
HJ    -0.354825381 -0.16941702 -0.65972442  0.1312607  0.19560448  0.5826990
SP     0.001373487 -0.62701911  0.04473236  0.6872318  0.15204295 -0.3160055
R200M -0.469979456  0.27204215  0.39768748  0.2519808  0.07615658  0.2929824
LJ    -0.486122948 -0.02282651 -0.25423876 -0.3911557  0.39416618 -0.5937738
JT     0.043843375 -0.68464607  0.14172860 -0.5227754 -0.20973167  0.2519431
R800M -0.466443166 -0.01000331 -0.14193097  0.1027218 -0.83537221 -0.2107755
              PC7
H100M -0.63876411
HJ    -0.12301165
SP     0.09787771
R200M  0.62595867
LJ     0.19387886
JT     0.35846022
R800M -0.09675483
\end{lstlisting} 

\vspace{-1.2cm}
{\scriptsize
\begin{eqnarray*}
Y_{1}	&=&{\color{lablinks2}{\color{lablinkshi}-0.444X_{1}^{*}-0.355X_2}+0.001X_3 {\color{lablinkshi} - 0.470X_{4}^{*}-0.486X_5}+0.044X_6 {\color{lablinkshi}-0.466X_{7}^{*}}}\\
Y_{2}	&=&{\color{lablinks2}-0.186X_{1}^{*}-0.169X_2{\color{lablinkshi}-0.627X_3}+0.2721X_{4}^{*}-0.023X_5{\color{lablinkshi}-0.685X_6} -0.010X_{7}^{*}} \\
Y_{3}	&=&{\color{blue} 0.547X_{1}^{*}{\color{mygreen}-0.660X_2}+0.045X_3+0.3981X_{4}^{*}{\color{mygreen}-0.254X_5}+0.142X_6 {\color{mygreen}-0.1422X_{7}^{*}} }\\
\vdots	&\vdots	& \vdots	
\end{eqnarray*}
}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the heptathlon dataset: Summary so far}
\begin{itemize}
\item 7 variables across 29 athletes.  Do we need all 7 variables? \pause

\vspl

\item After \shout{re-scaling} and \shout{standardisation}, we have 7 \shout{Principal Components}:

\begin{itemize}
\item [--] \textbf{PC1:} Average measure of ability in running/jumping events

\item [--] \textbf{PC2:} Average measure of ability in throwing events

\item [--] \textbf{PC3:} Contrast between (i) jumping/middle-distance running events and (ii) throwing events and sprints
\end{itemize} \pause
 

\item Variability accounted for:

{\tiny
\begin{center}
\begin{tabular}{|c|c| c |c|} \hline
PC		&1					&2	&3	\\ \hline
$\% \text{Var}$ &$(1.613^2/7)\times 100 = 37.2\%$	&$(1.218^2/7)\times 100 = 21.2\%$ &$(1.070^2/7)\times 100 = 16.4\%$  \\ \hline
Sum		&$37.2\%$		&$58.4\%$	&$74.8\%$	\\ \hline
\end{tabular}
\end{center}
}	\pause

\item Could probably  just use first 3 PCs and still retain most of the information in the data!
\end{itemize}
\end{frame}


\frame{
\frametitle{Principal Components Analysis}
We will now break for a problem session covering this section.  

\vsp

Please pause the video here and go to \shout{Problem Session 3} of \shout{Lesson 2}. 

\vsp

See you in a bit!
}

\frame{
\frametitle{PC scores}
So we can reduce the dimension of our dataset whilst still retaining much of the variation within it.  So what?! \pause

\vspl

Plots of \shout{PC scores} against each other, for the first two PCs, can often be used to reveal groupings or outliers.  \pause

\vspl

For example, in the heptathlon dataset, we had:
{\scriptsize
\begin{eqnarray*}
Y_{1}	&=&-0.444X_{1}^{*}-0.355X_2+0.001X_3-0.470X_{4}^{*}-0.486X_5+0.044X_6 -0.466X_{7}^{*}	\\
Y_{2}	&=&-0.186X_{1}^{*}-0.169X_2-0.627X_3+0.2721X_{4}^{*}-0.023X_5-0.685X_6 -0.010X_{7}^{*}.
\end{eqnarray*} \pause
}
The observed scores for each athlete, $Y_{1}^{(\text{obs})}$ and $Y_{2}^{(\text{obs})}$, can be found by substituting in their actual values for $X_{1}^{*},  X_{2}, \ldots$.

\vspl 

Tedious to do by hand -- easy to do in R!
}

\begin{frame}[fragile]
\frametitle{PC scores}

\vspace{-0.29cm}

\begin{lstlisting}
> pca2$x[,1]
             Jessica Ennis (GBR)          Lilli Schwarzkopf (GER) 
                     -3.50217160                      -1.24307944 
            Austra Skujyte (LTU) Antoinette Nana Djimou Ida (FRA) 
                      0.17195226                      -0.96884007 
           Jessica Zelinka (CAN)        Kristina Savitskaya (RUS) 
                     -1.65943423                      -1.15818453 
           Laura Ikauniece (LAT)          Hanna Melnychenko (UKR) 
                     -0.87405110                      -1.44906334 
           Brianne Theisen (CAN)            Dafne Schippers (NED) 
                     -1.24982570                      -1.75337081 
           Nadine Broersen (NED)         Jessica Samuelsson (SWE) 
                      0.03678361                      -0.86435915 
 Katarina Johnson-Thompson (GBR)                 Sharon Day (USA) 
                     -1.86873533                      -0.42476488 
            Yana Maksimava (BLR)           Eliska Klucinova (CZE) 
                     -0.01428641                       0.24099396 
            Ellen Sprunger (SUI)                Olga Kurban (RUS) 
                     -0.37690278                      -0.33981366 
         Marisa De Aniceto (FRA)             Gyorgyi Farkas (HUN) 
                      1.01829329                       1.14004303 
              Grit Sadeiko (EST)             Sofia Ifadidou (GRE) 
                      0.11303330                       1.98774068 
               Ivona Dadic (AUT)               Sarah Cowley (NZL) 
                      0.46512623                       0.84474007 
              Louise Hazel (GBR)              Ida Marcussen (NOR) 
                      1.00597850                       1.07407876 
          Chantae McMillan (USA)              Julia Machtig (GER) 
                      3.05124041                       4.09640007 
             Irina Karpova (KAZ) 
                      2.50047888 
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{PC scores}

\vspace{-0.29cm}

\begin{lstlisting}
> pca2$x[,2]
             Jessica Ennis (GBR)          Lilli Schwarzkopf (GER) 
                      -0.6398539                       -1.7214147 
            Austra Skujyte (LTU) Antoinette Nana Djimou Ida (FRA) 
                      -3.0425385                       -2.0221641 
           Jessica Zelinka (CAN)        Kristina Savitskaya (RUS) 
                      -0.3869343                       -0.5248583 
           Laura Ikauniece (LAT)          Hanna Melnychenko (UKR) 
                      -0.1908915                        0.5337838 
           Brianne Theisen (CAN)            Dafne Schippers (NED) 
                       0.0882352                        1.5966040 
           Nadine Broersen (NED)         Jessica Samuelsson (SWE) 
                      -1.1480645                        0.2723521 
 Katarina Johnson-Thompson (GBR)                 Sharon Day (USA) 
                       2.0803685                       -0.0457769 
            Yana Maksimava (BLR)           Eliska Klucinova (CZE) 
                      -0.1995714                        0.3041175 
            Ellen Sprunger (SUI)                Olga Kurban (RUS) 
                       0.8778327                        0.7701118 
         Marisa De Aniceto (FRA)             Gyorgyi Farkas (HUN) 
                      -0.5792726                       -0.2312338 
              Grit Sadeiko (EST)             Sofia Ifadidou (GRE) 
                       0.9458016                       -1.2727123 
               Ivona Dadic (AUT)               Sarah Cowley (NZL) 
                       1.6386397                        0.8700351 
              Louise Hazel (GBR)              Ida Marcussen (NOR) 
                       0.5758550                        0.3134866 
          Chantae McMillan (USA)              Julia Machtig (GER) 
                      -1.1981219                       -0.1319795 
             Irina Karpova (KAZ) 
                       2.4681645 
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{PC scores}
\begin{lstlisting}
> plot(pca2$x[,1],pca2$x[,2], xlab="First PC", ylab="Second PC")
> text(pca2$x[,1],pca2$x[,2],labels = rownames(heptathlon2))
\end{lstlisting}

\vspace{-0.5cm}

\begin{center}
\includegraphics[width=10cm]{scoreshep.pdf} 
\end{center}

\end{frame}



\frame{
\frametitle{PCA of the microarray dataset}
To illustrate the power of PCA in the analysis of `big data', we now consider a much more detailed dataset!

\vsp

The \shout{microrarray dataset} consists of measurements on the expression levels of 6,830 genes from 64 samples taken from malignant tumours in different patients.  

\vsp

This really is a \shout{big dataset}! 


\begin{itemize}
\item Scatterplot matrix: 23 million plots
\item No simple way of visualising these data
\end{itemize}
}

\begin{frame}[fragile]
\frametitle{PCA of the microarray dataset}
\begin{lstlisting}
> dim(nci)
[1]   64 6830

> nci[1:10,1:10]
                g1        g2        g3        g4        g5           g6
CNS       0.300000  1.180000  0.550000  1.140000 -0.265000 -7.00000e-02
CNS.1     0.679961  1.289961  0.169961  0.379961  0.464961  5.79961e-01
CNS.2     0.940000 -0.040000 -0.170000 -0.040000 -0.605000  0.00000e+00
RENAL     0.280000 -0.310000  0.680000 -0.810000  0.625000 -1.39000e-17
BREAST    0.485000 -0.465000  0.395000  0.905000  0.200000 -5.00000e-03
CNS.3     0.310000 -0.030000 -0.100000 -0.460000 -0.205000 -5.40000e-01
CNS.4    -0.830000  0.000000  0.130000 -1.630000  0.075000 -3.60000e-01
BREAST.1 -0.190000 -0.870000 -0.450000  0.080000  0.005000  3.50000e-01
NSCLC     0.460000  0.000000  1.150000 -1.400000 -0.005000 -7.00000e-01
NSCLC.1   0.760000  1.490000  0.280000  0.100000 -0.525000  3.60000e-01
                g7        g8          g9         g10
CNS       0.350000 -0.315000 -0.45000000 -0.65498050
CNS.1     0.699961  0.724961 -0.04003899 -0.28501950
CNS.2     0.090000  0.645000  0.43000000  0.47501950
RENAL     0.170000  0.245000  0.02000000  0.09501949
BREAST    0.085000  0.110000  0.23500000  1.49001949
CNS.3    -0.640000 -0.585000 -0.77000000 -0.24498050
CNS.4     0.100000  0.155000 -0.29000000 -0.08498051
BREAST.1 -0.040000 -0.265000 -0.31000000 -0.24498050
NSCLC    -0.920000 -0.515000 -0.28000000 -0.11498050
NSCLC.1   0.600000  0.175000  0.58000000  1.14501900
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{PCA of the microarray dataset}
\begin{lstlisting}
> pca3 = prcomp(x = nci)
> plot(pca3$x[,1], pca3$x[,2], xlab="First PC", ylab="Second PC")
> text(pca3$x[,1], pca3$x[,2],labels=rownames(nci), cex=0.7,pos=3)
\end{lstlisting}

\vspace{-0.75cm}

\begin{center}
\includegraphics[width=10cm]{scorecancer.pdf} 
\end{center}

\end{frame}

\frame{
\frametitle{Conclusions}
\begin{enumerate}
\item \shout{Simple graphical/numerical procedures} for exploring multivariate datasets
\begin{itemize}
\item Scatterplot matrices and 3-D scatterplots
\item Correlation matrices, with $p$-values
\end{itemize} \pause

\item \shout{Other procedures} -- e.g. Chernoff's faces, star symbol plots, Mahalanobis metric \pause

\item \shout{Principal Components Analysis} \pause
\begin{itemize}
\item Dimension reduction, whilst still retaining most of the information in the data
\item Spotting groupings/outliers using score plots
\item Be careful -- might need to pre-process your data!
\end{itemize} \pause

\item \shout{Further considerations} -- use PCs, rather than original variables, in statistical models (see later lecture)
\end{enumerate}
}







\end{document}
