<p>This is post #3 on the subject of linear regression, using R for computational demonstrations and examples. We cover here residuals (or prediction errors) and the RMSE of the prediction line.
The first post in the series is <a href="https://rbertolusso.github.io/posts/LR01-correlation">LR01: Correlation</a>.</p>
<hr><ul><li>Acknowledgments: organization is extracted from:
<ul><li>Freedman, Pisani, Purves, Statistics, 4th ed., probably
the best book on statistical thinking (it maybe has a total of 4-5 formulas). It is referred here as FPP.</li>
<li>A lot of what is good is due to Professor Rudy Guerra.</li>
</ul></li>
</ul><hr><h3>Previous</h3>
<p><a href="https://rbertolusso.github.io/posts/LR01-correlation">LR01: Correlation</a></p>
<p><a href="https://rbertolusso.github.io/posts/LR02-SDline-GoA-regression">LR02: SD line, GoA, Regression</a></p>
<h3>What we know so far</h3>
<ul><li>Correlation coefficient: measure of <strong>linear</strong> association,
or clustering about the SD line.</li>
</ul><ul><li>If the scatterplot of both variables is a football-shaped cloud of points,
those points cluster about the SD line, and the relationship between both variables
can be summarized by:
<ul><li>average of x-values, SD of x-values ($s_x$),</li>
<li>average of y-values, SD of y-values ($s_y$).</li>
<li>
<p>the correlation coefficient r.</p>
</li>
<li>Reminder: we are (still) not making any probabilistic assumption, so we are not treating
any variable as random, so everything stays in lower case. We are only saying (for now)
that the correlation coefficient makes sense, as a summary “descriptive” statistic,
for football-shaped
cloud of points (even non-random ones), and it makes progressively less sense the more we
depart from a football-shaped cloud of points. Following this reasoning, we could be using
standard deviation version FPP (dividing by $n$), or $s$ (dividing by $n-1$) because
we are not using them to estimate $\sigma$ (the population standard deviation) as that
would imply making probabilistic assumptions (by the way, both version of the standard
deviations, even with the right probabilistic assumptions, are biased estimators of
the population standard deviation).</li>
</ul></li>
</ul><p></p>
<ul><li>Graph of Averages (GoA): discrete function defined by $\text{Ave}(y|x)$, depending on
the size of the $x$-intervals chosen (as happens with histograms):</li>
</ul><p></p>
<p>The GoA should always work, also for non football-shaped cloud of points.</p>
<ul><li>
<p>The regression (or regression function), linear or non-linear, of $y$
(dependent variable) on $x$
(independent variable) is a continous function that is a “smoother” of the GoA
(that is a “discrete” function). It always work (but we may not know its
mathematical form). The regression is the continous version of $\text{Ave}(y|x)$
(GoA is the discrete version). If we are making probabilistic assumptions
(by considering $Y$ a random variable with given characteristics that we will
see later), we say that the regression function
is the conditional expectaton of $Y$ given $X=x$ (or, in mathematical notation,
$\text{E}(Y|X=x)$).</p>
</li>
<li>
<p>The regression line is a smoothed version of the GoA that is correct only
when the GoA is linear.</p>
</li>
<li>
<p>When the GoA is linear, a best fit regression line, that passes through the
point of averages $(\bar{x}, \bar{y})$, is found as:</p>
</li>
</ul><p>where the slope $b$ is:</p>
<p>and the intercept $a$ is:</p>
<p></p>
<ul><li>The regression line is an attenuated version of the SD line (that has slope
= $\frac{s_y}{s_x}$)</li>
</ul><p></p>
<ul><li>
<p>T or F: Regression is the line $y = a + bx$.</p>
</li>
<li>
<p>T or F: The SD line is the regression line.</p>
</li>
</ul><h3>Residuals, or Prediction errors</h3>
<ul><li>
<p>The regression method can be used to predict (guess) y from x
(or x from y…). However, do you expect actual values to satisfy
the predictions (guesses)?</p>
</li>
<li>
<p>They will differ, but, by how much? (What do you think?)</p>
</li>
<li>
<p>We will answer this in a little bit: we first need to consider
the prediction errors, or residuals.</p>
</li>
</ul><p>The distance of a point above (+) or below (-) the regression line is:</p>
<p>error = actual - predicted.</p>
<ul><li>In the Pearson’s data (which was the name of the red line plotted?), showing
only distances for a subset of points, it looks like:</li>
</ul><p></p>
<p>The prediction error is usually called residual. For consistency with
Sheather’s book, we will name it $\hat{e}_i$ (you can also find it as
$\hat{\epsilon}_i$, $r_i$, and perhaps other variants),
the predicted value (or fitted value) is denoted by $\hat{y}_i$.
The actual value is the y-coordinate of the point, $y_i$.</p>
<p>(Note: we are still not making any probabilistic assumptions, but hats are
used to denote estimators, that are random variables. Even if usually random
variables are denoted with upper case, residuals, or prediction errors, are usually
denoted with lower case (the lower case also include errors $e_i$).
When making probabilistic assumptions,
$\hat{y}_i$ will become $\hat{Y}_i$.
Sorry about the abuse of notation.)</p>
<p>Mathematically:</p>
<p>where</p>
<div><pre><code><span>## Predicted points
</span><span>yhat</span><span> </span><span>&lt;-</span><span> </span><span>meany</span><span> </span><span>-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span> </span><span>+</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>x</span><span>
</span><span>## Prediction errors (residuals): actual - predicted
</span><span>ehat</span><span> </span><span>&lt;-</span><span> </span><span>y</span><span> </span><span>-</span><span> </span><span>yhat</span><span>
</span><span>hist</span><span>(</span><span>ehat</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>&quot;Scott&quot;</span><span>,</span><span> </span><span>probability</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<ul><li>The smaller the distance of each point to the line, the better
the fit to the regression line.</li>
</ul><h3>The RMSE of the prediction line</h3>
<p>RMSE stands for Root Mean Square Error. This implies:</p>
<ol><li>squaring the errors, or residuals:</li>
</ol><ol><li>Finding their mean:</li>
</ol><ol><li>Taking square root of the resulting mean:</li>
</ol><p>Using R:</p>
<div><pre><code><span>(</span><span>RMSE</span><span> </span><span>&lt;-</span><span> </span><span>sqrt</span><span>(</span><span>mean</span><span>(</span><span>ehat</span><span>^</span><span>2</span><span>)))</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.434294
</code></pre>
</div>
<ul><li>According to FPP, “the root mean square error for regression says how far typical
points are above or below the regression line. The RMSE is to the regression line
as the SD is to the average. For instance, if the scatter diagram is football-shaped,
about 68% of the points on the scatter diagram will be within one RMSE of the regression
line, about 95% of then will be within 2 RMSE of the regression line”.</li>
</ul><p></p>
<div><pre><code><span>## Proportion of values contained between 1 RMSE
</span><span>print</span><span>(</span><span>mean</span><span>(</span><span>abs</span><span>(</span><span>ehat</span><span>)</span><span> </span><span>&lt;</span><span> </span><span>RMSE</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 0.7133581
</code></pre>
</div>
<div><pre><code><span>## Proportion of values contained between 2 RMSEs
</span><span>print</span><span>(</span><span>mean</span><span>(</span><span>abs</span><span>(</span><span>ehat</span><span>)</span><span> </span><span>&lt;</span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>RMSE</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 0.9573284
</code></pre>
</div>
<ul><li>
<p>Pretty good, no?</p>
</li>
<li>
<p>FPP also present a relation between RMSE and SDy (the one calculated by dividing by $n$)</p>
</li>
</ul><p>Let’s get it with R:</p>
<div><pre><code><span>## FPP version of sample standard deviation
</span><span>SD</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>sqrt</span><span>(</span><span>mean</span><span>((</span><span>x</span><span>-</span><span>mean</span><span>(</span><span>x</span><span>))</span><span>^</span><span>2</span><span>))</span><span>
</span><span>(</span><span>RMSE2</span><span> </span><span>&lt;-</span><span> </span><span>sqrt</span><span>(</span><span>1</span><span>-</span><span>r</span><span>^</span><span>2</span><span>)</span><span>*</span><span>SD</span><span>(</span><span>y</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.434294
</code></pre>
</div>
<div><pre><code><span>RMSE</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.434294
</code></pre>
</div>
<ul><li>The interpretation of RMSE is that it represents the typical size of the residuals.</li>
</ul><h3>Baby steps towards probabilistic assumptions</h3>
<ul><li>When the cloud of points is football-shaped the distribution of $y$-values
inside a strip is <strong>Normal</strong> with mean approximately equal to $\text{Ave}(Y|X=x)$
and standard deviation approximately equal to RMSE.</li>
</ul><p></p>
<p></p>
<div><pre><code><span>mean</span><span>(</span><span>ystrip66</span><span>)</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 67.65625
</code></pre>
</div>
<div><pre><code><span>sd</span><span>(</span><span>ystrip66</span><span>)</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.350964
</code></pre>
</div>
<div><pre><code><span>mean</span><span>(</span><span>ystrip70</span><span>)</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 69.76845
</code></pre>
</div>
<div><pre><code><span>sd</span><span>(</span><span>ystrip70</span><span>)</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.48953
</code></pre>
</div>
<ul><li>
<p>If we only know about $y$, and not $x$, what do we use as an
approximation to the spread (variation) of $y$?</p>
</li>
<li>
<p>If we also know about $x$, what do we use as an approximation
to the spread of $y$ in a strip?</p>
</li>
<li>
<p>How is RMSE in relation to SDy? (smaller, equal, bigger)?</p>
</li>
</ul><p>In the case of sample data, as happened with SD, RMSE is not used nowadays because it is biased
if used as an estimator of the standard deviation $\sigma$ of the residuals $e_i$ of the population
(at some point we will calculate which is the bias of its square as an estimator
of the variance $\sigma^2$ of $e_i$). Of course, this has importance only when we
are dealing with samples and making probabilistic assumptions.</p>
<p>The version that is used, called Residual standard error,
is also a biased estimator
of $\sigma$, but its square (called Mean Square Error of the residuals
and indicated by $\text{MS}_\text{res}$), is an unbiased estimator of the variance $\sigma^2$ of $e_i$.</p>
<p>To start getting used to the functions <code>lm</code>, <code>summary</code> and <code>anova</code> that we will use heavily for simple and multiple regression (we will learn more about them some posts from now), let’s see
where these values can be found.</p>
<div><pre><code><span>fit</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>x</span><span>)</span><span>
</span><span>(</span><span>sfit</span><span> </span><span>&lt;-</span><span> </span><span>summary</span><span>(</span><span>fit</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8772 -1.5144 -0.0079  1.6285  8.9685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.88660    1.83235   18.49   &lt;2e-16 ***
## x            0.51409    0.02705   19.01   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.437 on 1076 degrees of freedom
## Multiple R-squared:  0.2513,	Adjusted R-squared:  0.2506 
## F-statistic: 361.2 on 1 and 1076 DF,  p-value: &lt; 2.2e-16
</code></pre>
</div>
<p>The Residual standard error is easily spotted at the beginning of the third line starting
from the bottom. It is rounded to three decimals. The actual value with more decimals (still rounded)
is:</p>
<div><pre><code><span>sfit</span><span>$</span><span>sigma</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.436556
</code></pre>
</div>
<p>that is slightly different than:</p>
<div><pre><code><span>RMSE</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.434294
</code></pre>
</div>
<ul><li>How to “correct” RMSE to agree with Residual standard error?</li>
</ul><div><pre><code><span>RMSE</span><span> </span><span>*</span><span> </span><span>sqrt</span><span>(</span><span>n</span><span>/</span><span>(</span><span>n</span><span> </span><span>-</span><span> </span><span>2</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 2.436556
</code></pre>
</div>
<ul><li>
<p>Now we are in peace. But… why $n - 2$ ???</p>
</li>
<li>
<p>We said that the Residual standard error is a (biased) estimator of $\sigma$. Its
square, $\text{MS}_\text{res}$, is an unbiased estimator of $\sigma^2$.</p>
</li>
</ul><div><pre><code><span>sfit</span><span>$</span><span>sigma</span><span>^</span><span>2</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 5.936804
</code></pre>
</div>
<p>Its value, using <code>anova</code>:</p>
<div><pre><code><span>(</span><span>afit</span><span> </span><span>&lt;-</span><span> </span><span>anova</span><span>(</span><span>fit</span><span>))</span><span>
</span></code></pre>
</div>
<div><pre><code>## Analysis of Variance Table
## 
## Response: y
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## x            1 2144.6 2144.58  361.23 &lt; 2.2e-16 ***
## Residuals 1076 6388.0    5.94                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
</code></pre>
</div>
<p>is found on the intersection of the row Residuals and the column Mean Sq. The less-rounded value is:</p>
<div><pre><code><span>afit</span><span>$</span><span>&quot;Mean Sq&quot;</span><span>[</span><span>2</span><span>]</span><span>
</span></code></pre>
</div>
<div><pre><code>## [1] 5.936804
</code></pre>
</div>
<h3>Appendix</h3>
<h4>Code to load data and initial calculations</h4>
<div><pre><code><span>library</span><span>(</span><span>UsingR</span><span>)</span><span>
</span><span>## Pearson's data
## Father and son data
</span><span>data</span><span>(</span><span>father.son</span><span>)</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>father.son</span><span>$</span><span>fheight</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>father.son</span><span>$</span><span>sheight</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>length</span><span>(</span><span>x</span><span>)</span><span>
</span><span>## Summary statistics
</span><span>meanx</span><span> </span><span>&lt;-</span><span> </span><span>mean</span><span>(</span><span>x</span><span>)</span><span>
</span><span>meany</span><span> </span><span>&lt;-</span><span> </span><span>mean</span><span>(</span><span>y</span><span>)</span><span>
</span><span>r</span><span> </span><span>&lt;-</span><span> </span><span>cor</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>)</span><span>
</span></code></pre>
</div>
<h4>Code to produce the SD line plot</h4>
<div><pre><code><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Pearson's data. SD line&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Spread of the cloud
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>+</span><span>c</span><span>(</span><span>-1</span><span>,</span><span>1</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>+</span><span>c</span><span>(</span><span>-1</span><span>,</span><span>1</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>+</span><span>c</span><span>(</span><span>-2</span><span>,</span><span>2</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>+</span><span>c</span><span>(</span><span>-2</span><span>,</span><span>2</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>+</span><span>c</span><span>(</span><span>-3</span><span>,</span><span>3</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>+</span><span>c</span><span>(</span><span>-3</span><span>,</span><span>3</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>## Point of averages (center of the cloud)
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>## SD line using equation and sd
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>meany</span><span> </span><span>-</span><span> </span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span>,</span><span>
</span><span>b</span><span> </span><span>=</span><span> </span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span></code></pre>
</div>
<h4>Code to produce the Graph of averages plot</h4>
<div><pre><code><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;lightgrey&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Pearson's data. GoA&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Point of averages (center of the cloud)
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>72</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>64</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span>
</span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>## Graph of averages.
</span><span>sgav</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>tapply</span><span>(</span><span>sheight</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>),</span><span> </span><span>mean</span><span>))</span><span>
</span><span>## sgavnum &lt;- with(father.son, tapply(sheight, round(fheight,0), length))
</span><span>points</span><span>(</span><span>as.numeric</span><span>(</span><span>names</span><span>(</span><span>sgav</span><span>)),</span><span> </span><span>sgav</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>)</span><span>
</span><span>## text(as.numeric(names(sgav)), sgav, sgavnum, pos=3)
</span></code></pre>
</div>
<h4>Code to produce the Graph of averages + regression line plot</h4>
<div><pre><code><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;lightgrey&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Pearson's data. GoA and regression line&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Point of averages (center of the cloud)
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>72</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>64</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span>
</span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>## Graph of averages.
</span><span>sgav</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>tapply</span><span>(</span><span>sheight</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>),</span><span> </span><span>mean</span><span>))</span><span>
</span><span>## sgavnum &lt;- with(father.son, tapply(sheight, round(fheight,0), length))
</span><span>points</span><span>(</span><span>as.numeric</span><span>(</span><span>names</span><span>(</span><span>sgav</span><span>)),</span><span> </span><span>sgav</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>)</span><span>
</span><span>## text(as.numeric(names(sgav)), sgav, sgavnum, pos=3)
</span><span>
</span><span>## Regression line
</span><span>abline</span><span>(</span><span>a</span><span>=</span><span>meany</span><span>-</span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span>,</span><span> </span><span>b</span><span>=</span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>lwd</span><span>=</span><span>4</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h4>Code to produce the Residual plot</h4>
<div><pre><code><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;lightgrey&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Prediction errors (or Residuals)&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Regression line
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>meany</span><span> </span><span>-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>),</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>## for (fn in sample(nrow(father.son), 20)) {
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>c</span><span>(</span><span>39</span><span>,</span><span> </span><span>158</span><span>,</span><span> </span><span>204</span><span>,</span><span> </span><span>479</span><span>,</span><span> </span><span>686</span><span>,</span><span> </span><span>808</span><span>,</span><span> </span><span>844</span><span>,</span><span> </span><span>851</span><span>,</span><span> </span><span>852</span><span>,</span><span> </span><span>1070</span><span>))</span><span> </span><span>{</span><span>
</span><span>## Actual point
</span><span> </span><span>points</span><span>(</span><span>x</span><span>[</span><span>i</span><span>],</span><span> </span><span>y</span><span>[</span><span>i</span><span>])</span><span>
</span><span>## Predicted point yhat_i
</span><span> </span><span>yhat_i</span><span> </span><span>&lt;-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>x</span><span>[</span><span>i</span><span>]</span><span> </span><span>+</span><span> </span><span>meany</span><span>-</span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span>
</span><span>points</span><span>(</span><span>x</span><span>[</span><span>i</span><span>],</span><span> </span><span>yhat_i</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>lines</span><span>(</span><span>rep</span><span>(</span><span>x</span><span>[</span><span>i</span><span>],</span><span> </span><span>2</span><span>),</span><span> </span><span>c</span><span>(</span><span>y</span><span>[</span><span>i</span><span>],</span><span> </span><span>yhat_i</span><span>))</span><span>
</span><span>## Prediction error (or residual): actual - predicted
</span><span> </span><span>ehat_i</span><span> </span><span>&lt;-</span><span> </span><span>y</span><span>[</span><span>i</span><span>]</span><span> </span><span>-</span><span> </span><span>yhat_i</span><span>
</span><span>text</span><span>(</span><span>x</span><span>[</span><span>i</span><span>],</span><span> </span><span>(</span><span>y</span><span>[</span><span>i</span><span>]</span><span> </span><span>+</span><span> </span><span>yhat_i</span><span>)</span><span>/</span><span>2</span><span>,</span><span> </span><span>round</span><span>(</span><span>ehat_i</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>pos</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<h4>Code to produce the histogram of the residuals</h4>
<div><pre><code><span>## Predicted points
</span><span>yhat</span><span> </span><span>&lt;-</span><span> </span><span>meany</span><span> </span><span>-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span> </span><span>+</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>x</span><span>
</span><span>## Prediction errors (residuals): actual - predicted
</span><span>ehat</span><span> </span><span>&lt;-</span><span> </span><span>y</span><span> </span><span>-</span><span> </span><span>yhat</span><span>
</span><span>hist</span><span>(</span><span>ehat</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>&quot;Scott&quot;</span><span>,</span><span> </span><span>probability</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<h4>Code to produce the RMSE bands</h4>
<div><pre><code><span>## RMS error for regression
</span><span>
</span><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;lightgrey&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Regression line and 1 and 2 RMSE bands&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Regression line
</span><span>a</span><span> </span><span>&lt;-</span><span> </span><span>meany</span><span> </span><span>-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>meanx</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>r</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>a</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>b</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>a</span><span> </span><span>-</span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>RMSE</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>b</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>a</span><span> </span><span>-</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>RMSE</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>b</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>a</span><span> </span><span>+</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>RMSE</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>b</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>a</span><span> </span><span>=</span><span> </span><span>a</span><span> </span><span>+</span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>RMSE</span><span>,</span><span> </span><span>b</span><span> </span><span>=</span><span> </span><span>b</span><span>,</span><span> </span><span>lwd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h4>Code to produce the Graph of averages with strips of points</h4>
<div><pre><code><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>),</span><span> </span><span>col</span><span>=</span><span>&quot;lightgrey&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>xaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span> </span><span>yaxs</span><span> </span><span>=</span><span> </span><span>&quot;i&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Pearson's data. GoA and regression line&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Father height in inches&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Son height in inches&quot;</span><span>)</span><span>
</span><span>axp</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>58</span><span>,</span><span> </span><span>80</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>2</span><span>,</span><span> </span><span>at</span><span> </span><span>=</span><span> </span><span>axp</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>axp</span><span>)</span><span>
</span><span>## Point of averages (center of the cloud)
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>meanx</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>abline</span><span>(</span><span>h</span><span>=</span><span>meany</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;green&quot;</span><span>)</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>66</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>70</span><span>
</span><span>abline</span><span>(</span><span>v</span><span>=</span><span>fround</span><span>+</span><span>c</span><span>(</span><span>-.5</span><span>,</span><span>.5</span><span>),</span><span> </span><span>lty</span><span>=</span><span>3</span><span>)</span><span>
</span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>points</span><span>(</span><span>fheight</span><span>,</span><span> </span><span>sheight</span><span>,</span><span>
</span><span>pch</span><span>=</span><span>16</span><span>,</span><span> </span><span>col</span><span>=</span><span>ifelse</span><span>(</span><span>sheight</span><span> </span><span>></span><span> </span><span>meany</span><span> </span><span>+</span><span> </span><span>(</span><span>fheight</span><span>-</span><span>meanx</span><span>)</span><span>/</span><span>sd</span><span>(</span><span>x</span><span>)</span><span>*</span><span>sd</span><span>(</span><span>y</span><span>)</span><span>*</span><span>r</span><span>,</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>)))</span><span>
</span><span>## Graph of averages.
</span><span>sgav</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>tapply</span><span>(</span><span>sheight</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>),</span><span> </span><span>mean</span><span>))</span><span>
</span><span>## sgavnum &lt;- with(father.son, tapply(sheight, round(fheight,0), length))
</span><span>points</span><span>(</span><span>as.numeric</span><span>(</span><span>names</span><span>(</span><span>sgav</span><span>)),</span><span> </span><span>sgav</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>,</span><span> </span><span>pch</span><span>=</span><span>16</span><span>)</span><span>
</span><span>## text(as.numeric(names(sgav)), sgav, sgavnum, pos=3)
</span></code></pre>
</div>
<h4>Code to produce the histograms of the strips</h4>
<div><pre><code><span>par</span><span>(</span><span>mfrow</span><span>=</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>))</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>66</span><span>
</span><span>ystrip66</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>sheight</span><span>)</span><span>
</span><span>hist</span><span>(</span><span>ystrip66</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>7</span><span>)</span><span>
</span><span>fround</span><span> </span><span>&lt;-</span><span> </span><span>70</span><span>
</span><span>ystrip70</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>subset</span><span>(</span><span>father.son</span><span>,</span><span> </span><span>round</span><span>(</span><span>fheight</span><span>,</span><span>0</span><span>)</span><span> </span><span>==</span><span> </span><span>fround</span><span>),</span><span>
</span><span>sheight</span><span>)</span><span>
</span><span>hist</span><span>(</span><span>ystrip70</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>7</span><span>)</span><span>
</span></code></pre>
</div>
<h3>Previous</h3>
<p><a href="https://rbertolusso.github.io/posts/LR01-correlation">LR01: Correlation</a></p>
<p><a href="https://rbertolusso.github.io/posts/LR02-SDline-GoA-regression">LR02: SD line, GoA, Regression</a></p>
<h3>Related</h3>
<p><a href="https://rbertolusso.github.io/posts/intubate-and-stat-functions-in-pipelines">intubate &lt;||> R stat functions in data science pipelines</a></p>