Here's a probably less code intense approach. Unfortunately I can only show you the recipe, as I have next to zero experience with the pgf-functions involved. But I think an other user can fill the coding gaps more easily.
Here's the recipe, using only, what's already available from pgfplots
, using symbols known from robust technology development:
(P.S.: You can skip the L-calculation and get R2 from b, r and St alone by simple replacements.)
Code for this image:
\documentclass[10pt,a4paper]{article}\usepackage{booktabs}\begin{document} \section{What's available from pgfplots} Via \emph{pgfplotstableregressiona} and \emph{-b} from \emph{pgfplots} you have already calculated: \begin{eqnarray} b &=& \frac{\sum(x_i-x_m)(y_i-y_m)}{\sum(x_i-x_m)^2} = \frac{L}{r}\\ y_m &=& a + b \times x_m \end{eqnarray} Via key \emph{variance=\{$<column\ name>$\}} you can calculate both \begin{eqnarray} r &=& \sum(x_i-x_m)^2\\ S_t &=&\sum(y_i-y_m)^2 \end{eqnarray} \section{Where you need to go} \begin{eqnarray} R^2 &=& \frac{(\sum(x_i-x_m)(y_i-y_m))^2}{(\sum(x_i-x_m)^2)(\sum(y_i-y_m)^2)}\\&=& \frac{L^2}{r \times S_t} \end{eqnarray} \section{How to go there} \begin{tabular}{ll} \toprule Calculate&from/via in pgfplots\\ \midrule $b$ & pgfplotstableregressionb\\ $r = Var(x)$ & variance\\ $S_t = Var(y)$ & variance\\ \midrule $L = b \times r$& (math)\\ \midrule $R^2 = \frac{L^2}{r \times S_t}$& (math)\\ \bottomrule \end{tabular}\end{document}
P.S.2: Though the recipe remains valid, the term variance
is used in a misleading way in the pgfmanual. It does not calculate for a data column, but assumes to be just some data given with each point ...