8

Can anyone explain the uneven appearance of the regression line in this plot?

\documentclass[border=0.5cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{
a,b
5,14
4,13
6,11
8,12
8,10
5,9
3,12
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [only marks] table [x=a, y=b] {\data};
\addplot [dashdotted] table [x=a, y={create col/linear regression={y=b}}] {\data};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here enter image description here

7

Pgfplots decomposes the regression line into small overlapping segments. Luckily it computes the parameters of the regression line and stores them into \pgfplotstableregressionb and \pgfplotstableregressiona, which allows one to draw the regression line as an ordinary single-stroke plot.

\documentclass[border=0.5cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{
a,b
5,14
4,13
6,11
8,12
8,10
5,9
3,12
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [only marks] table [x=a, y=b] {\data};
\addplot [draw=none] table [x=a, 
y={create col/linear regression={y=b}}] {\data};
\addplot[dashdotted,domain=3:8] {\pgfplotstableregressionb+\pgfplotstableregressiona*x};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

If you do not like the fact that the domain 3:8 is hard coded, you could do

\documentclass[border=0.5cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{
a,b
5,14
4,13
6,11
8,12
8,10
5,9
3,12
}\data
% from https://tex.stackexchange.com/a/445369/121799
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{#3}\of{#1}%
    \let#4\pgfplotsretval
}
\newcommand{\GetDomain}[3]{
\pgfplotstablegetrowsof{\data}
\pgfmathtruncatemacro{\rownumber}{\pgfplotsretval-1}
\ReadOutElement{\data}{0}{#1}{\tmp}
\let#2\tmp
\let#3\tmp
\foreach \XX in {1,...,\rownumber}
{
\ReadOutElement{\data}{\XX}{#1}{\tmp}
\pgfmathsetmacro{#2}{min(#2,\tmp)}
\pgfmathsetmacro{#3}{max(#3,\tmp)}
\xdef#2{#2}
\xdef#3{#3}
}
}
%
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [only marks] table [x=a, y=b] {\data};
\addplot [draw=none] table [x=a, 
y={create col/linear regression={y=b}}] {\data};
\GetDomain{a}{\amin}{\amax}
\addplot[dashdotted,domain=\amin:\amax] {\pgfplotstableregressionb+\pgfplotstableregressiona*x};
\end{axis}
\end{tikzpicture}
\end{document}
  • Why does pgfplots do that? Given the result this seems like a clear bug. And linear regression never requires more than a single line segment, only nonlinear/local regression does. – Konrad Rudolph Oct 25 '18 at 13:59
  • 1
    @KonradRudolph I wouldn't call it a bug. I think it was just not foreseen that someone wants to draw a dash-dotted line in a data set in which the x coordinates do not appear in an ascending order. – user121799 Oct 25 '18 at 14:23
  • 1
    @marmot - Kind of sounds like a bug to me – Dason Oct 25 '18 at 14:57
  • 1
    @Dason Sure, this is something one may argue about. My attitude it that pgfplots is a really great tool, which allows one to do many nice things, and it provides means to draw the dash-dotted line with little effort. IMHO it would be a bug if the line was off (incorrect slope or sth like this), but this is not the case. All one has to do is to work a tiny bit harder to get the result. But of course it is a matter of taste which point of view one finds more convincing. – user121799 Oct 25 '18 at 15:02
  • 1
    It's not really for me to say if it's a bug or not, but just to note: the effect is apparent on other types of line (dotted, dashed), not just dash-dotted. – Gareth Walker Oct 25 '18 at 15:17

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.