# Linear regression on modified datas

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};
$\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

• ,@zeslayer See Sec 3.3.4 Add a Selected Fit-line, page 27 of the PGFPLOTS manual for how to include a regression plot. Commented Jul 28, 2015 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 ? Commented Jul 28, 2015 at 16:13

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}

\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}
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}