使用循环绘制回归线

使用循环绘制回归线

请建议如何使用以下代码中的 X 和 Y 列的值来拟合 a 和 b

\documentclass{article}
\usepackage{gnuplot-lua-tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{\gpbboxborder}


\begin{document}

\begin{tikzpicture}[gnuplot]

\begin{axis}[
    width=10cm,
    xlabel={something},
    ylabel={something},
]
    \addplot [blue,only marks] table [x=X, y=Y] {
    X       Y
    145444  3746643.308
    145250  3972396.385
    145183  5026818
    145449  8220037.462
    145355  7696519.385
    145347  7952214.231
    145394  5869103.66
    145421  7268796.583
    145249  8472536.083
    145237  9305086.167
    145358  5155636.25
    145416  6077647.583
    145337  5861633.167
    145384  4426391.667
    145229  4101591.378
    145392  4570673.462
    145271  6972571.692
    145287  6527322.308
    145319  4211914.846
    145321  4735368.385
    145411  7849477.75
    145349  8152333.083
    145233  4891463
    145261  5013476.583
    145424  4030219.154
    145381  4528078.154
    145326  5445707.231
    145413  5461362.231
    145311  5619711.846
    145409  9969929.462
    145248  5306496.923
    145270  7.53E+06
    145281  4478472.083
    145341  5173546.667
    };
   {f(x)};
    \addplot [thick,red,dashed,id=test] gnuplot [raw gnuplot] {
    X       Y
    145444  3746643.308
    145250  3972396.385
    145183  5026818
    145449  8220037.462
    145355  7696519.385
    145347  7952214.231
    145394  5869103.66
    145421  7268796.583
    145249  8472536.083
    145237  9305086.167
    145358  5155636.25
    145416  6077647.583
    145337  5861633.167
    145384  4426391.667
    145229  4101591.378
    145392  4570673.462
    145271  6972571.692
    145287  6527322.308
    145319  4211914.846
    145321  4735368.385
    145411  7849477.75
    145349  8152333.083
    145233  4891463
    145261  5013476.583
    145424  4030219.154
    145381  4528078.154
    145326  5445707.231
    145413  5461362.231
    145311  5619711.846
    145409  9969929.462
    145248  5306496.923
    145270  7.53E+06
    145281  4478472.083
    145341  5173546.667
        f(x) = a*x + b;           
        a = 2000;
        b = -3e8;
        % fit a and b by `using' columns 1 and 2 of 'data.txt'
        fit f(x) 'X Y' using 1:2 via a, b;
        straight line
        set samples 2;            
        plot [x=145183:145449] f(x);
    };
   \end{axis}

\end{tikzpicture}

\end{document} 

循环可能是一个解决方案,但无法弄清楚如何运行它。

无法在序言中的 .text 文件中定义值,因为需要在文档中添加多个图。

答案1

pgfplots以及pgfplotstable可用于线性回归。回归系数的值将存储在 \pgfplotstableregressiona和中\pgfplotstableregressionb

在此处输入图片描述

请参阅下面给出的代码。请注意,\res在应用回归之前,数据已排序并保存。

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel={something},
    ylabel={something},
]
\pgfplotstablesort{\res}{
    X       Y
    145444  3746643.308
    145250  3972396.385
    145183  5026818
    145449  8220037.462
    145355  7696519.385
    145347  7952214.231
    145394  5869103.66
    145421  7268796.583
    145249  8472536.083
    145237  9305086.167
    145358  5155636.25
    145416  6077647.583
    145337  5861633.167
    145384  4426391.667
    145229  4101591.378
    145392  4570673.462
    145271  6972571.692
    145287  6527322.308
    145319  4211914.846
    145321  4735368.385
    145411  7849477.75
    145349  8152333.083
    145233  4891463
    145261  5013476.583
    145424  4030219.154
    145381  4528078.154
    145326  5445707.231
    145413  5461362.231
    145311  5619711.846
    145409  9969929.462
    145248  5306496.923
    145270  7.53E+06
    145281  4478472.083
    145341  5173546.667
    };
   {f(x)};
    \addplot [blue,only marks] table [sort, sort key=X, x=X, y=Y] {\res};
    \addplot [dashed,red,thick] table [x=X, y={create col/linear regression={y=Y}}] {\res};
    \addlegendentry {data}
    \addlegendentry{%
    $\pgfmathprintnumber{\pgfplotstableregressiona} \times x
    \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
   \end{axis}
\end{tikzpicture}
\end{document} 

相关内容