Quantcast
Channel: How to calculate correlation from a table of data - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by Marijn for How to calculate correlation from a table of data

$
0
0

The linked example works with a few modifications.

First, the use of the default variable \tableLabel in the original code is a bit confusing, I have removed that. This means that you need to provide the macro that stores the table (in your case \tableWisFysTrendlijn) as the first of seven arguments.

Second, you read the table inside of \begin{center}, which means that the variable \tableWisFysTrendlijn was defined only in that scope. Therefore it could not be used outside of the center environment for calculation or printing of the correlation value. Moving \pgfplotstableread outside of the environment fixes that.

\documentclass{article}\usepackage{tikz}\usepackage{pgfplotstable}\newcommand\MakeRegression[7]{%    % #1: original datatable    % #2: x-column    % #3: y-column    % #4: name of new regression column    % #5: macro to store slope in    % #6: macro to store intercept in    % #7: macro to store R squared    % create the regression column:    \pgfplotstablecreatecol[linear regression={x=#2,y=#3}]{#4}{#1}    % store slope and intercept    \edef#5{\pgfplotstableregressiona}    \edef#6{\pgfplotstableregressionb}    % calculate sum of y    \pgfplotstablecreatecol[create col/expr={\pgfmathaccuma+\thisrow{#3}}]{tmp}{#1}    % find number of rows in table    \pgfplotstablegetrowsof{#1}    \pgfmathtruncatemacro{\lastrow}{\pgfplotsretval-1}    % get total sum of y    \pgfplotstablegetelem{\lastrow}{tmp}\of#1    % calculate mean    \pgfmathsetmacro{\yMean}{\pgfplotsretval/(\lastrow+1)}    % calculate residuals and diff from mean    \pgfplotstablecreatecol[      create col/assign/.code={           \pgfmathparse{(\thisrow{#3}-\thisrow{#4})^2}           \edef\entry{\pgfmathresult}           \pgfkeyslet{/pgfplots/table/create col/next content}\entry        }    ]{residuals}{#1}    \pgfplotstablecreatecol[      create col/assign/.code={           \pgfmathparse{(\thisrow{#3}-\yMean)^2}           \edef\entry{\pgfmathresult}           \pgfkeyslet{/pgfplots/table/create col/next content}\entry        }    ]{diffmean}{#1}    % calculate sum of residuals and diff from mean    \pgfplotstablecreatecol[create col/expr={\pgfmathaccuma+\thisrow{residuals}}]{sumres}{#1}    \pgfplotstablecreatecol[create col/expr={\pgfmathaccuma+\thisrow{diffmean}}]{sumdiff}{#1}    % extract SS_res and SS_tot    \pgfplotstablegetelem{\lastrow}{sumres}\of#1    \pgfmathsetmacro{\SSres}{\pgfplotsretval}    \pgfplotstablegetelem{\lastrow}{sumdiff}\of#1    \pgfmathsetmacro{\SStot}{\pgfplotsretval}    % calculate r    \pgfmathsetmacro{#7}{sqrt(1-\SSres/\SStot)}}\begin{document}\pgfplotstableread{    x   y       70.6    68    66.2    66.3    73.9    74.1    68.2    71.3    64.2    56.7    69.8    69.6    60.3    62.4    56.8    57.6    55  55.1    67  65.2    57.9    55.7    57.9    59.6    53.1    55.3    61.2    59    66.2    66.6    56.9    52.3    58  57.3    78.5    79.9    63.5    62.5    51  54.1    62.4    67.5    50.5    50.2    61.7    62.4    70.5    73.5}\tableWisFysTrendlijn\begin{center}\begin{tikzpicture}\begin{axis}[title=title,    axis lines=center,    axis x discontinuity=crunch,    axis y discontinuity=crunch,    ymin=30, ymax=100,    xmin=30, xmax=100,    height=10cm,    width=\linewidth,    ylabel near ticks,    xlabel near ticks,xlabel={A},ylabel={B},    scatter/classes={a={mark=*,draw=black}}]\addplot[only marks,  mark size = 1pt] table{\tableWisFysTrendlijn};\addplot[no marks,red, thick] table[y={create col/linear regression={y=y}}] {\tableWisFysTrendlijn};\legend{leerling, $y= \pgfmathprintnumber{\pgfplotstableregressiona} \cdot x    \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}\end{axis}\end{tikzpicture}\end{center}The equation is $y= \pgfmathprintnumber{\pgfplotstableregressiona} \cdot x  \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$\MakeRegression{\tableWisFysTrendlijn}{x}{y}{reg1}{\SlA}{\IntA}{\RsqA}The correlation is: \RsqA\end{document}

enter image description here


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>