# PGFPlots: calculate linear regression of part of the plot [duplicate]

I'm trying to calculate the young modulus of a certain material using it's load-deflection curve. For such, I need to get the slope of the linear part of the curve (just the beginning). But my files contain the whole data, so I tried to remove the rest of the data by restricting the domain as shown in This Question, didn't work. Then I came upon this one: PGFPlots: calculate a linear regression, ignoring some data.

I thought it would solve my problem but the answer is kind of incomplete, as it only tells how to ignore the beginning of the data which is the opposite of what I want. I also saw a question on how to mod the file but there must be an easier way I'm not seeing.

Here's a MWE (with the domain restricted)

    \usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12, legend pos=north west, legend style={draw=none}}

\begin{tikzpicture}
\begin{axis}[xlabel=Deformação (mm),ylabel=Força (N), ymin=0, width=\textwidth, height=\textwidth, scaled ticks=false, ytick={0,10000,20000,30000,40000,50000}]
\addplot +[restrict x to domain=7:14] [no markers, red, thick] table [x=Deformacao, y=Forca, col sep=semicolon]{cp2.csv};
\addplot +[restrict x to domain=7:14] [no markers, black, dashed] table [y={create col/linear regression={y=Forca}}, col sep=semicolon]{cp2.csv};
\addlegendentry{E=$\pgfmathprintnumber{\pgfplotstableregressiona}$(N/mm)}
\end{axis}
\end{tikzpicture}


This is the result:

Here's the complete data (no domain restriction)

And if it's of interest this is the cp2.csv file

• You can use \addplot[y filter/.expression={y>20 ? nan : y}] to filter out the coordinates in \addplot[y filter/.expression={y>20 ? nan : y}] table[ y={create col/linear regression}, ] {\jobname-plot.dat};. Change 20 as you wish. – user11232 Aug 25 '15 at 0:45
• Sorry, I'm putting the whole data there, theres poinst where y is less than 50000, which would be your 20, so that doesn't work. =/ – Guilherme Zanotelli Aug 25 '15 at 0:53
• For the record: Here seems to be the same question, currently also without any (working) answer. – Stefan Pinnow Oct 24 '16 at 15:59
• As stated in my previous comment there is a similar question dealing with that problem. I provided there a "workaround" solution using gnuplot using the raw gnuplot feature of PGFPlots. I think this solution could easily be adapted to your specific problem, if "using gnuplot" is an option for you. Please let us know. – Stefan Pinnow Dec 12 '16 at 22:15
• @StefanPinnow thanks for the workaround. I guess using gnuplot is an option, yes. Although it would be better if not needed, but right now I have already delivered my report and passed the subject. Don't you think that the question you linked is a duplicate? – Guilherme Zanotelli Dec 13 '16 at 5:56