1

I've already tried different codes with tikzpicture but especially the normal distribution graphs are giving me headaches.

Any ideas?

Would appreciate any kind of help!

enter image description here

That is what I have got so far, but I have no clue how to integrate the normal distributions ...

\documentclass{article}
\usepackage{tikzsymbols}
\usepackage{pgfplots} 
\usepackage{pgfplotstable}

\pgfplotstableread{
X Y
0 2 
1 2.25
2 2.5
3 2.75
4 2
5 2.75
6 3
7 3.25
8 3.5
9 3.75
10 4
}\datatable

\begin{document}
\pgfplotsset{every axis legend/.append style={
        cells={anchor=west}}}

\begin{tikzpicture}
\begin{axis}[legend pos=north west,xmin=0,xmax=10,
ymin=0,ymax=5,enlargelimits=0.1]

\addplot[draw=none,color=red] table [
x=X,
y={create col/linear regression={y=Y}},
] {\datatable};
\xdef\slope{\pgfplotstableregressiona}
\xdef\offset{\pgfplotstableregressionb}
\addplot[no marks,color=red,domain=-2:9] {\slope*x+\offset};
\addlegendentry{$E(Y | x_i)$}
\coordinate (aux1) at (2,{\slope*2+\offset});
\coordinate (aux2) at (2,2.5);
\end{axis}
\end{tikzpicture}
\end{document}
  • 3
    Please show us the code you have so far! – Mensch Dec 2 '18 at 22:01
  • 1
    you can certainly provide code to draw everything else apart from the normal distribution curves, given the answer(s) to your previous question. so please do.. – Troy Dec 2 '18 at 22:07
2

Here is a proposal: define a pic for the Gaussian and place it wherever you want.

\documentclass{article}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}

\pgfplotstableread{
X Y
0 2 
1 2.25
2 2.5
3 2.75
4 2
5 2.75
6 3
7 3.25
8 3.5
9 3.75
10 4
}\datatable
\tikzset{pics/.cd,
Gaussian/.style={code={
\draw[gray] plot[variable=\x,domain=-\GaussDomain:\GaussDomain] ({\GaussHeight*exp(-2*\x*\x/\GaussWidth)},\x);
\draw[gray,dashed] (0,0) -- (\GaussHeight,0);
}}}
\pgfkeys{Gauss width/.store in=\GaussWidth,
Gauss width=0.5,
Gauss height/.store in=\GaussHeight,
Gauss height=0.8,
Gauss domain/.store in=\GaussDomain,
Gauss domain=1.2
}
\begin{document}
\pgfplotsset{every axis legend/.append style={
        cells={anchor=west}}}

\begin{tikzpicture}
\begin{axis}[legend pos=north west,xmin=0,xmax=10,
ymin=0,ymax=5,enlargelimits=0.1]

\addplot[draw=none,color=red] table [
x=X,
y={create col/linear regression={y=Y}},
] {\datatable};
\xdef\slope{\pgfplotstableregressiona}
\xdef\offset{\pgfplotstableregressionb}
\addplot[no marks,color=red,domain=-2:9] {\slope*x+\offset}
coordinate[pos=0.3](p1) coordinate[pos=0.7](p2);
\addlegendentry{$E(Y | x_i)$}
% \coordinate (aux1) at (2,{\slope*2+\offset});
% \coordinate (aux2) at (2,2.5);
\end{axis}
\path foreach \X in {1,2} {(p\X) pic{Gaussian}};
\end{tikzpicture}
\end{document}

You can adjust the width, height, domain or color of the Gaussian by setting the corresponding pgf keys accordingly.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.