0

Based on the MWE of this question, I want to apply the regression only to a portion of the graph as in this other question, but not just the last portion. I want to do regression on the beginning so that the instruction skip first n=3 do not apply.

I can't make it work as I wish, i.e. regression only on the initial part of the graph. The command select coords between index={1}{3} generates an error.

\begin{tikzpicture} 
\begin{axis}[ytick={1,...,6}] 
\pgfplotstableread{% 
X Z Y 
1 3 0 
2 4 1 
3 5 2 
4 6 6 }\test 
\addplot[only marks, mark=*] 
     table[x=X, y=Y] {\test}; 
\addplot[   no markers,dashed, red, 
     select coords between index={1}{3}     
     ]      
     table [
     header=false,
     x=X,
     y={create col/linear regression={y=Y}}] {\test}; 
\end{axis}     
\end{tikzpicture}

When commenting the line with select coords between index the regression applies to all graph, and this is not what I want

regression applies to all graph, this is not what I want

5
  • Not answering the question, but regression on only a portion of a dataset is usually nonsensical from a statistical point of view. Commented Jan 13, 2016 at 21:03
  • I disagree, I can choose to apply a regression on a sub-part where I admit a certain rule/pattern/trend and another part with different behaviour, this is my choice as a modeller. Commented Jan 14, 2016 at 9:11
  • 1
    After some discussion with Christian (the author of PGFPlots and PGFPlotsTable) it seems that currently (v1.14) there is no way to ignore lines in the middle or the end while reading or processing a table. Only while typesetting lines can be "ignored" or omitted. Commented Oct 18, 2016 at 18:55
  • For the record: Here seems to be the same question, currently also without any (working) answer. Commented Oct 24, 2016 at 15:58
  • @AubreyBlumsohn from the statistical point of view it may as well be (I don't think so as there are statistical ways to wisely ignore outliers) but there is use for it in other areas. See the question Stefan linked for an example. Commented Dec 13, 2016 at 6:00

1 Answer 1

1

As stated already a while ago in the comment below the question there is currently no way to ignore lines in the middle or the end while reading or processing a data table (in PGFPlots directly).

But you can use the raw gnuplot feature as a workaround to achieve that. (Of course that means that you have to have gnuplot installed on your machine).

Please have a look at the comments in the code for more details.

% used PGFPlots v1.14 and gnuplot v5.0 patchlevel 3
% (inspired by my solution given at <https://tex.stackexchange.com/a/343735/95441>)
\begin{filecontents*}{test.txt}
X Z Y
1 3 0
2 4 1
3 5 3
4 6 6
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot [only marks, mark=*] table [x=X, y=Y] {test.txt};
            \addplot [dashed,red,] gnuplot [raw gnuplot] {
                % define the function to fit
                f(x) = a*x + b;
                % fit a and b by `using' columns 1 and 3 of 'test.txt' and
                % from that only the lines 1 to 3 (with the `every' key)
                fit f(x) 'test.txt' using 1:3 every ::1::3 via a, b;
                % set number of samples to 2, which is sufficient for a straight line
                set samples=2;
                % then return the resulting table to PGFPlots to plot it
                % using the x interval 1 to 4
                plot [x=1:4] f(x);
            };
             \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .