如何計算並顯示線性迴歸的新值

如何計算並顯示線性迴歸的新值

下面的MWE展示了我在大學使用的一個模板,用於顯示校準數據和相應的線性回歸。現在我問是否可以計算給定 $Y$ 值的 $X$ 值並將其顯示在類似於下圖的圖表中。

在圖中顯示計算值

到目前為止,我必須手動計算這些值並給它們一個心愛的 pgfplots。

我希望問題足夠清楚並且有一個半自動的解決方案。

PS:我使用LuaLaTeX進行編譯。為了更好的可用性,我註解掉了我使用的字體。

\documentclass{standalone}

\usepackage{luaotfload}
\usepackage[no-math]{fontspec}
%\defaultfontfeatures{Ligatures=TeX} %,Scale=MatchLowercase} still buggy
%\setmainfont{Linux Biolinum O}
%\setsansfont{Linux Biolinum O}
%\setmonofont{Linux Libertine Mono O}

\usepackage{polyglossia}
\setmainlanguage{german}
\usepackage[autostyle=true]{csquotes}

\usepackage[
    tbtags,
    sumlimits,
    intlimits,
    namelimits
    ]{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amscd}
\setcounter{MaxMatrixCols}{12}
\usepackage[german]{translator}
\usepackage[detect-all=true]{siunitx}
\sisetup{
%   strict,
    output-decimal-marker={,},
    exponent-product=\cdot,
    text-micro={\fontfamily{mdbch}\textmu},
    math-micro=\muup
}
\DeclareSIUnit\molar{M}
\usepackage[math-style=ISO,bold-style=ISO]{unicode-math}
\setmathfont{Asana Math}
\usepackage{lualatex-math}

\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    title={Kalibration für den Versuch XYZ},
    /pgf/number format/use comma,
    width=\linewidth,
    grid=major,
    grid style={dashed,gray!30},
    x label style={at={(axis description cs:.5,-.025)},anchor=north},
    y label style={at={(axis description cs:.05,.5)},anchor=south},
    xlabel=Konzentration~{[}\si{\milli\gram\per\milli\litre}{]},
    ylabel=Extinktion,
%   xtick={0,60,90,120,150,180},
    x tick label style={rotate=90,anchor=east},
    legend cell align=left,
    legend pos=north west,
    ]
    \addplot[
    only marks,
    thick,
    color=black,
    mark=*,
    mark options={fill=red},
    ] table [x=X,y=Y, row sep=\\] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{Messpunkte}
    \addplot[thick,color=blue] table[row sep=\\,y={create col/linear regression={y=Y}},mark=none] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$};
    \draw[thick,color=blue] (axis cs:20,-1) -- (axis cs:20,.15609);
    \draw[thick,color=blue] (axis cs:-20,.15609) -- (axis cs:20,.155);
    \node [coordinate,pin=below right:{\scriptsize\textit{minimal effective concentration}}] at (axis cs:20,.15609) {};
    \end{axis}
    \end{tikzpicture}
\end{document}

答案1

繪製這些線的最簡單方法是將所有值定義為巨集並使用 計算 x 值\pgfmathparse{(\myY-\pgfplotstableregressionb)/\pgfplotstableregressiona}。結果可以另存為\pgfmathsetmacro\myX{\pgfmathresult}.

% % Y-Value % %
\def\myY{0.155}
% % % % % % % %

\def\xMin{0}
\def\yMin{0} 

最小值用於與軸的交點。

\documentclass{standalone}

\usepackage[german]{translator}
\usepackage[detect-all=true]{siunitx}
\sisetup{
%   strict,
    output-decimal-marker={,},
    exponent-product=\cdot,
    text-micro={\fontfamily{mdbch}\textmu},
    math-micro=\muup
}
\DeclareSIUnit\molar{M}


\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

% % Y-Value % %
\def\myY{0.155}
% % % % % % % %

\def\xMin{0}
\def\yMin{0} 

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    title={Kalibration für den Versuch XYZ},
    /pgf/number format/use comma,
    width=\linewidth,
    grid=major,
    grid style={dashed,gray!30},
    x label style={at={(axis description cs:.5,-.025)},anchor=north},
    y label style={at={(axis description cs:.05,.5)},anchor=south},
    xlabel=Konzentration~{[}\si{\milli\gram\per\milli\litre}{]},
    ylabel=Extinktion,
%   xtick={0,60,90,120,150,180},
    xmin=\xMin,
    ymin=\yMin,
    x tick label style={rotate=90,anchor=east},
    legend cell align=left,
    legend pos=north west,
    ]
    \addplot[
    only marks,
    thick,
    color=black,
    mark=*,
    mark options={fill=red},
    ] table [x=X,y=Y, row sep=\\] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{Messpunkte}
    \addplot[thick,color=blue] table[row sep=\\,y={create col/linear regression={y=Y}},mark=none] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$};
        \pgfmathparse{(\myY-\pgfplotstableregressionb)/\pgfplotstableregressiona}     
    \pgfmathsetmacro\myX{\pgfmathresult} 
    \draw[thick,color=blue] (axis cs:\myX,\yMin) -- (axis cs:\myX,\myY);
    \draw[thick,color=blue] (axis cs:\xMin,\myY) -- (axis cs:\myX,\myY); 
    \node [coordinate,pin=below right:{\scriptsize\textit{minimal effective concentration}}] at (axis cs:\myX,\myY) {};
    \end{axis}
    \end{tikzpicture}
\end{document} 

相關內容