4

I recently stumbled over pgfplotstable and its ability to process table information and create new columns. To hone my skills, I tried to create a script that would calculate an exponential smoothing forecast for a timeseries for different smoothing factors.

The calculation and plotting seems to have worked just fine, but I could not manage to give each of the new columns in the table a new column name that would appear if I print out the table using \pgfplotstabletypeset.

The assignment in general seems to work (i.e. the column name is changed from its default, however, all columns end up having the same column name, when I print the table. Attached is my stripped down case:

    \documentclass{article}
    \usepackage{amsmath}
    \usepackage{pgfplots}
    \pgfplotsset{compat=newest,width=10cm,legend style={font=\footnotesize}}
    \usepackage{pgfplotstable}
    \begin{document}

    % read observed time-series
    \pgfplotstableread{
    x   y
    1   4.237485502
    2   5.593257216
    3   6.101509413
    4   5.328536597
    5   6.353556834
    6   5.84178478
    7   5.271878886
    8   6.952802431
    9   4.579257831
    10  7.742456136
    }\mytable

    % get first x-Value as forecast value for period 1
    \pgfplotstablegetelem{0}{y}\of{\mytable}
    \pgfmathsetmacro{\finit}{\pgfplotsretval}
    % iterate through different levels of \alpha
    \pgfplotsforeachungrouped \alph in {10,20,...,50}{
        \pgfplotstablecreatecol[
            expr accum={ % in the first row, do not calculate forecast as linear combination of actual demand and forecast of last period, but just use initial value
                \ifnum\pgfplotstablerow=0
                    \finit
                \else
                    (1-\alph/100)*\pgfmathaccuma+\alph/100*\prevrow{y}
                \fi
            }{\finit}
        ]{\alph}{\mytable}
        % set name for column and display style
        \pgfplotstableset{columns/\expandafter\alph/.style={column name={$ES_{\alph}$},dec sep align={c}}}
    }
    % print forecasted values
    \begin{table}
            \center
            \pgfplotstabletypeset[%
                    header=has colnames
            ]{\mytable}
        \caption{Results of the Exponential Smoothing Excercise (Table)}
    \end{table}

    % plot observed and forecasted timeseries
    \begin{figure}
        \center
        \begin{tikzpicture}
            \begin{axis}[
                    legend cell align=left,legend pos=north west,
                    xlabel=$t$,ylabel=$Y$
                ]
                \addplot[only marks] table[y=y,x=x]{\mytable};
                \addlegendentry{Original Data};
                \pgfplotsinvokeforeach {10,20,...,50}{
                    \addplot +[mark=none,thin] table[y=#1,x=x]{\mytable};
                    \addlegendentry{$\alpha=\pgfmathprintnumber{#1}\%$};
                }
            \end{axis}
        \end{tikzpicture}
        \caption{Results of the Exponential Smoothing Excercise (Plot)}
    \end{figure}

    \end{document}

While you're at it: Do you know how I can define the values for alpha (10,20,...,50 in my code) only once and reuse it? I am aware that this is a different question, but my first attempts did not work and these might be low hanging fruits for you ;-)

  • 1
    You need expanded style hence estyle \pgfplotstableset{columns/\alph/.estyle={column name={$ES_{\alph}$},dec sep align={c}}}. Also that \expandafter does not do anything. If you want to expand that one too, you need to start from \pgfplotstableset and the first brace. – percusse Oct 19 '14 at 18:55
  • Great, that worked! The \expandafter was a remainder of my attempts to fix the problem because I assumed it would come down to expansion, but I was not aware of the .estyle property. – Henning Oct 19 '14 at 20:09
3

To close the threat here: The solution suggested by percusse works perfectly!

Here is the essential part of the line of code that assigns the column name within \pgfplotsforeachungrouped:

\pgfplotstableset{columns/\alph/.estyle={column name={$ES_{\alph}$}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.