# PGFPlots: calculate a linear regression, ignoring some data

I know that I can use PGFPlots to calculate a linear regression with the create col/linear regression command.

I have a set of data that is curved for the first few points, but linear for the remainder of the data set. I want to find a linear regression but ignore the first n points from my data file in my regression. Is this possible with PGFPlots?

A contrived example of this data would be:

x     y
0     0
1     15
2     25
3     28
4     30
5     31
6     32
7     33
8     34
9     35
10    36


where the curve is linear starting at x = 4. A plot of this looks like:

One (not great) solution I can think of is to make a new data set without the data points and run my regression on this new set.

• Welcome to TeX.SX! It would be nice, if you can put an example file with some data points in the question to make it easier for someone, who might want to answer the question. – Heiko Oberdiek May 30 '15 at 19:51
• Good idea! I'll edit one in. – Greg d'Eon May 30 '15 at 19:55

The first lines of the table can be ignored by option skip first n. The first plot of the following example shows the calculated regression line for the calculated area between point 5 and the last point. The second plot draws the line over the full range using the calculated parameters of the regression line.

\begin{filecontents*}{\jobname-plot.dat}
x     y
0     0
1     15
2     25
3     28
4     30
5     31
6     32
7     33
8     34
9     35
10    36
\end{filecontents*}

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
\begin{axis}
table {\jobname-plot.dat};
y={create col/linear regression},
] {\jobname-plot.dat};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
table {\jobname-plot.dat};
table[
skip first n=5,
y={create col/linear regression},
] {\jobname-plot.dat};
{\pgfplotstableregressiona*x + \pgfplotstableregressionb};
\end{axis}
\end{tikzpicture}
\end{document}


• Nice! Exactly what I was hoping for. – Greg d'Eon May 31 '15 at 20:39

Here is a solution using R and knitr embedded inside of a LaTeX file. This assumes you have R installed and understand how to run the knitr package. http://yihui.name/knitr/

\begin{filecontents}{data.txt}
x    y
0     0
1    15
2    25
3    28
4    30
5    31
6    32
7    33
8    34
9    35
10   36
\end{filecontents}
\documentclass[10pt,letterpaper]{article}

\begin{document}
<<echo=FALSE,out.width="3in">>=