プロットに線形回帰線を追加したいと思います。MWE は次のとおりです。
\begin{document}
\pgfplotstableread{
Year ICumCDP
2008 3.73E-07
2009 3.35E-07
2010 2.01E-07
2011 3.88E-07
2012 1.36E-06
}{\tableICumCDP}
\pgfplotstablecreatecol[linear regression={ymode=log}, x=Year, y=ICumCDP]{regression}{\tableICumCDP}
\xdef\slope{\pgfplotstableregressiona} % save the slope parameter
\xdef\intercept{\pgfplotstableregressionb} % save the intercept parameter
\begin{tikzpicture}
\begin{semilogyaxis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
log basis y = 10,
ylabel = {ICumCDP},
ymin = 1E-8,
ymax = 1E-4,
yminorgrids = true,
xlabel = {Year},
xtick = {2008,2009,2010,2011,2012}
]
\addplot [only marks, color=blue, x=Year, y=ICumCDP] table {\tableICumCDP};
\addplot [no markers, color=red, domain=2008:2012] {exp(x*\slope + \intercept)};
\end{semilogyaxis}
\end{tikzpicture}\\
\end{document}
印刷された回帰直線が正しくありません:
私のコードはsemilogyaxis を使用しながら線形回帰フィットを x 軸の全範囲に拡張する方法
私は同じ質問をしたドイツのフォーラム、しかし今まで誰も私を助けてくれませんでした。
答え1
これは数値の不正確さ (年の数値が大きい、y の数値が小さい) が原因で発生するようです。データを変換すると (オンザフライで実行できます)、回帰が機能します。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\pgfplotstableread{
Year ICumCDP
2008 3.73E-07
2009 3.35E-07
2010 2.01E-07
2011 3.88E-07
2012 1.36E-06
}{\tableICumCDP}
\pgfplotstableset{
create on use/shifted year/.style={ % Make the row number available
create col/expr=\thisrow{Year}-2008
}
}
\pgfplotstablecreatecol[linear regression={ymode=log, x=shifted year}]{regression}{\tableICumCDP}
\xdef\slope{\pgfplotstableregressiona} % save the slope parameter
\pgfmathsetmacro\intercept{\pgfplotstableregressionb-\slope*2008}
\begin{tikzpicture}
\begin{semilogyaxis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
log basis y = 10,
ylabel = {ICumCDP},
ymin = 1E-8,
ymax = 1E-4,
yminorgrids = true,
xlabel = {Year},
xtick = {2008,2009,2010,2011,2012}
]
\addplot [only marks, color=blue, x=Year, y=ICumCDP] table {\tableICumCDP};
\addplot [no markers, color=red, domain=2008:2012] {exp((x)*\slope + \intercept)};
\end{semilogyaxis}
\end{tikzpicture}\\
\end{document}