multivariate-statistics/figures/schemes/ordinary_least_squares.tex

45 lines
1.1 KiB
TeX
Raw Permalink Normal View History

2023-11-16 16:47:14 +01:00
\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usepackage{luacode}
\begin{document}
\begin{tikzpicture}
% Draw axes
\draw[->] (0,0) -- (5,0);
\draw[->] (0,0) -- (0,5);
\directlua{
function runif(min, max)
return min + (max - min) * math.random()
end
math.randomseed(42)
x_min = 0
x_max = 5
error_min = -1
error_max = 1
beta0 = 2
beta1 = 1/5
x_values = {}
y_values = {}
for i=1,42 do
x = runif(x_min, x_max)
epsilon = runif(error_min, error_max)
y_hat = beta0 + beta1 * x
y = y_hat + epsilon
tex.print("\\draw[-,very thin, lightgray] ("..x..","..y_hat..") -- ("..x..","..y..") ;")
x_values[i] = x
y_values[i] = y
end
for i=1,42 do
x = x_values[i]
y = y_values[i]
tex.print("\\node[black] at ("..x..","..y..") {.};")
end
}
% Draw least square line
\draw[-,blue,thick] (0,2) -- (5,\directlua{tex.print(5*beta1+beta0)});
% Draw square norm
\end{tikzpicture}
\end{document}