![如何绘制线性回归的一般形式](https://linux22.com/image/427099/%E5%A6%82%E4%BD%95%E7%BB%98%E5%88%B6%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E7%9A%84%E4%B8%80%E8%88%AC%E5%BD%A2%E5%BC%8F.png)
答案1
你可以尝试这样的事情,用 pgfplots:
\documentclass[border=2mm]{standalone}
\usepackage {pgfplots}
\pgfplotsset{compat=1.17}
\def\func(#1){0.75*#1+0.58} % Linear regression function
\pgfmathsetmacro\y{\func(2)} % \alpha x_1 + \beta
\begin{document}
\begin{tikzpicture}[line cap=round, mark options={mark size=1.5pt,fill=white}]
\begin{axis}
[
xmin=0,
xmax=4,
ymin=0,
ymax=4,
domain=0.25:3.75,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
xtick={2},
xticklabels={$x_1$},
ytick=\empty
]
\addplot [thick, blue] {(\func(x))};
\addplot [dashed, red] coordinates {(2,0) (2,2.44)};
\addplot [mark=*, red] coordinates {(2,\y)};
\addplot [only marks] coordinates {(0.5,0.80) (1,1.57) (1.5,1.34)
(2,2.44) (2.5,2.48) (3,2.66)
(3.5,3.17)};
\node at (2,2.44) [left] {$y_1$};
\node[red] at (2,\y) [right] {$\alpha x_1+\beta$};
\node[blue] at (2.6,3.1) {$y=\alpha x+\beta$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
虽然很晚了,但这是 TIKZ 唯一的尝试:
\documentclass[tikz,border=3.5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{plotmarks}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\pgfsetplotmarksize{0.4ex}
\draw[->] (0,0) -- (4,0) node[xshift=0.25cm]{$x$};
\draw[->] (0,0) -- (0,4) node[xshift=0.25cm]{$y$};
\draw[-,blue] (0.5,0.5) -- (3.5,3.5);
\foreach \b in {1,1.5,...,3}{
\node[black] at (\b+rnd-rnd,\b+rnd-rnd){\pgfuseplotmark{o}};
\b
}
\node[inner sep=0,black, label={[below, yshift=-0.2cm]$x_1$}] (n1)at (2,0){};
\node[inner sep=0,black, label={[above]$y_1$}](n2) at (2,2.5){\pgfuseplotmark{o}};
\path[draw,dashed,red] (n1)--(n2);
\node[red,fill=white,inner sep=0.35ex, label={[right,xshift=0.2cm]$\alpha x_1+\beta$}] at (2,2){\pgfuseplotmark{o}};
\node[inner sep=0,blue] (n1)at (1.5,3.5){$y=\alpha x_1+\beta$};
\end{tikzpicture}
\end{document}