Now set \[
\mathbf{Y}= \left[\begin{matrix}Y_1 \\
Y_2 \\
\vdots \\
Y_n
\end{matrix}\right], \quad
\mathbf{X}= \left[ \begin{matrix} 1 &x_{11}& \dots & x_{1p} \\
1 &x_{21}& \dots & x_{2p} \\
\vdots &\vdots& \ddots & \vdots\\
1 &x_{n1}& \dots & x_{np}
\end{matrix} \right],\quad \mathbf{b}= \left[\begin{matrix}\beta_0 \\
\beta_1 \\
\vdots \\
\beta_p
\end{matrix}\right],
\quad \mathbf{e}= \left[\begin{matrix}\varepsilon_1 \\
\varepsilon_2 \\
\vdots \\
\varepsilon_n
\end{matrix}\right]
\] Then the above equations can be written in matrix form as \(\mathbf{Y}= \mathbf{X}\mathbf{b}+ \mathbf{e}\).
Least-squares estimators in matrix form
Provided \(\mathbf{X}^T\mathbf{X}\) is invertible, the entries of the vector \[
\hat{\mathbf{b}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{Y}
\] give the least-squares estimators \(\hat \beta_0,\hat \beta_1,\dots,\hat \beta_p\).
Important: Can only compute \(\hat{\mathbf{b}}\) if no column of \(\mathbf{X}\) can be constructed as a linear combination of other columns (equivalent to \(\mathbf{X}^T\mathbf{X}\) invertible).
SC apartments
Make indicator or dummy variables to encode the four cities:
\[\begin{align*}
\texttt{Columbia} &= \left\{\begin{array}{ll}
1& \text{ if \texttt{city} is Columbia}\\
0& \text{otherwise }\end{array}\right. \\
\texttt{Greenville} &= \left\{\begin{array}{ll}
1& \text{ if \texttt{city} is Greenville}\\
0& \text{otherwise }\end{array}\right. \\
\texttt{Rock Hill} &= \left\{\begin{array}{ll}
1& \text{ if \texttt{city} is Rock Hill}\\
0& \text{otherwise }\end{array}\right.
\end{align*}\]
Then Charleston is indicated by \((\texttt{Columbia},\texttt{Greenville},\texttt{Rock Hill}) = (0,0,0)\).
Note: If we included in \(\mathbf{X}\) an indicator for all four levels, \(\mathbf{X}^T\mathbf{X}\) would not be invertible due to the presence of the intercept column (this is linear algebra knowledge)!
SC apartments
The \(\texttt{pets}\) column contains the values “yes” and “no”.
Convert it to a dummy variable column: \[
\texttt{pets} = \left\{\begin{array}{ll}
1& \text{ if yes}\\
0& \text{ if no} \end{array}\right.
\]
This code computes \(\hat{\mathbf{b}}\) from \(\mathbf{X}\) and \(\mathbf{Y}\).
bhat <-solve(t(X) %*% X) %*%t(X) %*% Ybhat
[,1]
Intercept 3.9327
Columbia -0.2409
Greenville -0.1195
Rock Hill -0.1454
nbath -0.1073
nbed 0.0645
log_sqft 0.4728
pets -0.0186
Interpretation of the slope parameters
Consider what stories \(\hat \beta_0,\hat \beta_1,\dots,\hat \beta_p\) tell in the fitted MLR model \[
\hat Y_i = \hat \beta_0 + x_{i1}\hat \beta_1 + \dots + x_{ip}\hat \beta_p , \quad i =1,\dots,n.
\]
\(\hat \beta_0\), as in SLR, just gives the function the right “height”.
\(\hat \beta_j\) is the amount by which the mean of \(Y\) is estimated to change due to a 1-unit increase in covariate \(j\), with all other variables held fixed.
Take note of how interpretations change when variables are transformed!
The effect of an additional bedroom (all else being equal) is to increase the expected log price by \(0.065\), or to increase the expected price by a factor of \(1.067\), i.e. by \(6.666\) percent.
An apartment with \(1\%\) more square feet (all else being equal) is expected to cost approximately \(0.473\%\) more.
An apartment which allows pets (all else being equal) is expected to have a log price \(0.019\) lower, that is, to be cheaper by a factor of \(0.982\), i.e. to be \(1.84\%\) cheaper.
In which city are apartments most expensive (all else being equal)?
Do not omit all else being equal (or ceteris paribus in the Latin ;-))!
Estimating the error term variance
After obtaining \(\hat \beta_0,\hat \beta_1,\dots,\hat \beta_p\), define the
We most often test hypotheses about the \(\beta_j\) of the form \[
\begin{array}{lclcl}
H_0\text{: } \beta_j \geq 0 &\text{ or } & H_0\text{: } \beta_j = 0 &\text{ or } & H_0\text{: } \beta_j \leq 0\\
H_1\text{: } \beta_j < 0 && H_1\text{: } \beta_j \neq 0 && H_1\text{: } \beta_j > 0.
\end{array}
\] Reject or fail to reject \(H_0\) based on the value of the test statistic \[
T_{\operatorname{test}} = \frac{\hat \beta_j }{\hat\sigma \sqrt{\Omega_{jj}}}.
\] Rejection rules for the above at significance level \(\alpha\) are \[
\begin{array}{lclcl}
T_{\operatorname{test}} < -t_{n-(p+1),\alpha} &\text{ or } & |T_{\operatorname{test}}| > t_{n-(p+1),\alpha/2}&\text{ or } & T_{\operatorname{test}} > t_{n-(p+1),\alpha}.
\end{array}
\]
The corresponding p-values are, with \(T \sim t_{n-(p+1)}\), the probabilities \[
\begin{array}{lclcl}
P(T < T_{\operatorname{test}}) &\text{ or } & 2 \times P(T > |T_{\operatorname{test}}|) & \text{ or } & P(T > T_{\operatorname{test}}).
\end{array}
\]
SC apartments
Obtain p-values for testing \(H_0\): \(\beta_j = 0\) vs \(H_1\): \(\beta_j \neq 0\) for each \(j\).
CI for the mean and PI for \(Y_{\operatorname{new}}\) at \(\mathbf{x}_{\operatorname{new}}\)
For a new vector of covariate values \(\mathbf{x}_{\operatorname{new}}\), let \[
\hat Y_{\operatorname{new}} = \hat \beta_0 + \hat \beta_1 x_{\operatorname{new},1} + \dots + \hat \beta_p x_{\operatorname{new},p}
\]
A \((1-\alpha)\times 100\) CI for \(\beta_0 + \beta_1 x_{\operatorname{new},1} + \dots + \beta_p x_{\operatorname{new},p}\) is given by \[
\hat Y_{\operatorname{new}} \pm t_{n-(p+1),\alpha/2} \hat \sigma \sqrt{ \Omega_{\operatorname{new}}},
\]
A \((1-\alpha)\times 100\) PI for \(Y_{\operatorname{new}}\) corresponding to \(\mathbf{x}_{\operatorname{new}}\) is given by \[
\hat Y_{\operatorname{new}} \pm t_{n-(p+1),\alpha/2} \hat \sigma \sqrt{1 + \Omega_{\operatorname{new}}},
\] where \(\Omega_{\operatorname{new}} = \tilde{\mathbf{x}}_{\text{new}}^T \Omega ~\tilde{\mathbf{x}}_{\text{new}}^T\) with \(\tilde{\mathbf{x}}_{\text{new}}^T = (1 ~ x_{\operatorname{new},1} ~ \cdots ~ x_{\operatorname{new},p})^T\).
SC apartments
Build 95% CI for the average rent of apartments in Greenville with one bathroom, two bedrooms, nine-hundred square feet, and which allow pets.
xnew <-c(1,0,1,0,1,2,log(900),1)om_new <-t(xnew) %*% Om %*% xnewYnew_hat <-t(xnew) %*% bhatseci <-sqrt(sgsqhat) *sqrt(om_new)loci <- Ynew_hat - ta2 * seciupci <- Ynew_hat + ta2 * seci# exponentiate since we have used the log of the priceeloci <-exp(loci)eupci <-exp(upci)
The confidence interval is \([1040.545,1234.266]\).
SC apartments
Now build a 95% PI for the rent of a single such a property.
sepi <-sqrt(sgsqhat) *sqrt( 1+ om_new)lopi <- Ynew_hat - ta2 * sepiuppi <- Ynew_hat + ta2 * sepi# exponentiate since we have used the log of the priceelopi <-exp(lopi)euppi <-exp(uppi)
The prediction interval is \([745.099,1723.675]\).
pi <-predict(lm_out, newdata = newdata, int ="pred")exp(pi)
fit lwr upr
1 1133 745 1724
Sums of squares in multiple linear regression
We decompose the variation in \(Y_1,\dots,Y_n\) by defining the:
Total sum of squares: \(\operatorname{SS}_{\operatorname{Tot}} = \sum_{i=1}^n(Y_i - \bar Y_n)^2\)
Regression sum of squares: \(\operatorname{SS}_{\operatorname{Reg}} = \sum_{i=1}^n(\hat Y_i - \bar Y_n)^2\)
Error sum of squares: \(\operatorname{SS}_{\operatorname{Error}} = \sum_{i=1}^n(Y_i - \hat Y_i)^2\)
We have \(\operatorname{SS}_{\operatorname{Tot}} = \operatorname{SS}_{\operatorname{Reg}} + \operatorname{SS}_{\operatorname{Error}}\).
The coefficient of determination is defined as \(\displaystyle R^2 = \frac{\operatorname{SS}_{\operatorname{Reg}}}{\operatorname{SS}_{\operatorname{Tot}}}\).
\(R^2 \in [0,1]\)
Proportion of variation in \(Y\) “explained” by the covariates \(x_1,\dots,x_p\).
The mean squares in multiple linear regression
The SS, appropriately scaled, follow chi-square distributions:
where \(\phi_{\operatorname{Tot}}\) and \(\phi_{\operatorname{Reg}}\) are noncentrality parameters.
Dividing \(\operatorname{SS}_{\operatorname{Reg}}\) and \(\operatorname{SS}_{\operatorname{Error}}\) by their dfs, we define:
Regression mean square: \(\displaystyle \operatorname{MS}_{\operatorname{Reg}} = \frac{\operatorname{SS}_{\operatorname{Reg}}}{p}\)
Error mean square: \(\displaystyle \operatorname{MS}_{\operatorname{Error}} = \frac{\operatorname{SS}_{\operatorname{Error}}}{n-(p+1)}\)
Moreover, define the adjusted R squared as \(\displaystyle \bar R^2 = 1 - \frac{\operatorname{MS}_{\operatorname{Error}} }{\operatorname{SS}_{\operatorname{Tot}}/(n-1)}\).
Adjustment “penalizes” the inclusion of additional covariates.
The Analysis of Variance (ANOVA) table
We often present the SS, df, and MS values in a table like this:
Source
Df
SS
MS
F value
p-value
Reg
\(p\)
\(\operatorname{SS}_{\operatorname{Reg}}\)
\(\operatorname{MS}_{\operatorname{Reg}}\)
\(F_{\operatorname{test}}\)
\(P(F > F_{\operatorname{test}})\)
Error
\(n-(p+1)\)
\(\operatorname{SS}_{\operatorname{Error}}\)
\(\operatorname{MS}_{\operatorname{Error}}\)
Total
\(n-1\)
\(\operatorname{SS}_{\operatorname{Tot}}\)
This is an example of an ANOVA table.
The F-value and the p-value we will discuss later in these slides.
lm1 <-lm(log(price) ~ nbath, data = scapts)lm2 <-lm(log(price) ~ nbath + city, data = scapts)lm3 <-lm(log(price) ~ nbath + city + nbed, data = scapts)lm4 <-lm(log(price) ~ nbath + city + nbed +log(sqft), data = scapts)lm5 <-lm(log(price) ~ nbath + city + nbed +log(sqft) + pets, data = scapts)SSR1 <- SST -sum(lm1$residuals^2)SSR2 <- SST -sum(lm2$residuals^2)SSR3 <- SST -sum(lm3$residuals^2)SSR4 <- SST -sum(lm4$residuals^2)SSR5 <- SST -sum(lm5$residuals^2)seqSS <-c(SSR1,SSR2 - SSR1,SSR3 - SSR2, SSR4 - SSR3, SSR5 - SSR4)names(seqSS) <-c("city","nbath","nbed","log(sqft)","pets")round(seqSS,3)
city nbath nbed log(sqft) pets
1.309 1.517 1.867 1.602 0.016
Checking model assumptions
Validity of the foregoing analyses depends on these assumptions:
The responses are normally distributed around the regression line (Check QQ plot of residuals). If \(n\) is large this only matters for prediction intervals.
The response has the same variance for all covariate values (Check residuals vs fitted values plot).
The covariates and the response are linearly related (Check residuals vs fitted values plot).
The response values are independent of each other (No way to check; must trust experimental design).
Generating diagnostic plots from lm() with plot()
plot(lm_out,which =2)
plot(lm_out,which =1,add.smooth = F)
Leverage and Cook’s distance in MLR
The leverage of a point \((\mathbf{x}_i,Y_i)\) among \((\mathbf{x}_1,Y_1),\dots,(\mathbf{x}_n,Y_n)\) is \[
\text{lev}_i = \text{ entry $i$ on the diagonal of the matrix } \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}.
\] Leverage only shows outlying-ness in the covariate space.
Cook’s Distance measures how much each data point changes the fit: \[
D_i = \frac{1}{(p+1) \hat \sigma^2}\sum_{j = 1 }^n(\hat Y_j - \hat Y_{j(i)})^2 = \frac{\hat e_i^2}{(p+1)\hat \sigma^2}\frac{\text{lev}_i}{(1 - \text{lev}_i)^2} \quad \text{ for } i = 1,\dots,n,
\] where \(\hat Y_{j(i)}\) is the \(j\)th fitted value from the model fitted without obs \(i\).
plot(lm_out,which =4)
Interactions
When the effect of one predictor depends on the value of another it is called an interaction.
E.g., does the effect of square feet differ between Charleston and Rock Hill, Charleston and Columbia?
We can include interactions by including in \(\mathbf{X}\) products of pairs of predictors.
Most often we consider interactions between dummy variables and numeric variables.
Such interactions act as “slope modifiers”, allowing different slope coefficients for different subsets of the data.
SC apartments
Add an interaction between square feet and each city indicator.