Compilateur LaTeX en ligne
www.cpierquet.fr
fichiers/regressionlineaire.tex
↓ télécharger
\documentclass{article}
\usepackage{siunitx}
\usepackage{listofitems}
\usepackage{xfp}
\usepackage{tkz-tab}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{
	compat=newest,
	xlabel near ticks,
	ylabel near ticks
}

\newcommand\regressionlin[2]{
	%xmin et xmax
	\def\LXmin{\fpeval{min(#1)}}
	\def\LXmax{\fpeval{max(#1)}}
	%listes des données
	\def\xliste{#1}
	\def\yliste{#2}
	\readlist*\LX{\xliste}
	\readlist*\LY{\yliste}
	%taille des listes
	\def\LNB{\inteval{\LXlen}}
	
	%somme des LX et des LY OK
	\xdef\LXSomme{0}
	\xdef\LYSomme{0}
	\foreach \i in {1,2,...,\LNB}{
		\xdef\LXSomme{\fpeval{\LXSomme+\LX[\i]}}
	}
	\foreach \i in {1,2,...,\LNB}{
		\xdef\LYSomme{\fpeval{\LYSomme+\LY[\i]}}
	}
	%moyenne des LX et des LY OK
	\xdef\LXmoy{\fpeval{\LXSomme/\LNB}}
	\xdef\LYmoy{\fpeval{\LYSomme/\LNB}}
	
	%variance des LX et des LY OK
	\xdef\LXvar{0}
	\foreach \i in {1,2,...,\LNB}{
		\xdef\LXvar{\fpeval{\LXvar+(\LX[\i]-\LXmoy)^2}}
	}
	\xdef\LXvar{\fpeval{\LXvar/\LNB}}
	
	\xdef\LYvar{0}
	\foreach \i in {1,2,...,\LNB}{
		\xdef\LYvar{\fpeval{\LYvar+(\LY[\i]-\LYmoy)^2}}
	}
	\xdef\LYvar{\fpeval{\LYvar/\LNB}}
	
	%covariance des XY OK
	\xdef\LXYvar{0}
	\foreach \i in {1,2,...,\LNB}{
		\xdef\LXYvar{\fpeval{\LXYvar+(\LX[\i]-\LXmoy)*(\LY[\i]-\LYmoy)}}
	}
	\xdef\LXYvar{\fpeval{\LXYvar/\LNB}}
	
	%COEFF  OK
	\xdef\COEFFa{\fpeval{\LXYvar/\LXvar}}
	\xdef\COEFFb{\fpeval{\LYmoy-\COEFFa*\LXmoy}}
	\xdef\COEFFr{\fpeval{\LXYvar/sqrt(\LXvar*\LYvar)}}
	\xdef\COEFFrd{\fpeval{\COEFFr*\COEFFr}}
}

\begin{document}

\regressionlin{1994,1995,1996,1997,1998,1999,2000,2001,2002,2004,2005,2006,2007,2008,2009,2010}{1718,1710,1708,1700,1698,1697,1691,1688,1683,1679,1671,1670,1663,1661,1656,1649}

Liste des X := \showitems\LX.

Liste des Y := \showitems\LY.

Min des X := \LXmin{} et Max des X := \LXmax.

Somme des X := \LXSomme{} et somme des Y := \LYSomme.

Moyenne des X := \LXmoy{} et moyenne des Y := \LYmoy.

Variance des X := \LXvar{} et Variance des Y := \LYvar{}

Covariance des X/Y := \LXYvar.

Coefficient $a=\COEFFa$.

Coefficient $b=\COEFFb$.

Coefficient $r=\COEFFr$.

Coefficient $r^2=\COEFFrd$.
	
\begin{tikzpicture}
	\begin{axis}[
		/pgf/number format/.cd,
		use comma,
		xmin = 1992, xmax = 2012,
		ymin = 1640, ymax = 1740,
		width = 0.9\textwidth,
		height = 0.5\textwidth,
		xtick distance = 2,
		ytick distance = 10,
		grid = both,
		minor tick num = 1,
		major grid style = {lightgray},
		minor grid style = {lightgray!25},
		xlabel = {\small Année ($x$)},
		ylabel = {\small Altitude du glacier (en m) ($y$)},
		x tick label style={/pgf/number format/.cd, set thousands separator={}},
		y tick label style={/pgf/number format/.cd, set thousands separator={}},
		legend cell align = {left},
		legend pos = north east
		]
		\regressionlin{1994,1995,1996,1997,1998,1999,2000,2001,2002,2004,2005,2006,2007,2008,2009,2010}{1718,1710,1708,1700,1698,1697,1691,1688,1683,1679,1671,1670,1663,1661,1656,1649}
		\addplot[teal, only marks] table{
			X Y
			1994 1718
			1995 1710
			1996 1708
			1997 1700
			1998 1698
			1999 1697
			2000 1691
			2001 1688
			2002 1683
			2004 1679
			2005 1671
			2006 1670
			2007 1663
			2008 1661
			2009 1656
			2010 1649
		};
		%\addlegendentry{Données}
		\addplot [thick,orange,domain=\LXmin:\LXmax,samples=2]{\COEFFa*x+\COEFFb};
		\addlegendentry{$y = \fpeval{ceil(\COEFFa,3)}\,x + \fpeval{ceil(\COEFFb,3)}$};
		\addlegendentry{$R^2=\fpeval{ceil(\COEFFrd,5)}$};
	\end{axis}
\end{tikzpicture}

\end{document}