Linear Regression

Linear regression example.

MATLAB source

y = randn(100,1)  % random y values
n = numel(y)  % number of points
m = 1/n * 10 * [1:n]'  % x values
y = y + m  % add slope to y values
A = [m ones(n,1)]  % compose A matrix
x = inv(transpose(A)*A)*transpose(A)*y

LaTeX source code

\documentclass[11pt]{article}

% graphics
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{fillbetween}


\begin{document}
	\begin{tikzpicture}

		\pgfplotsset{
			scale only axis,
		}

		\begin{axis}[
			xmin=-.5,
			xmax=10.5,
			xlabel=$x$,
			ylabel=$y$,
			samples=100,
			]
\addplot [only marks,mark=x] table {
    0.1000    0.0416
    0.2000    2.7350
    0.3000    0.7386
    0.4000    0.8375
    0.5000   -0.3377
    0.6000   -0.7075
    0.7000    1.4941
    0.8000    0.6027
    0.9000    1.5492
    1.0000    0.1685
    1.1000    1.9960
    1.2000   -0.6135
    1.3000    2.8667
    1.4000    2.2465
    1.5000    1.6102
    1.6000    0.4389
    1.7000    1.3025
    1.8000    2.0543
    1.9000    3.1078
    2.0000    0.9665
    2.1000    3.3951
    2.2000    4.9681
    2.3000    1.8047
    2.4000    2.8688
    2.5000    1.8427
    2.6000    0.8830
    2.7000    4.1705
    2.8000    3.4941
    2.9000    2.3893
    3.0000    3.1134
    3.1000    2.8702
    3.2000    1.7383
    3.3000    0.4177
    3.4000    3.3525
    3.5000    3.0375
    3.6000    3.0234
    3.7000    2.8540
    3.8000    1.9828
    3.9000    3.3783
    4.0000    4.1614
    4.1000    3.0382
    4.2000    4.6505
    4.3000    4.0272
    4.4000    4.2985
    4.5000    3.0709
    4.6000    3.8356
    4.7000    5.1101
    4.8000    4.0101
    4.9000    5.0616
    5.0000    6.9779
    5.1000    5.8953
    5.2000    6.2374
    5.3000    7.6603
    5.4000    6.5754
    5.5000    5.8977
    5.6000    7.5512
    5.7000    4.7953
    5.8000    5.1178
    5.9000    7.9183
    6.0000    6.4543
    6.1000    6.0579
    6.2000    6.6582
    6.3000    6.0359
    6.4000    6.1080
    6.5000    6.1132
    6.6000    6.0460
    6.7000    5.7563
    6.8000    6.7402
    6.9000    6.3253
    7.0000    8.4419
    7.1000    7.3355
    7.2000    8.2214
    7.3000    7.1602
    7.4000    7.3184
    7.5000    8.7582
    7.6000    9.0685
    7.7000    7.7329
    7.8000    9.4925
    7.9000    8.3757
    8.0000    8.3685
    8.1000   10.2920
    8.2000    9.7357
    8.3000    9.2073
    8.4000    8.8023
    8.5000    7.9616
    8.6000    8.4848
    8.7000   10.5220
    8.8000    6.7785
    8.9000    8.2630
    9.0000    8.8112
    9.1000    9.2483
    9.2000    7.4161
    9.3000    9.8291
    9.4000   10.1297
    9.5000    8.5599
    9.6000    9.3407
    9.7000    8.4865
    9.8000   11.4245
    9.9000   10.2735
   10.0000    9.7491
};
\addplot[][domain=-1:11]{1.0225*x+0.0012};
		\end{axis}

	\end{tikzpicture}

\end{document}
(0 Posts)
Zürich-based Software Engineer with Google; opinions are my own. I am interested in data science, software engineering, 3d-printing, arts, music, microcontrollers, and sports.
View all author’s posts