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

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

新年快乐。

我用 pgfplots 拟合了人口年数据的趋势线。但我得到的斜率值是错误的并拦截b。通过 LibreOffice Calc,我得到了以下值:f(x)= 4,478.87X– 8,966,996.64(以及在线回归工具 http://www.alcula.com/calculators/statistics/linear-regression/)。

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread{
X Y
2007 19348
2008 23457
2009 32624
2010 34270
2011 46888
2012 45224
2013 53556
2014 55007
2015 49664
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=outer north east]
\addplot [only marks, mark = *] table {\datatable};
\addplot [thick, red] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatable};
\addlegendentry{$y(x)$}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \; x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}

具有错误回归参数的趋势线

如果我将 2007-2015 的 X 范围更改为 1-9(即,如果我将图向左移动),则斜率和截距结果正常。

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread{
X Y
1 19348
2 23457
3 32624
4 34270
5 46888
6 45224
7 53556
8 55007
9 49664
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=outer north east]
\addplot [only marks, mark = *] table {\datatable};
\addplot [thick, red] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatable};
\addlegendentry{$y(x)$}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \; x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}

具有 OK 回归参数的趋势线

我该如何修复这个问题?

感谢您的帮助!

答案1

我认为这超出了 PGF 数学解析器精度的极限。您可以改为将其用作gnuplot后端来进行拟合:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\usepackage{filecontents}

\begin{filecontents*}{data.dat}
X Y
2007 19348
2008 23457
2009 32624
2010 34270
2011 46888
2012 45224
2013 53556
2014 55007
2015 49664
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    legend pos=outer north east,
    xticklabel style={/pgf/number format/1000 sep={}},
    legend cell align=left
]
\addplot [only marks, mark = *] table {data.dat};
\addlegendentry{Data}

\addplot +[raw gnuplot, thick, red, mark=none, smooth] gnuplot {
    FIT_LIMIT=1.e-14;
    f(x)=a*x+b;
    fit f(x) 'data.dat' using 1:2 via a,b;
    % Next, plot the function using the x positions from the table
    plot 'data.dat' using 1:(f($1))
    set print "parameters.dat"; % Open a file to save the parameters into$
    print a,b;
       };     
\addlegendentry[]{\pgfplotstableread{parameters.dat}\parameters% Open the file Gnuplot wrote
    \pgfplotstablegetelem{0}{0}\of\parameters \edef\paramA{\pgfplotsretval}% Get first element, save into \paramA
    \pgfplotstablegetelem{0}{1}\of\parameters \edef\paramB{\pgfplotsretval}% Get second element, save into \paramB
     $\pgfmathprintnumber{\paramA} x \pgfmathprintnumber{\paramB}$    
}  
\end{axis}
\end{tikzpicture}
\end{document}

相关内容