將線性迴歸擴展到整個圖

將線性迴歸擴展到整個圖

我有一組數據,我想對它們執行線性迴歸並顯示結果線。由於計算精度的原因,這些值在某個點後達到穩定水平,我不想將這些數據包含在迴歸計算中。

我目前正在做的是有兩個數據文件:一個包含所有數據,另一個包含用於唯一線性回歸的數據(也許不是最好的,但我沒有找到其他任何內容)。這是一個 MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\begin{filecontents}[overwrite,noheader]{data.dat}
h err
0.05 3.11604689111e-09
0.025 1.89857594659e-11
0.0125 3.14299545014e-13
0.00625 2.38864314351e-14
0.003125 4.93001614302e-14
\end{filecontents}
\begin{filecontents}[overwrite,noheader]{data_for_reg.dat}
h err
0.05 3.11604689111e-09
0.025 1.89857594659e-11
0.0125 3.14299545014e-13
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmode=log, ymode=log]
\addplot[only marks,blue] table[x=h,y=err]{data.dat};
\addplot[no markers,red,thick] table [x=h,y={create col/linear regression={y=err}}] {data_for_reg.dat};
\end{axis}
\end{tikzpicture}
\end{document}

得到一個包含所有資料(藍色)和所選點的線性迴歸的圖,但僅在這些點上(紅色): 在此輸入影像描述

我想做的是將這條線延伸到整個繪圖,如上圖中的綠色所示(綠色已手動添加到螢幕截圖中)

答案1

來自pgfplots手冊(第 4.24 節擬合線 – 迴歸),迴歸的參數儲存在\pgfplotstableregressiona和中\pgfplotstableregressionb。因此,繪製相應的函數,並使用適當的域和update limits=false保持軸不變的鍵:

\addplot[update limits=false, no markers, green, thick, domain={1e-3:1e-1}]
{x^\pgfplotstableregressiona*exp(\pgfplotstableregressionb)};

例子:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\begin{filecontents}[overwrite,noheader]{data.dat}
h err
0.05 3.11604689111e-09
0.025 1.89857594659e-11
0.0125 3.14299545014e-13
0.00625 2.38864314351e-14
0.003125 4.93001614302e-14
\end{filecontents}
\begin{filecontents}[overwrite,noheader]{data_for_reg.dat}
h err
0.05 3.11604689111e-09
0.025 1.89857594659e-11
0.0125 3.14299545014e-13
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmode=log, ymode=log]
\addplot[only marks,blue] table[x=h,y=err]{data.dat};
\addplot+[no markers, white] table [x=h,y={create col/linear regression={y=err}}] {data_for_reg.dat};
\addplot[update limits=false, no markers, green, thick, domain={1e-3:1e-1}]
{x^\pgfplotstableregressiona*exp(\pgfplotstableregressionb)};
\end{axis}
\end{tikzpicture}
\end{document}

擬合數據

相關內容