8

Since I was asked in another post:

Trend line or line of best fit in pgfplots

to ask this question explicitly, I'm now doing so:

How can I expand the linear regression fit, generated with for example (note the semilogyaxis !!):

\documentclass[fontsize=12pt,openright,oneside,DIV11,a4paper,numbers=noenddot,headsepline,parskip=half]{scrbook}
\usepackage[latin1]{inputenc}
\usepackage[cmex10]{amsmath}
\usepackage{dsfont}

% SIUnitx package
\usepackage{siunitx}
\DeclareSIUnit{\dBm}{dBm}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.3}

\begin{document}
    \begin{tikzpicture}
        \begin{semilogyaxis}[
                legend style={font=\footnotesize},
                legend pos=north east,
                legend cell align=left,
                /pgf/number format/.cd,
                use comma,
            xlabel=x,
            ylabel=y,
            ymin=,
            ymax=,
            xmin=0,
            xmax=10,
            ]             
            \addplot+[only marks,color=black, mark=square*,mark options={fill=black}] table[x=x,y=y] {measurement1.txt};
            \addlegendentry{measurement1};

% Here I would like to plot the linear regression for the whole x-axis range, not only for the x-values in measurement1.txt

            \addplot+[solid,thick,color=black, no marks] table[y={create col/linear regression={y}}] {measurement1.txt};
            \addlegendentry{linearregression1};   

        \end{semilogyaxis}
        \end{tikzpicture}
\end{document} 

to the full x-axis range (for example 0 to 10 and I have measurmentpoints ranging from 2 to 8 only)? It plots only for the x-values for the data points in measurement1.txt. Yes I could extract the slope and intercept using

\xdef\slope{\pgfplotstableregressiona}
\xdef\yintercept{\pgfplotstableregressionb}
\addplot+[solid,thick,color=black, no marks,domain=0:-10] (x,\slope*x+\yintercept); 

but if I use semilogyaxis, the line will not be a straight line any more (of course not!). Although the linear regression generated in the axis environment is perfect linear (but is not spanned over the entire x-axis range ...).

Christian
  • 165

1 Answers1

8

The equation for the regression line for logarithmically transformed data is

Y=exp(b+m*X)

where m and b are your slope and intercept, respectively. So to plot the line, you should use

\addplot {exp(\intercept+\slope*x)};

Instead of using an addplot command to determine the slope and intercept, you can do the regression outside of your axis environment using \pgfplotstablecreatecol[linear regression={ymode=log}]{<col name>}{<data table>}. Note that in that case, you have to explicitly set ymode=log. Within a semilogyaxis, this is done automatically.

Here's a complete example:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\begin{document}

\pgfplotstableread{
1   2.3
2   3.4
3   9
4   17
5   30
6   70
7   120
8   250
9   650
}\datatable

\pgfplotstablecreatecol[linear regression={ymode=log}]{regression}{\datatable}
\xdef\slope{\pgfplotstableregressiona} % save the slope parameter
\xdef\intercept{\pgfplotstableregressionb} % save the intercept parameter

\begin{tikzpicture}
\begin{axis}[
    ymode=log,
    xmin=0,xmax=10
]
\addplot [only marks, red] table {\datatable}; % plot the data
\addplot [no markers, domain=0:10] {exp(\intercept+\slope*x)}; 
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • 1
    Thank's Jake that's exactly what I was looking for. I tried it in a similar way but I used 10^{linear regression} instead of exp{linear regression}... this was the mistake :). – Christian Aug 28 '12 at 12:14