# Create a new pgfplotstable that is a subset of another pgfplotstable

I'm using pgfplots to plot a data set, and I want to add a local regression line for a subset of the data. I'm able to calculate the regression line and plot if for the subset, but pgfplots is still using the full data set to calculate the regression line.

I'm also able to print a table containing only the data I want to plot the regression line for. I want to put this subset in a new table, and estimate the regression line on that table, but I can't figure out how. Alternatively, I would like to directly restrict pgfplots to only use the subset to estimate the regression.

Below is a MWE. I first print the subset of data that I want to estimate the regression line on (only two observations in this example). Then I plot the figure I want, but where the regression line is clearly estimated using the full data set.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{pgfplotstable}

\begin{document}

X   Y
9   8.5555553
27.5    9.6833334
45.5    9.015625
63.5    8.0161295
81.5    8.6268654
100 9.9777775
118.5   10.681818
136.5   9.5500002
154.5   10.106061
172.5   9.8783779
191 9.8529415
209.5   10.940298
227.5   10.075758
246 10.089552
263.5   10.526316
282 11.934066
300.5   11.708333
318.5   11.807693
336.5   11.791667
355 12.036586
}\datapoints

\pgfplotstabletypeset[
row predicate/.code={
\pgfplotstablegetelem{#1}{X}\of{\datapoints}
\pgfmathparse{\pgfplotsretval>210 && \pgfplotsretval<250}
\ifnum \pgfmathresult = 1
\else \pgfplotstableuserowfalse
\fi}
]{\datapoints}

\begin{figure}
\begin{tikzpicture}
\begin{axis}
\addplot[restrict x to domain=210:250] table[y={create col/linear regression={y=Y}}]{\datapoints};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

• I think the easiest is to create a new table instead of typesetting and using that macro name for the plotting. Then you can delete that macro by \pgfplotstableclear{\table} or whatever your favorite \relax method is. – percusse Oct 20 '14 at 5:04
• The typesetting is only there to show that I have the code to pick out the subset I'm after. I want to create a new table containing that data, but I can't figure out how - that's my exactly my question! If you mean that I could create the new table by hand, then I agree, but what I'm trying to do is create an animation illustrating local linear kernel regression, so I will want to draw many of these pictures where the sample used for the regression line is shifted. – standard_error Oct 20 '14 at 8:02

I've found an answer myself, although I'm not quite happy with it. It is possible to write out the restricted data set to disc, then read it in to a new table, and run the regression on the new table. I'm attaching a MWE below. I would prefer if this could be done without having to write to disc, but this solution at least works.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{pgfplotstable}

\begin{document}

X   Y
9   8.5555553
27.5    9.6833334
45.5    9.015625
63.5    8.0161295
81.5    8.6268654
100 9.9777775
118.5   10.681818
136.5   9.5500002
154.5   10.106061
172.5   9.8783779
191 9.8529415
209.5   10.940298
227.5   10.075758
246 10.089552
263.5   10.526316
282 11.934066
300.5   11.708333
318.5   11.807693
336.5   11.791667
355 12.036586
}\datapoints

\pgfplotstablesave[row predicate/.code={
\pgfplotstablegetelem{#1}{X}\of{\datapoints}
\pgfmathparse{\pgfplotsretval>210 && \pgfplotsretval<250}
\ifnum \pgfmathresult = 1
\else \pgfplotstableuserowfalse
\fi}]{\datapoints}{temp.dat}