使用循環的迴歸線

使用循環的迴歸線

請建議如何使用以下程式碼中的 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} 

相關內容