线性回归 - 使用 pgfplots 绘制趋势线

线性回归 - 使用 pgfplots 绘制趋势线

我正在尝试使用 pgfplots 显示趋势线。我按照手册中的说明进行操作,并环顾四周。我以为我已经正确实施了它,但没有回归线,尽管点显示正确。

有什么建议么?

\begin{tikzpicture}[scale=1]
\begin{axis}[
   /pgf/number format/.cd,
        use comma,
        1000 sep={}]
\addplot [color=blue, only marks] coordinates{
(   1985    ,   293.4445874757  )
(   1986    ,   279.3118605376  )
(   1987    ,   282.6149240816  )
(   1988    ,   275.0321316854  )
(   1989    ,   282.514265914   )
(   1990    ,   265.2009422222  )
(   1991    ,   275.1986687778  )
(   1992    ,   284.7559793069  )
(   1993    ,   276.545687619   )
(   1994    ,   292.6098138043  )
(   1995    ,   302.9776170115  )
(   1996    ,   319.0738164198  )
(   1997    ,   299.9292631959  )
(   1998    ,   361.3646108235  )
(   1999    ,   293.1112261224  )
(   2000    ,   294.2231206931  )
(   2001    ,   283.6616129293  )
(   2002    ,   313.2993810185  )
(   2003    ,   273.69578875    )
(   2004    ,   278.5970550926  )
(   2005    ,   286.7000069643  )
(   2006    ,   262.0665872115  )
(   2007    ,   245.7247934066  )
(   2008    ,   258.789477  )
(   2009    ,   239.1641025714  )
(   2010    ,   243.2148177778  )
(   2011    ,   240.0082707477  )
(   2012    ,   242.0303828571  )
(   2013    ,   230.697315  )};
\addplot table[y={create col/linear regression={y=Y}}]{
(   1985    ,   293.4445874757  )
(   1986    ,   279.3118605376  )
(   1987    ,   282.6149240816  )
(   1988    ,   275.0321316854  )
(   1989    ,   282.514265914   )
(   1990    ,   265.2009422222  )
(   1991    ,   275.1986687778  )
(   1992    ,   284.7559793069  )
(   1993    ,   276.545687619   )
(   1994    ,   292.6098138043  )
(   1995    ,   302.9776170115  )
(   1996    ,   319.0738164198  )
(   1997    ,   299.9292631959  )
(   1998    ,   361.3646108235  )
(   1999    ,   293.1112261224  )
(   2000    ,   294.2231206931  )
(   2001    ,   283.6616129293  )
(   2002    ,   313.2993810185  )
(   2003    ,   273.69578875    )
(   2004    ,   278.5970550926  )
(   2005    ,   286.7000069643  )
(   2006    ,   262.0665872115  )
(   2007    ,   245.7247934066  )
(   2008    ,   258.789477  )
(   2009    ,   239.1641025714  )
(   2010    ,   243.2148177778  )
(   2011    ,   240.0082707477  )
(   2012    ,   242.0303828571  )
(   2013    ,   230.697315  )};
\end{axis}
\end{tikzpicture}

答案1

首先,您似乎需要包含pgfplotstable包。几天前我也遇到过类似的问题。像这样写入数据帮我解决了这个问题。:

\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\begin{tikzpicture}
    \pgfplotsset{width=10cm,
        compat=1.3,
        legend style={font=\footnotesize}}
    \begin{axis}[
    xlabel={Glukosekonzentration [\si{\gram\per\liter}]},
    ylabel={Absorption $[-]$},
    legend cell align=left,
    legend pos=north west]
    \addplot[only marks] table[row sep=\\]{
        X Y\\
        0.1 0.147\\
        0.1 0.165\\
        0.8 0.918\\
        0.8 1.149\\
    };
    \addlegendentry{Messpunkte}
    \addplot table[row sep=\\,
    y={create col/linear regression={y=Y}}] % compute a linear regression from the
    %input table
    {
        X Y\\
        0.1 0.147\\
        0.1 0.165\\
        0.8 0.918\\
        0.8 1.149\\
    };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. Regression} %
    \end{axis}
    \end{tikzpicture}

在这个例子中,您告诉pgfplots属于 Y 轴的点,以便能够计算它。

结果如下:

在此处输入图片描述

答案2

在此处输入图片描述

从模块 中检出leastsquares功能。 :Asymptote statstr.asy

import graph;
import markers;
import stats;
real[] xp={
 1985
,1986
,1987
,1988
,1989
,1990
,1991
,1992
,1993
,1994
,1995
,1996
,1997
,1998
,1999
,2000
,2001
,2002
,2003
,2004
,2005
,2006
,2007
,2008
,2009
,2010
,2011
,2012
,2013
};
real[] yp={
 293.4445874757
,279.3118605376
,282.6149240816
,275.0321316854
,282.514265914 
,265.2009422222
,275.1986687778
,284.7559793069
,276.545687619 
,292.6098138043
,302.9776170115
,319.0738164198
,299.9292631959
,361.3646108235
,293.1112261224
,294.2231206931
,283.6616129293
,313.2993810185
,273.69578875  
,278.5970550926
,286.7000069643
,262.0665872115
,245.7247934066
,258.789477  
,239.1641025714
,243.2148177778
,240.0082707477
,242.0303828571
,230.697315  
};

real w=300; real h=200;
size(w,h,IgnoreAspect);

real xmin=1984; real xmax=2014;
real ymin=50; real ymax=400;
fixedscaling((xmin,ymin),(xmax,ymax));

frame mark=newframe;
pen markPen=darkblue+0.8pt;
real markScale=2.2;
filldraw(mark,scale(markScale)*polygon(5),yellow,markPen);

linefit lf=leastsquares(xp,yp);
//  struct linefit {
//    real m,b;     // slope, intercept
//    real dm,db;   // standard error in slope, intercept
//    real r;       // correlation coefficient
//    real fit(real x) {
//      return m*x+b;
//    }
//  }
defaultpen(fontsize(10pt));
string trendStr="$y="+format("%4.2f",lf.m)+"x+"+format("%4.2f",lf.b)+"$";

draw(graph(xp,yp),nullpen, legend="data", marker(mark));

pen trendPen=red+1.3pt+opacity(0.7);
guide trendLine=graph(lf.fit,xmin+1,xmax-1);

draw(trendLine,legend=trendStr,trendPen);

add(legend(),point(S),-20S,UnFill);

xaxis(YEquals(ymin),xmin,xmax,RightTicks(Step=5,step=1));
yaxis(XEquals(xmin),ymin,ymax,LeftTicks(Step=50,step=25,beginlabel=false));

运行asy -f pdf tr.asy以获取独立的tr.pdf

相关内容