0

I have the data in 001.csv below that I want to plot. I need two plots: one showing the data and another one showing logarithmic regression. I have no problems plotting the line for the data (and also adding a linear regression line) but I just can't manage to get a logarithmic regression line in there. As far as I understand, I need to use gnuplot for this but unfortunately I can't make it work. Any help would be much much appreciated.

Cheers,

Cem

%%%%%%%%%%%%%%%%%%%%%%
%This is my data:

\begin{filecontents}{001.csv}
    1990 275
    1991 187
    1992 249
    1993 177
    1994 212
    1995 303
    1996 323
    1997 364
    1998 461
    1999 427
    2000 683
    2001 553
    2002 571
    2003 682
    2004 585
    2005 608
    2006 609
    2007 643
    2008 719
    2009 545
\end{filecontents}

%%%%%%%%%%%%%%%%%%%%%%
%This is my plot for the data:

\begin{tikzpicture}
\begin{axis}[
    axis on top=false, axis x line=middle, axis y line=middle,
    xlabel={\footnotesize Year},
    ylabel={\footnotesize Frequency count},
    xmin=1975, xmax=2009,
    ymin=0, ymax=900,
    xtick={1980,1985,1990,1995,2000,2005,2009},
    ytick={0,100,200,300,400,500,600,700,800},
    legend pos=north east,
    legend style={font=\fontsize{6}{5}\selectfont},
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    /pgf/number format/.cd,
    1000 sep={}
    ]

\addplot[
    smooth,tension=0.9,color=blue,mark=diamond
        ] table {001.csv};

%Somewhere here I need a logarithmic regression line.

\end{axis}
\end{tikzpicture}

%%%%%%%%%%%%%%%%%%%%%%
  • Welcome to TeX.SX. Sorry, but the plot doesn't look like a logarithmic regression would be much better than a linear one. Are you sure you need a log regression? – Stefan Pinnow Jan 23 '17 at 21:36
  • Thanks Stefan! You see, I'm trying to propose an estimated date for the beginning of a certain phenomenon by extrapolating backwards using a regression line. Logarithmic regression gives me a much more reasonable estimate than a linear one. So, yes, log regression would be great :-) – Cem Jan 23 '17 at 21:56
1

As I already stated in the comments, the data points don't look like the log plot would be a good, or let's say, better choice than a linear one. So here I present a solution showing both lines.

    \begin{filecontents}{001.csv}
        1990 275
        1991 187
        1992 249
        1993 177
        1994 212
        1995 303
        1996 323
        1997 364
        1998 461
        1999 427
        2000 683
        2001 553
        2002 571
        2003 682
        2004 585
        2005 608
        2006 609
        2007 643
        2008 719
        2009 545
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=center,
            xlabel={\footnotesize Year},
            ylabel={\footnotesize Frequency count},
            xmin=1985, xmax=2014,
            ymin=0, ymax=890,
            ymajorgrids=true,
            xmajorgrids=true,
            grid style=dashed,
            /pgf/number format/.cd,
                1000 sep={},
        ]

            \addplot[
                only marks,
                color=blue,
                mark=diamond*,
            ] table {001.csv};

            \addplot [draw=red,very thick] gnuplot [raw gnuplot] {
                f(x)= a*log(x) + b;
                fit f(x) "001.csv" using 1:2 via a,b;
                plot [x=1990:2009] f(x);
            };

            \addplot [draw=green,very thick, dashed] gnuplot [raw gnuplot] {
                % give some useful initial values
                a=25;
                b=-50000;
                f(x)= a*x + b;
                fit f(x) "001.csv" using 1:2 via a,b;
                plot [x=1990:2009] f(x);
            };

        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • Great, thanks so much Stefan! Is there a way to extend the regression line all the way to the x-axis, to extrapolate backwards? And, yes, there isn't much difference between log and linear here. In LibreOffice Calc the two kinds of regression look very different, yielding a difference of around 4 to 5 years in the estimate. – Cem Jan 23 '17 at 22:26
  • Sure. Change the interval in the line plot [x=1990:2009] f(x) to whatever you need. If my answer solved your problem you can think of -- besides upvoting it -- accepting it (by clicking on the checkmark ✓). Then this question disappears from the "still open" list. Otherwise please either edit your question or ask a follow-up question. Thanks. – Stefan Pinnow Jan 23 '17 at 22:36

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.