1

I would like to create a linear regression on modified data.

My raw data is on a file called data.dat like that :

x,y
1,2
2,3
3,4
4,5
5,8
6,9
7,34

If I do something like the code below, I get the linear regression for the not modified data.

\documentclass{beamer}


\usepackage{etex}
\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{epstopdf}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{pstricks}

\usepackage{graphicx}


\pgfplotsset{legend style={fill=none, font=\tiny, legend pos=north east}}


\begin{document}


\begin{frame}

\setbeamercovered{invisible}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={x},
        ylabel={y},
        width=0.8\paperwidth, height=0.8\paperheight,
        ymin=0,
        xmin=0,
        xmajorgrids, xminorgrids, ymajorgrids,
    ]   

    \addplot table [col sep=comma, y expr= \thisrow{y}/((7.75)), x expr = \thisrow{x}^(2)]{data.dat};
    \addlegendentry{Huile/Eau, débit nul};
    \addplot table[col sep=comma,y={create col/linear regression}]{data.dat};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}    


    \end{axis}  
\end{tikzpicture}
\end{frame}

\end{document}

It give me this instead of a linear regression of my plot

enter image description here

  • ,@zeslayer See Sec 3.3.4 Add a Selected Fit-line, page 27 of the PGFPLOTS manual for how to include a regression plot. – AboAmmar Jul 28 '15 at 15:47
  • On this page, they explain how to give weight to each point for the linear regression but not to fit to a modified curve no ? Did I miss something ? – zeslayer Jul 28 '15 at 16:13
1

Thanks to Gonzalo Medina in this post : plot and fit from pgfplottable, I did manage to do what I wanted.

The final code is :

\documentclass{beamer}


\usepackage{etex}
\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{epstopdf}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{pstricks}
\usepackage{array}

\usepackage{graphicx}

\usepackage{filecontents}


\begin{document}




\begin{frame}


\pgfplotstableread[col sep = comma]{data.dat}\loadedtable
\pgfplotstableset{
create on use/new/.style={
create col/expr={\thisrow{x}^(2)}},
create on use/new2/.style={
create col/expr ={\thisrow{y}/((7.75))}
}
}

\begin{tikzpicture}
\begin{axis}
\addplot table [x=new,y=new2] {\loadedtable};
\addlegendentry{test};
\addplot table[x=new,y={create col/linear regression={y=new2}}] {\loadedtable};
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}  
\end{axis}
\end{tikzpicture}

\end{frame}

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.