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).
Estimating the error term variance
After obtaining \(\hat \beta_0,\hat \beta_1,\dots,\hat \beta_p\), define the
residuals as \(\hat \varepsilon_i = Y_i - \hat Y_i\)
for \(i = 1,\dots,n\).
Then an unbiased estimator of \(\sigma^2\) is given by \[
\hat \sigma^2 = \frac{1}{n-(p+1)} \sum_{i=1}^n \hat \varepsilon_i^2.
\]
SC apartments (cont)
Make indicator or dummy variables to encode the four cities:
\[\begin{align*}
x_{\text{Columbia}} &= \left\{\begin{array}{ll}
1& \text{ if city is Columbia}\\
0& \text{otherwise }\end{array}\right. \\
x_{\text{Greenville}} &= \left\{\begin{array}{ll}
1& \text{ if city is Greenville}\\
0& \text{otherwise }\end{array}\right. \\
x_{\text{Rock Hill}} &= \left\{\begin{array}{ll}
1& \text{ if city is Rock Hill}\\
0& \text{otherwise }\end{array}\right.
\end{align*}\]
Then Charleston is indicated by \((x_{\text{Columbia}},x_{\text{Greenville}},x_{\text{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 (cont)
The pets column contains the values “yes” and “no”.
Use a dummy variable to encode it: \[
x_{\text{pets}} = \left\{\begin{array}{ll}
1& \text{ if yes}\\
0& \text{ if no} \end{array}\right.
\]
SC apartments (cont)
Construct the response vector and the design matrix.
Estimate the regression coefficients and the error term variance:
bhat <-solve(t(X) %*% X) %*%t(X) %*% Ybhat
[,1]
Intercept 3.93274698
Columbia -0.24090102
Greenville -0.11950394
Rock Hill -0.14535978
nbath -0.10728344
nbed 0.06453526
log_sqft 0.47283468
pets -0.01857260
Yhat <- X %*% bhatehat <- Y - Yhatp <-ncol(X) -1sgsqhat <-sum(ehat^2) / (n - (p +1))sgsqhat
[1] 0.04336654
Interpretation of the slope parameters
Consider what stories \(\beta_0,\beta_1,\dots,\beta_p\) tell in the MLR model \[
Y_i = \beta_0 + x_{i1}\beta_1 + \dots + x_{ip}\beta_p + \varepsilon_i, \quad i =1,\dots,n.
\]
\(\beta_0\), as in SLR, just gives the function the right “height”.
\(\beta_j\) is the amount by which the mean of \(Y\) changes due to a 1-unit increase in covariate \(j\), with all other variables held fixed.
SC apartments
bhat
[,1]
Intercept 3.93274698
Columbia -0.24090102
Greenville -0.11950394
Rock Hill -0.14535978
nbath -0.10728344
nbed 0.06453526
log_sqft 0.47283468
pets -0.01857260
The effect of an additional bedroom (all else being equal) is to increase the expected log price by 0.0645353, or to increase the expected price by a factor of 1.0666632, or by 6.6663193 percent.
An apartment which allows pets (all else being equal) is expected to have a log price 0.0185726 lower, that is, to be cheaper by a factor of 0.9815988, i.e. to be 1.8401195 percent 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 ;-))!
Confidence intervals for the slope parameters
Let \(\displaystyle \Omega = (\mathbf{X}^T\mathbf{X})^{-1}\) with \(\Omega_{jj}\) the diagonal entry corresponding to \(\beta_j\).
Then the estimator \(\hat \beta_j\) is distributed as \[
\hat \beta_j \sim \text{Normal}\left(\beta_j,\sigma^2 \Omega_{jj}\right).
\]
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 (cont)
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}} + 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}} + 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 (cont)
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)\).
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.274 745.0992 1723.675
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.
Check for significance of any interaction effects and give interpretations.
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)
Leverage and Cook’s distance in MLR
The leverage of a point \((Y_i,\mathbf{x}_i)\) among \((Y_1,\mathbf{x}_1),\dots,(Y_n,\mathbf{x}_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)
References
“Apartment for Rent Classified.” 2019. UCI Machine Learning Repository.