루프를 사용한 회귀선

루프를 사용한 회귀선

다음 코드에서 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

pgfplotspgfplotstable선형 회귀에 사용할 수 있습니다 . 회귀계수 값은 \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} 

관련 정보