4

How can I get what is on the screenshot?

enter image description here

MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usepackage{lmodern}
\begin{document}
    \begin{tikzpicture}[font=,
        declare function={f(\x)=0.5*pow(abs(\x-2),2)-0.06*pow(\x-2,3);}]
        \foreach \Z in {1,...,42}
        {\pgfmathsetmacro{\X}{\Z/10}
            \pgfmathsetmacro{\Y}{f(\X)+0.9*rnd}
            \ifnum\Z=1
            \xdef\LstOne{(\X,\Y)}
            \xdef\LstTwo{"(\X,\Y)"}
            \else
            \xdef\LstOne{\LstOne (\X,\Y)}
            \xdef\LstTwo{\LstTwo,"(\X,\Y)"}
            \fi}
        %  \begin{scope}[local bounding box=over0]
        %  \foreach \Z in {1,...,42}
        %  {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
        %  \fill \Coor circle[radius=1pt];
        %  }
        %  \draw plot[smooth] coordinates \LstOne;
        %  \end{scope}
        \begin{scope}[local bounding box=over,xshift=-5cm]
            \foreach \Z in {1,...,40}
            {\pgfmathsetmacro{\Last}{{\LstTwo}[\Z-1]}
                \pgfmathsetmacro{\Current}{{\LstTwo}[\Z]}
                \pgfmathsetmacro{\Next}{{\LstTwo}[\Z+1]}
                %\typeout{\Last,\Current,\Next}
                \edef\temp{\noexpand\path ($0.6*\Current+0.2*\Last+0.2*\Next$)   coordinate 
                    (p\Z);}
                \temp
                \ifnum\Z=1
                \xdef\LstThree{(p\Z)}
                \else
                \xdef\LstThree{\LstThree (p\Z)}
                \fi
            }
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] plot[smooth] coordinates \LstThree;
        \end{scope}
        %
        \begin{scope}[local bounding box=good,xshift=-10cm]
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] plot[smooth,domain=0.1:4.2,variable=\x] (\x,{f(\x)+0.45});
        \end{scope}
        %
        \begin{scope}[local bounding box=under,xshift=-15cm]
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] (0.1,0.4) -- (4.2,2);
        \end{scope}
        %
        \foreach \X in {over,good,under}
        {\draw[gray,thin] ([xshift=-3pt,yshift=3pt]\X.north west) rectangle 
            ([xshift=3pt,yshift=-3pt]\X.south east);
            \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]\X.north west) node[right=1.5pt,fill=white]{Valores} 
            |- ([xshift=3pt,yshift=-3pt]\X.south east) node[below left]{Datos};}
    \end{tikzpicture}
\end{document}
Stephen
  • 956
  • 3
    I would suggest using the pgfplots package instead of drawing it in tikz. Also, does "grade" refer to the "degree" of the polynomial? Do you have a source which that defines that explicitly -- can't seem to locate a definition online via Google. – Peter Grill Dec 27 '20 at 04:02
  • Yes, grade does refer to degree! – Stephen Dec 27 '20 at 04:06
  • This reminds me of https://tex.stackexchange.com/questions/573127/tikz-plots-are-not-centered (content, not code). – John Kormylo Dec 27 '20 at 17:41

1 Answers1

2

I think that the easier way is to allow to R to make the fit and to knitr make the tikz graph.

A 3x1 example:

\documentclass{standalone}
\begin{document}
<<echo=F,dev='tikz', fig.height= 4, fig.width=4 >>=
set.seed(20)
x <- 1:60
y <- 50 - .001 * x^2 +  .001 * x^3
y <- y + rnorm(length(x), mean=30, sd=15)
plot(x,y, type="n",col="cyan",las=1, xlab="Datos",ylab="Valores",xaxs="i", yaxs="i")
grid(lwd=3)
points(x,y,pch=16)
model <- lm(y ~ poly(x,25))
predicted <- predict(model)
lines(x,predicted,col='tan4',lwd=5)
model <- lm(y ~ poly(x,7))
predicted <- predict(model)
lines(x,predicted,col='orange',lwd=5)
model <- lm(y ~ poly(x,3))
predicted <- predict(model)
lines(x,predicted,col='red',lwd=5)
@
\end{document}

mwe

Fran
  • 80,769
  • For some reason it does not compile in LaTeX :(. I am using TexStudio – Stephen Dec 27 '20 at 17:20
  • @Delan You should learn first about knitr. This is not (yet) a true .tex file but a .Rnw file (.or Rtex in Overleaf). You will need install R with knitr to convert this to a true tex file as explained here, or use RStudio (recommended) or Overleaf (on-line) that take care of all the process. – Fran Dec 27 '20 at 19:26