<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linear Regression | Zhu Xuelin</title><link>https://linstats.github.io/tags/linear-regression/</link><atom:link href="https://linstats.github.io/tags/linear-regression/index.xml" rel="self" type="application/rss+xml"/><description>Linear Regression</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Sat, 12 Sep 2026 00:00:00 +0000</lastBuildDate><image><url>https://linstats.github.io/media/icon_hu15689388221013369737.png</url><title>Linear Regression</title><link>https://linstats.github.io/tags/linear-regression/</link></image><item><title>What Is Leverage and Why It Matters?</title><link>https://linstats.github.io/teaching/leverage-and-standardized-residuals/</link><pubDate>Sat, 12 Sep 2026 00:00:00 +0000</pubDate><guid>https://linstats.github.io/teaching/leverage-and-standardized-residuals/</guid><description>&lt;p>&lt;em>Adapted from NUS DSA3361 Tutorial 4 — Inferential Data Analytics.&lt;/em>&lt;/p>
&lt;p>When we first encounter regression, we often feel that estimating \(\hat{\beta}\) and obtaining the prediction equation \(\hat{y}=X\hat{\beta}\) should be enough. But courses usually do not stop there. We continue by looking more closely at the hat matrix \(H\), leverage values \(h_{ii}\), and related ideas.&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>Prerequisite:&lt;/strong> the matrix form of linear regression, especially&lt;/p>
&lt;p>\(\hat{\beta}=(X^TX)^{-1}X^Ty\;\;\Rightarrow\;\;\hat{y}=X\hat{\beta}\;\;\Rightarrow\;\;H=X(X^TX)^{-1}X^T\).&lt;/p>
&lt;/blockquote>
&lt;p>We will answer two questions that may be on your mind:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Why do we call \(h_{ii}=H_{ii}\) leverage values? Or why it measures leverage?&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Why does \(h_{ii}\) matter when we analyse residuals?&lt;/strong>&lt;/li>
&lt;/ul>
&lt;p>For simplicity, we will focus entirely on simple linear regression (SLR). Suppose that we have five observations: \((x_1,y_1), \ldots,(x_5,y_5),\)
and that the \(x\)&amp;rsquo;s have already been standardized, so that \(\operatorname{avg}(x)=0\) and \(\operatorname{std}(x)=1\).&lt;/p>
&lt;p>Under this standardization, the two regression coefficients can be written as&lt;/p>
\[
\hat{\beta}_1=\frac{1}{5}\sum_{j=1}^{5}x_jy_j\;\;\text{and}\;\;
\hat{\beta}_0=\bar{y}=\frac{1}{5}\sum_{j=1}^{5}y_j.
\]&lt;p>Now imagine changing the response of observation \(i\): replace \(y_i\) with \(y_i+\Delta\), while leaving all the other values unchanged. Then&lt;/p>
\[
\hat{\beta}_0\;\;\Rightarrow\;\;\hat{\beta}_0+\frac{\Delta}{5}\;\;\text{and}\;\;
\hat{\beta}_1\;\;\Rightarrow\;\;\hat{\beta}_1+\frac{x_i\Delta}{5}.
\]&lt;p>If \(x_i\approx 0\), then moving \(y_i\) up or down has almost no effect on the slope. It only shifts the regression line a little. But if \(|x_i|\) is large, the same change \(\Delta\) produces a much larger change in the slope: \(\Delta\hat{\beta}_1=x_i\Delta / 5\).&lt;/p>
&lt;p>&lt;mark style="background-color: #fff3b0; color: #222;">This is the intuitive origin of the word &lt;span style="font-weight: 700; color: #222;">leverage&lt;/span>: the farther a point is from the centre of the \(x\)-values, the more leverage it has to pull and rotate the regression line.&lt;/mark>&lt;/p>
&lt;p>But how much power can a point&amp;rsquo;s leverage really have? \(h_{ii}\) gives us a useful answer: it measures how much the point pulls the fitted line toward &lt;strong>itself&lt;/strong>.&lt;/p>
&lt;h2 id="why-h_ii-measures-leverage">Why \(h_{ii}\) Measures Leverage?&lt;/h2>
&lt;p>Suppose we have already estimated \(\hat{\beta}\). If we want fitted values for all five original observations, the prediction equation \(\hat{y}=X\hat{\beta}\) tells us to plug the original \(X\) back in:&lt;/p>
\[
\begin{pmatrix}\hat{y}_1\\\hat{y}_2\\\hat{y}_3\\\hat{y}_4\\\hat{y}_5\end{pmatrix}=\begin{pmatrix}1&amp;x_1\\1&amp;x_2\\1&amp;x_3\\1&amp;x_4\\1&amp;x_5\end{pmatrix}\hat{\beta}=
X(X^TX)^{-1}X^T\begin{pmatrix}y_1\\y_2\\y_3\\y_4\\y_5\end{pmatrix}=H\begin{pmatrix}y_1\\y_2\\y_3\\y_4\\y_5\end{pmatrix}.
\]&lt;p>In this example, \(H\) and \(h_{ii}\) are quite intuitive (I computed them for you):&lt;/p>
\[
H=\frac{1}{5}
\begin{pmatrix}
1+x_1^2 &amp; 1+x_1x_2 &amp; \cdots &amp; 1+x_1x_5 \\
1+x_2x_1 &amp; 1+x_2^2 &amp; \cdots &amp; 1+x_2x_5 \\
\vdots &amp; \vdots &amp; \ddots &amp; \vdots \\
1+x_5x_1 &amp; 1+x_5x_2 &amp; \cdots &amp; 1+x_5^2
\end{pmatrix}
\;\;\Rightarrow\;\;
h_{ii}=\frac{1+x_i^2}{5}.
\]&lt;p>If we focus on one of these fitted values, say the one for observation 1, then&lt;/p>
\[
\hat{y}_1=h_{11}y_1+\cdots+h_{15}y_5
=\frac{(1+x_1^2)y_1+(1+x_1x_2)y_2+\cdots+(1+x_1x_5)y_5}{5}.
\]&lt;p>Now the connection is visible:&lt;/p>
\[
y_1\text{ changes by }\Delta
\;\;\Rightarrow\;\;
\hat{y}_1\text{ changes by }\frac{1+x_1^2}{5}\Delta.
\]&lt;p>Clearly, the farther \(x_1\) is from the centre of the \(x\)-values, the more \(\hat{y}_1\) responds to a change in \(y_1\), and the stronger this self-pull becomes. For example:&lt;/p>
&lt;ul>
&lt;li>If \(x_1=2\), then \(h_{11}=(1+2^2)/5=1\). Moving \(y_1\) up by 1 unit changes \(\hat{y}_1\) by 1.&lt;/li>
&lt;li>If all other \(x_i=-0.5\), so other \(h_{ii}=(1+0.5^2)/5=0.25\). Moving \(y_i\) up by 1 unit changes \(\hat{y}_i\) by only 0.25.&lt;/li>
&lt;/ul>
&lt;p>The following figure may help make this idea more intuitive:
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img alt="Moving one high-leverage point changes the fitted regression line." srcset="
/teaching/leverage-and-standardized-residuals/featured-v2_hu5535729127743117904.webp 400w,
/teaching/leverage-and-standardized-residuals/featured-v2_hu16161458666698725301.webp 760w,
/teaching/leverage-and-standardized-residuals/featured-v2_hu14083171401276890129.webp 1200w"
src="https://linstats.github.io/teaching/leverage-and-standardized-residuals/featured-v2_hu5535729127743117904.webp"
width="760"
height="456"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>&lt;mark style="background-color: #fff3b0; color: #222;">So we see that \(h_{ii}\) measures how strongly observation \(i\) can pull the fitted line toward itself.&lt;/mark> The larger \(h_{ii}\) is, the more strongly the observation can shift the line, just like the point with \(x_1=2\) moves the line noticeably above, pulling \(\hat{y}_1\) closer to \(y_1\), making its residual \(e_1=y_1-\hat{y}_1\) look smaller. High-leverage points can therefore partly hide their own residuals. This leads us to our final topic:&lt;/p>
&lt;h2 id="why-does-h_ii-matter-when-we-analyse-residuals">Why does \(h_{ii}\) matter when we analyse residuals?&lt;/h2>
&lt;p>If \((x_1,y_1)\) is a high-leverage point, its fitted value \(\hat{y}_1\) is pulled toward \(y_1\). Its prediction may therefore look unusually accurate, with a smaller residual \(e_1=y_1-\hat{y}_1\). With a little matrix algebra, we can see why the residual is also less variable:&lt;/p>
\[
\begin{aligned}
e&amp;=y-\hat{y}=Iy-X\hat{\beta}\\
&amp;=Iy-X(X^TX)^{-1}X^Ty=(I-H)y.
\end{aligned}
\]&lt;p>Therefore, we find \(\operatorname{Cov}(e)=\sigma^2(I-H)\) or specifically \(\operatorname{Var}(e_1)=\sigma^2(1-h_{11})\). The larger the leverage \(h_{11}\), the smaller the variance of the first observation&amp;rsquo;s residual.&lt;/p>
&lt;p>How about the others? If we add up all five leverage values, we get&lt;/p>
\[
\sum_{i=1}^{5}h_{ii}
=\sum_{i=1}^{5}\frac{1+x_i^2}{5}
=\frac{5+\sum_{i=1}^{5}x_i^2}{5}
=\frac{5+5}{5}
=2.
\]&lt;details>
&lt;summary>Mini-Challenge: Why is \(\sum_{i=1}^{5}x_i^2=5\)?&lt;/summary>
\[
\text{Initial assumption:}\;\;
\operatorname{std}(x)=1
\;\;\Rightarrow\;\;
\frac{1}{5}\sum_{i=1}^{5}x_i^2=1
\;\;\Rightarrow\;\;
\sum_{i=1}^{5}x_i^2=5.
\]&lt;/details>
&lt;p>The total leverage power of all observations is fixed and limited! If \((x_1,y_1)\) has more power to pull the regression line, the other points have less power to pull it. As a result, the other residuals collectively have larger variance. This leads to the question:&lt;/p>
&lt;blockquote>
&lt;p>If we want to compare residuals, directly comparing the raw values \(e_1,\ldots,e_5\) is not quite fair, because their variability depends on its leverage.&lt;/p>
&lt;/blockquote>
&lt;p>The fix is quite simple. Since we know that \(\operatorname{Var}(e_i)=\sigma^2(1-h_{ii})\), we can standardize each residual by its own leverage-adjusted variability, replacing the unknown \(\sigma\) with the sample estimate \(s\). This gives the &lt;strong>studentized residual&lt;/strong>:&lt;/p>
\[
r_i:=
\frac{e_i}{s\sqrt{1-h_{ii}}}\;\;\text{where}\;
s=\sqrt{\frac{\sum_{j=1}^{n}e_j^2}{n-p}}\;\text{is an estimate of real std }\sigma.
\]&lt;p>The resulting \(r_i\) has some very useful properties. For any observation \(i\), we have approximately \(E(r_i)=0\) and \(\operatorname{Var}(r_i)=1\). If we also assume normally distributed errors, then approximately \(r_i\sim N(0,1)\). This makes studentized residuals convenient for checking model assumptions, such as constant variance and normality.&lt;/p>
&lt;p>That is why the model-diagnostics sections in DSA3361 (&lt;code>05 Model Diagnostics.pdf&lt;/code>) and ST3131 (&lt;code>Topic 3 To Model Adequacy Checking.pdf&lt;/code>) pay particular attention to \(r_i\).&lt;/p></description></item><item><title>ST3131 Regression Tutorials</title><link>https://linstats.github.io/teaching/st3131/</link><pubDate>Sat, 25 Apr 2026 00:00:00 +0000</pubDate><guid>https://linstats.github.io/teaching/st3131/</guid><description>&lt;p>I served as a teaching assistant for ST3131 in AY25/26 Semester 2. While going through the course with students, I received many thoughtful questions, and I decided to organize some of them here as supplementary notes and slides.&lt;/p>
&lt;p>Please note that these are &lt;strong>NOT&lt;/strong> the official materials for the current offering of the course. If you rely on any of the material here, please do so at your own discretion. For official guidance, always refer to your course instructor and the current Canvas page.&lt;/p>
&lt;h3 id="linear-regression-basics">Linear Regression Basics&lt;/h3>
&lt;p>If you are confused by how $\hat{\beta}$, $\mathrm{se}(\hat{\beta})$, the t-value, or confidence intervals are computed in &lt;code>R&lt;/code>, the following references may be helpful:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="files/table-SLR.pdf" target="_blank">This SLR table&lt;/a> for simple linear regression. It also includes a summary of ANOVA.&lt;/li>
&lt;li>&lt;a href="files/table-MLR.pdf" target="_blank">This MLR table&lt;/a> for multiple linear regression. It also includes a summary of ANOVA.&lt;/li>
&lt;/ul>
&lt;h3 id="tutorial-3-some-extensions">Tutorial 3: Some Extensions&lt;/h3>
&lt;ul>
&lt;li>The difference between the Confidence Interval (CI) and the Prediction Interval (PI)&lt;/li>
&lt;li>Another way to interpret the coefficient $\hat{\beta}$ in the model&lt;/li>
&lt;li>Proving that $\mathrm{SampCor}(x,y)=\sqrt{R^2}$ in simple linear regression&lt;/li>
&lt;/ul>
&lt;p>If you are interested, you can read &lt;a href="files/notes-extension-in-SLR.pdf" target="_blank">this Tutorial 3 extension note&lt;/a>.&lt;/p>
&lt;h3 id="tutorial-6-model-assumptions">Tutorial 6: Model Assumptions&lt;/h3>
&lt;ul>
&lt;li>What regression model assumptions mean&lt;/li>
&lt;li>How to deal with possible violations in practice&lt;/li>
&lt;/ul>
&lt;p>In &lt;a href="files/notes-model-assumptions.pdf" target="_blank">this note&lt;/a>, we use a dataset as a concrete example to show how these assumptions can be checked.&lt;/p>
&lt;h3 id="tutorial-7--8-multicollinearity">Tutorial 7 &amp;amp; 8: Multicollinearity&lt;/h3>
&lt;p>Multicollinearity can create difficulties in computation, whether in &lt;code>R&lt;/code> or &lt;code>Python&lt;/code>.&lt;/p>
&lt;ul>
&lt;li>&lt;a href="files/slides-MC.pdf" target="_blank">These Tutorial 7 slides&lt;/a> introduce what multicollinearity is and why it causes trouble.&lt;/li>
&lt;li>&lt;a href="files/slides-solve-MC.pdf" target="_blank">These Tutorial 8 slides&lt;/a> introduce how to detect multicollinearity and some common remedies.&lt;/li>
&lt;/ul>
&lt;h3 id="tutorial-10-logistic-regression">Tutorial 10: Logistic Regression&lt;/h3>
&lt;p>Linear regression (SLR/MLR) is used when the response variable $y$ is continuous. But what should we use when facing a classification problem, for example when $y$ takes only two values such as male/female?&lt;/p>
&lt;p>A natural extension is logistic regression. &lt;a href="files/slides-logit-reg.pdf" target="_blank">These Tutorial 10 slides&lt;/a> give a quick introduction to the model, including how to fit it, perform hypothesis testing, and make predictions.&lt;/p></description></item></channel></rss>