# PGFPlots linear regression variance

I would like to compute a weighted linear regression of my data, but my data file lists the standard deviation of each point, not the variance.

Is there a way to have an equation in the variance to square my standard deviation? I will be reading my data in from a file.

I have seen the following in the PGFPlotsTable manual, but can't make it work in my instance. I've altered it to do what I want it to do.

\pgfplotstableset{
create on use/vari/.style={
create col/expr={U^2}}
}


My file:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}[baseline]
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
]
only marks,
error bars/.cd, y dir=both, y explicit,
]
table[
x = X,
y = Y,
y error = U,
]
{
X   Y   U
1   1   1
20  20  4.472
40  35  5.916
60  71  8.426
80  78  8.832
100 114 10.677
};

red,
]
table[
x = X,
y = {create col/linear regression={y=Y, variance=U}}, %this should be U^2
]
{
X   Y   U
1   1   1
20  20  4.472
40  35  5.916
60  71  8.426
80  78  8.832
100 114 10.677
};
\end{axis}
\end{tikzpicture}

\end{document}


You are very nearly there. All that's missing is to use \thisrow{U} instead of U in your create col/expr

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\pgfplotstabletypeset[ % just to show that this works
create on use/vari/.style={
create col/expr={\thisrow{U}^2}},
columns={U,vari}
]{
X   Y   U
1   1   1
20  20  4.472
40  35  5.916
60  71  8.426
80  78  8.832
100 114 10.677
}

\bigskip

\begin{tikzpicture}[baseline]
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
]
only marks,
error bars/.cd, y dir=both, y explicit,
]
table[
x = X,
y = Y,
y error = U,
]
{
X   Y   U
1   1   1
20  20  4.472
40  35  5.916
60  71  8.426
80  78  8.832
100 114 10.677
};

red,
]
table[
create on use/vari/.style={
create col/expr={\thisrow{U}^2}},
x = X,
y = {create col/linear regression={y=Y, variance=vari}}, %this should be U^2
]
{
X   Y   U
1   1   1
20  20  4.472
40  35  5.916
60  71  8.426
80  78  8.832
100 114 10.677
};
\end{axis}
\end{tikzpicture}

\end{document}

• Ta a lot! I did use that in a previous incarnation, but must have had something else wrong. – masher Jul 20 '18 at 6:44