[Tex/LaTex] Multi-row and Multi-column text alignment and grid

multicolumnmultirow

I am trying to create the table below using tabular in latex.

enter image description here

Exploring the stack exchange for similar problems like this one has brought me to my current table:

\usepackage{multirow}

\begin{document}
  \begin{table}[htbp]
    \caption{Confusion Matrix}
    \begin{center}
    \begin{tabular}{|c|c|c|c|}\hline\vline
      \multirow{1}{*}{N=986}
        & \multicolumn{3}{c}{Predicted} \\%
      \cline{3-4}
        \multirow{1}{*}{}
           & No & Yes\\%
      \hline
      \multirow{2}{*}{Actual}
        & \multirow{1}{*}{No} & TN = 27 & FP = 176\\%
        \cline{2-4}
        & \multirow{1}{*}{Yes} & FN = 9 &  TP = 174\\%
        \hline
    \end{tabular}
    \end{center}
  \label{tab:confusion-matrix}
\end{table}

\end{document}

So far, this code produces the given table:

enter image description here

Now, how what should I change in order to make sure that N=986 would occupy a cell merging the first 2 rows and columns, while Predicted occupies Row 1 and Columns 3 & 4, while No and Yes occupies the Row 2 and Columns 3 & 4 respectively? I have already tried to play around with the code but this one was the closest that I got to the intended output.

P.S.
I am also unsure why the vline is not there in the rightmost part of the first two cells. I am really having quite a trouble with table manipulation with LaTeX.

Best Answer

after removing unnecessary multirows and \vline, and adding missed ampersand:

enter image description here

\documentclass{article}
\usepackage{multirow}
\usepackage[skip=1ex]{caption}

\begin{document}
  \begin{table}[htbp]
\caption{Confusion Matrix}
\label{tab:confusion-matrix}
\centering
\renewcommand\arraystretch{1.2}
\begin{tabular}{|c|c|c|c|}\hline
\multirow{2}{*}{N=986}
    & \multicolumn{3}{c|}{Predicted}    \\
    \cline{2-4}
    &   & No & Yes                      \\
  \hline
\multirow{2}{*}{Actual}
    &   No  &   TN = 27 & FP = 176      \\
  \cline{2-4}
    &   Yes &   FN = 9 &  TP = 174      \\
    \hline
\end{tabular}
    \end{table}
\end{document}
Related Question