Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a table containing a flag and x and y values. I'd like to have a regression for all rows with 1 in the first column. How can I do this?

\documentclass{article}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{pgfplotstable}
\begin{document}
  \pgfplotstableread{
    Exmpl  a   v
    1      0   0
    1      1   1
    1      2   1
    1      3   4
    2      0   -0
    2      1   -1
    2      2   -1
    2      3   -4
  } \data
  \begin{tikzpicture}
    \begin{axis} % [legend pos=outer north east]
      \addplot table [x=a, y=v] {\data};
      \addplot table [x=a, y={create col/linear regression={y=v}}] {\data};
    \end{axis}
  \end{tikzpicture}
\end{document}

I think, the pgfplots filters don't apply here, since I guess, pgfplots only gets the x and y values. It would be great, if there is a possibility to create a filtered table and do the regression and plotting stuff from there.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I think I have almost the solution with the help of two questions: Create a contingency table using pgfplotstable and Accessing individual table elements with pgfplots? The idea is, to transpose the matrix, create a new matrix, loop over all columns and copy the columns, if the Exmpl - value is 1.

\documentclass{article}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{pgfplotstable}
\usepackage{ifthen}

\newcommand{\pgfplotstablefilterrows}[3]
{
  \pgfplotstablegetrowsof{#1}
  \pgfmathsetmacro{\NumOfRows}{\pgfplotsretval}
  \pgfmathsetmacro{\MaxRow}{\NumOfRows-1}
  \pgfplotstablegetcolsof{#1}
  \pgfmathsetmacro{\NumOfCols}{\pgfplotsretval}

  \pgfplotstabletranspose{\TransposedData}{#1}
  \pgfplotstableset{create on use/TransposedHead/.style={copy column from table={\TransposedData}{[index]0}}}
  \pgfplotstablenew[columns={TransposedHead}]{\NumOfCols}{\TransposedFilteredData}
  \pgfplotsforeachungrouped \pgfplotstablerowindex in {0,1,...,\MaxRow}{ % Row loop
    #3
  }
  \pgfplotstabletranspose[colnames from=TransposedHead,input colnames to=]{#2}{\TransposedFilteredData}
  \pgfplotstableclear{\TransposedData}
  \pgfplotstableclear{\TransposedFilteredData}
}

\begin{document}
  \pgfplotstableread{
    Exmpl  a   v
    1      0   0
    1      1   1
    1      2   1
    1      3   4
    2      0   -0
    2      1   -1
    2      2   -1
    2      3   -4
  } \data

  \pgfplotstablefilterrows{\data}{\FilteredData}
  {
    \pgfplotstablegetelem{\pgfplotstablerowindex}{[index]0}\of\data
    \ifthenelse{\equal{\pgfplotsretval}{1}}
    {
      \pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}
    }
    {}
  }

  \begin{tikzpicture}
    \begin{axis} % [legend pos=outer north east]
      \addplot table [x=a, y=v] {\data};
      \addplot table [x=a, y={create col/linear regression={y=v}}] {\FilteredData}; % compute a linear regression from the input table
    \end{axis}
  \end{tikzpicture}
\end{document} 

As you might see from the code, I'm not really experienced in latex programming. Anyway, it works. Best, Juhui

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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