具有固定点的线性回归

具有固定点的线性回归

我有一个数据集,我想为其添加一条趋势线。对于我的系统,在 (2, 0) 上始终会有一个固定点。我有一条代表该系统的线,但目前我没有考虑固定点。是否有功能可以实现此目的?

\begin{figure}
\begin{tikzpicture}
\begin{axis} [
    ylabel={Some Time [ms]},
    ]
    \addplot[only marks, color=red, error bars/.cd,
                       y dir=both, y explicit]
        table [x=num_of_nodes, y=MyData, y error=stdev_delay_var] {MyData.dat};
    \label{plot_one}
    \addplot +[mark=none, dashed] 
        table[y={create col/linear regression={y=MyData}}]
        {MyData.dat};
\end{axis}
\end{tikzpicture}
\caption{Test Results}
\label{fig:test_results}
\end{figure}

答案1

您可以使用raw gnuplot函数来实现这一点。有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
    % because there where no data to play with, I created some dummy data
    \begin{filecontents*}{MyData.dat}
        x    y
        0    2.831
        1    2.843
        2    4.580
        3    4.808
        4    6.825
        5    7.000
        6    8.611
        7    9.295
        8    9.159
        9    11.773
        10   11.923
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        % some options to have a nice picture
        xmin=-1,
        ymin=-1,
        axis lines=middle,
        legend style={
            at={(axis cs:9.8,0.2)},
            anchor=south east,
        },
        legend cell align=left,
    ]
        % show the data points
        \addplot+ [only marks] table [
            x=x,
            y=y,
        ] {MyData.dat};

        % here the "normal" linear regression using the equation $y = a*x + b$
        \addplot+ [
            mark=none,
            blue,
            thick,
        ] table [
            y={create col/linear regression={y=y}}
        ] {MyData.dat};

        % here the "modified" linear regression using the equation $ y = a*x + 2$
        % (for that we use the `raw gnuplot' feature ...
        \addplot+ [
            mark=none,
            red,
            thick,
        ] gnuplot [raw gnuplot,id=mod_lin_reg] {
            % ... define the function to fit, ...
            f(x)=a*x + 2;
            % ... fit the function ...
            fit f(x) 'MyData.dat' using 1:2 via a;
            % ... set the number of samples to two (which is enough for a line) ...
            set samples 2;
            % ... and plot the result in the given domain
            plot [x=0:10] f(x);
        };
            % this is used to calculate the slope of the modified function
            % (for the legend entry)
            \pgfplotstableread[header=false]{\jobname.mod_lin_reg.table}{\datatable}
            \pgfplotstablegetelem{1}{[index]1}\of{\datatable}
                \pgfmathsetmacro{\NewSlope}{(\pgfplotsretval-2)/10}

        \legend{
            data points,
            $y = \pgfmathprintnumber{\pgfplotstableregressiona} \, x
                 + \pgfmathprintnumber{\pgfplotstableregressionb}$,
            $y = \pgfmathprintnumber{\NewSlope} \, x + 2$,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容