2

I am to teach, how to do linear regression, and to do so I am making my slides in beamer and I am using PGFplots to do the linear regression, i.e.

\begin{tikzpicture}[scale=0.7]
  \begin{axis}
  \addplot[only marks] table[x=Lat,y=Mort]{skincancer.txt};
  \addplot[dashed] table[x=Lat,y={create col/linear regression={y=Mort}}]{skincancer.txt};
  \end{axis}
\end{tikzpicture}

Where skincancer.txt is just some textfile containing some data which is somewhat linear dependent.

What I am interested in doing is to mark 2 points on the regression line, in order to remind the students how to find the parameters (alpha and beta) for the linear regression line y = \alpha x+\beta.

EDIT: The goal is to not manually enter any values. All values should be grabbed from the dataset that I import. Though it is okay that I have to specify that it is the ith and n-ith points that I am interested in.

  • Something like \draw (axis cs:x,y) circle(5mm); replacing x and y with the coordinates? – JPi Mar 27 '17 at 12:39
  • Well yes, but the coordinates are unknown to me. They are something PGFplots have calculated. – Nicky Mattsson Mar 27 '17 at 12:43
2

The coefficients are available in \pgfplotstableregressiona and \pgfplotstableregressionb, so you can just add a second plot with only markers and samples at specific x-values. I took the example below from the manual, and edited it slightly.

To get the x-values from row i and j of the data table, you can do something like in the code below. I don't know if this is particularly fast, but it seems to work.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread{
X Y
1 1.1
2 1.8
3 3.5
4 6
5 4.9
6 7
}\mydata

\begin{document}

% Preamble: \pgfplotsset{width=7cm,compat=1.14}
\begin{tikzpicture}
\begin{axis}[legend pos=outer north east]
\addplot +[only marks,mark=x] table[x=X,y=Y] {\mydata};
\addplot [red] table[
y={create col/linear regression={y=Y}}] {\mydata};

% plot dots on the regression line
\addplot [
   only marks,
   mark=*] table[
   y expr={\pgfplotstableregressiona*
            ifthenelse( % \coordindex starts counting from 0
                  \coordindex==1,% row i-1
                  \thisrow{X}, % X-value from row i-1
                   ifthenelse(\coordindex==4, % row j-1
                         \thisrow{X}, % X-value from row j-1
                         nan)) % if other row numbers, use nan
            + \pgfplotstableregressionb},
   ]
 {\mydata};

%\xdef\slope{\pgfplotstableregressiona} %<-- might be handy occasionally
\addlegendentry{$y(x)$}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}
  • Are there any to get the x coordinates directly from the dataset? like asking for the 3rd and 5th to last points? – Nicky Mattsson Mar 27 '17 at 12:44
  • @NickyMattsson Ehm, probably. Maybe you can do it with \pgfplotstablegetelem (from pgfplotstable). I don't have time to figure it out at the moment though, maybe later today. – Torbjørn T. Mar 27 '17 at 12:53
  • @NickyMattsson I updated my answer to read the second and fifth x-value from the table. Change the values after \coordindex== to choose the row numbers. – Torbjørn T. Mar 28 '17 at 20:26

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.