%%% R code to generate the input data files from corresponding simulation logs
% R> setwd("~/Work/tensorPredictors")
% R>
% R> for (sim.name in c("1a", "1b", "1c", "1d", "1e")) {
% R>     pattern <- paste0("sim\\_", sim.name, "\\_normal\\-[0-9T]*\\.csv")
% R>     log.file <- sort(
% R>         list.files(path = "sim/", pattern = pattern, full.names = TRUE),
% R>         decreasing = TRUE
% R>     )[[1]]
% R>
% R>     sim <- read.csv(log.file)
% R>
% R>     aggr <- aggregate(sim[, names(sim) != "sample.size"], list(sample.size = sim$sample.size), mean)
% R>
% R>     write.table(aggr, file = paste0("LaTeX/plots/aggr-", sim.name, "-normal.csv"), row.names = FALSE, quote = FALSE)
% R> }
\documentclass[border=0cm]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{bm}

\definecolor{gmlm}{RGB}{0,0,0}
\definecolor{mgcca}{RGB}{86,180,233}
\definecolor{tsir}{RGB}{0,158,115}
\definecolor{hopca}{RGB}{230,159,0}
\definecolor{pca}{RGB}{240,228,66}
\definecolor{lpca}{RGB}{0,114,178}
\definecolor{clpca}{RGB}{213,94,0}

\pgfplotsset{
    compat=newest,
    every axis/.style={
        xtick={100,200,300,500,750},
        ymin=-0.05, ymax=1.05,
        grid=both,
        grid style={gray, dotted}
    },
    every axis plot/.append style={
        mark = *,
        mark size = 1pt,
        line width=0.8pt
    }
}
\tikzset{
    legend entry/.style={
        mark = *,
        mark size = 1pt,
        mark indices = {2},
        line width=0.8pt
    }
}

\begin{document}
\begin{tikzpicture}[>=latex, scale = 0.58]

    \begin{axis}[
        name=sim-1a,
        xticklabel=\empty
    ]
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1a-normal.csv};
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-1a-normal.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-1a-normal.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-1a-normal.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-1a-normal.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-1a.north west) {
        a: linear dependence
    };

    \begin{axis}[
        name=sim-1b,
        anchor=north west, at={(sim-1a.right of north east)}, xshift=0.1cm,
        xticklabel=\empty,
        yticklabel=\empty
    ]
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-1b-normal.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-1b-normal.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-1b-normal.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-1b-normal.csv};
        \addplot[color = gmlm, line width=1pt] table[x = sample.size, y = dist.subspace.gmlm] {aggr-1b-normal.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-1b.north west) {
        b: cubic dependence
    };

    \begin{axis}[
        name=sim-1c,
        anchor=north west, at={(sim-1b.right of north east)}, xshift=0.1cm,
        ylabel near ticks, yticklabel pos=right
    ]
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-1c-normal.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-1c-normal.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-1c-normal.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-1c-normal.csv};
        \addplot[color = gmlm, line width=1pt] table[x = sample.size, y = dist.subspace.gmlm] {aggr-1c-normal.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-1c.north west) {
        c: rank $1$ $\boldsymbol{\beta}$'s
    };

    \begin{axis}[
        name=sim-1d,
        anchor=north west, at={(sim-1a.below south west)}, yshift=-1.3em
    ]
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-1d-normal.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-1d-normal.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-1d-normal.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-1d-normal.csv};
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1d-normal.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-1d.north west) {
        d: tri-diagonal $\boldsymbol{\Omega}$'s
    };

    \begin{axis}[
        name=sim-1e,
        anchor=north west, at={(sim-1d.right of north east)}, xshift=0.1cm,
        yticklabel=\empty
    ]
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-1e-normal.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-1e-normal.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-1e-normal.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-1e-normal.csv};
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1e-normal.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-1e.north west) {
        e: misspecified
    };


    \matrix[anchor = center] at (sim-1e.right of east -| sim-1c.south) {
        \draw[color=gmlm,  legend entry, line width=1pt] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {GMLM};  \\
        \draw[color=tsir,  legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {TSIR};  \\
        \draw[color=mgcca, legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {MGCCA}; \\
        \draw[color=hopca, legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {HOPCA}; \\
        \draw[color=pca,   legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {PCA};   \\
    };


    \node[anchor = north] at (current bounding box.south) {Sample Size $n$};

    \node[anchor = south, rotate = 90] at (current bounding box.west) {Subspace Distance $d(\boldsymbol{B}, \hat{\boldsymbol{B}})$};
    \node[anchor = south, rotate = 270] at (current bounding box.east) {\phantom{Subspace Distance $d(\boldsymbol{B}, \hat{\boldsymbol{B}})$}};

    \node[anchor = south, font=\large] at (current bounding box.north) {Tensor Normal Simulation};

\end{tikzpicture}
\end{document}
