plot(NA,xlim =range(f),ylim =c(0,1.2*max(dfmat[-1,])),xlab ="r",ylab ="pdf of F distribution")for(j in1:length(nu1)) lines(dfmat[j,]~f, col = j)legend(x = .5*max(f),y =1.1*max(dfmat[-1,]),legend = lab,col =1:length(nu1), lty =1,bty ="n", cex = .8)
The overall F-test
We may wish to test whether any covariates are important, that is \[
\text{$H_0$: $\beta_j = 0$ for all $j = 1,\dots,p$}.
\]
The overall F-test of significance is carried out as follows:
Fit the model with all the covariates and obtain the value \[
F_{\operatorname{test}} = \frac{\operatorname{MS}_{\operatorname{Reg}}}{\operatorname{MS}_{\operatorname{Error}}} \left( = \frac{\operatorname{SS}_{\operatorname{Reg}}/ p}{\operatorname{SS}_{\operatorname{Error}}/ (n - (p+1))} \right)
\]
Reject \(H_0\) at \(\alpha\) if \(F_{\operatorname{test}} > F_{p,n - (p + 1),\alpha}\).
Obtain p-value is \(P(F > F_{\operatorname{test}})\), where \(F \sim F_{p,n-(p+1)}\).
This test statistic and p-value are reported by summary() on lm().
Exercise
Show that the test statistic of the overall F test can be written \[
F_{\operatorname{test}} = \frac{\operatorname{MS}_{\operatorname{Reg}}}{\operatorname{MS}_{\operatorname{Error}}} = \frac{(n - (p+1))}{p}\frac{R^2}{1 - R^2},
\] where \(R^2\) is the coefficient of determination.
SC apartments
lm_out <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)summary(lm_out)
Suppose you fit a regression model with \(p = 10\) predictors on a data set with \(214\) observations, and you obtain \(\hat \sigma = 0.207\) and \(R^2 = 0.43\). It turns out that these pieces of information are sufficient to determine all the values in the ANOVA table. See if you can fill in all the values.
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}}\)
Testing for significance of a subset of covariates
Consider testing the significance of a subset \(D \subset \{1,\dots,p\}\) of covariates: \[
\text{$H_0$: $\beta_j = 0$ for all $j \in D$.}
\] Use the full-reduced model F-test:
Let \(s\) be the number of covariates in \(D\) and compute \[
F_{\operatorname{test}} =\frac{(\operatorname{SS}_{\operatorname{Error}}(\text{Reduced}) - \operatorname{SS}_{\operatorname{Error}}(\text{Full}))/s}{\operatorname{SS}_{\operatorname{Error}}(\text{Full})/(n -(p+1))},
\]
“Full” is the model with all \(p\) covariates.
“Reduced” is the model after dropping the covariates in \(D\).
Reject \(H_0\) at \(\alpha\) if \(F_{\operatorname{test}} > F_{s,n-(p+1),\alpha}\).
Obtain p-value as \(P(F > F_{\operatorname{test}})\), where \(F \sim F_{s,n-(p+1)}\).
SC apartments
Check whether the number of bedrooms or bathrooms contributes significantly to the rent.
That is, test \(H_0\): \(\beta_{\operatorname{nbath}} = \beta_{\operatorname{nbed}} = 0\).
Fit full and reduced model and obtain \(\operatorname{SS}_{\operatorname{Error}}\):
lm_red <-lm(log(price) ~ city +log(sqft) + pets +log(sqft):city, data = scapts)lm_full <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)SSE_red <-sum(lm_red$residuals^2)SSE_full <-sum(lm_full$residuals^2)
Reduced model (without the city variables and their interactions): \[\begin{align*}
\widehat{\log(\texttt{price})} = 3.832 &+ (-0.112) \cdot \texttt{nbath} + (0.042) \cdot \texttt{nbed} \\
&+ (0.483) \log(\texttt{sqft}) + (-0.06) \cdot \texttt{pets}
\end{align*}\]
We obtain \(\operatorname{SS}_{\operatorname{Error}}(\text{Full}) = 8.69\) and \(\operatorname{SS}_{\operatorname{Error}}(\text{Reduced}) = 10.586\).
SC apartments
Compute the test statistic for the full-reduced model F test.
s <-6# significance of this many columns of X being testedFtest <- (SSE_red - SSE_full)/s / ( SSE_full / (n - (p +1)))alpha <-0.05F_crit <-qf(1- alpha,s,n-(p+1))pval <-1-pf(Ftest, s, n - (p+1))
We obtain \(F_{\operatorname{test}} = 7.381\) and \(F_{s,n-(p+1),0.05} = 2.143\), and the p-value is \(3.723\times 10^{-7}\).
Full-reduced model F test for a single covariate
If we test \(H_0\): \(\beta_j = 0\) for a single covariate using the full-reduced model F test, then:
The test statistic \(F_{\operatorname{test}}\) will be equal to the square of the test statistic \(T_{\operatorname{test}}\) for testing \(H_0\): \(\beta_j = 0\) in the full model.
The p-value will be the same as that of the test based on \(T_{\operatorname{test}}\). This is due to the fact that \(T \sim t_\nu\) implies \(T^2 \sim F_{1,\nu}\).
SC apartments
Use the full-reduced model F test to test whether the pets variable has an influence on the price.
lm_red <-lm(log(price) ~ city + nbath + nbed +log(sqft) +log(sqft):city, data = scapts)lm_full <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)SSE_red <-sum(lm_red$residuals^2)SSE_full <-sum(lm_full$residuals^2)s <-1# significance of a single covariate being testedFtest <- (SSE_red - SSE_full)/s / ( SSE_full / (n - (p +1)))pval <-1-pf(Ftest,s,n-(p+1))
We obtain \(\sqrt{F} = 0.26 = |T_{\operatorname{test}}|\). The p value is \(0.795\). Compare this to that given in the summary() output.
Effect of correlations among the covariates
From before \(\operatorname{Var}\hat \beta_j = \sigma^2 \Omega_{jj}/n\). An alternate expression gives \[
\operatorname{Var}\hat \beta_j = \frac{1}{1 - R^2_j} \frac{\sigma^2}{\sum_{i=1}^n(x_{ji} - \bar x_j)^2},
\] where \(R^2_j\) is the \(R^2\) from regressing \(x_j\) on the other covariates.
So multicollinearity of \(x_j\) with the other covariates “inflates” \(\operatorname{Var}\hat \beta_j\):
Makes confidence intervals for \(\beta_j\) wider.
Makes tests of \(H_0\): \(\beta_j = 0\) vs \(H_1\): \(\beta_j \neq 0\) less powerful.
Call \(\displaystyle \frac{1}{1- R^2_j}\) the variance inflation factor (VIF) for \(x_j\), \(j = 1,\dots,p\).
SC apartments
Add to the data set a spurious predictor highly correlated with the square footage.
Check the effect of this on our inferences for \(\beta_{\log(\operatorname{sqft})}\).
# make new x correlated with square feetx <- scapts$sqft +rnorm(n,0,50)scaptsx <-cbind(scapts,x)
SC apartments
plot(scaptsx)
SC apartments
lm_out <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)confint(lm_out)
Note the change in VIF for log(sqft) due to including x in the model!
Variable selection
Sometimes the number of potentially important predictors is quite large.
Large \(p\) tends to increase the VIFs, leading to low power.
So we may wish to discard some predictors. We briefly discuss:
Best subset selection with Mallow’s \(C(p)\)
Forward and backward stepwise selection with AIC
LASSO selection
And most importantly:
The dangers of naïve post-selection inference!!
Best subset selection with Mallow’s \(C_p\)
Given \(q\) available covariates, there are \(2^q\) possible subset models (why?).
Mallow’s\(C_p\) can be used to compare subset models: Let \[
C_p = (n - (p + 1))\left[\frac{\operatorname{MS}_{\operatorname{Error}}(\operatorname{subset})}{\operatorname{MS}_{\operatorname{Error}}(\operatorname{all})} - 1\right] + (p + 1),
\] where
\(p\) is the number of predictors in the subset model.
\(\operatorname{MS}_{\operatorname{Error}}(\operatorname{subset})\) is the \(\operatorname{MS}_{\operatorname{Error}}\) of the subset model.
\(\operatorname{MS}_{\operatorname{Error}}(\operatorname{all})\) is the \(\operatorname{MS}_{\operatorname{Error}}\) of the model with all the covariates.
If the subset model is adequate, \(\operatorname{MS}_{\operatorname{Error}}(\operatorname{subset})\) estimates the same target as \(\operatorname{MS}_{\operatorname{Error}}(\operatorname{all})\), so the first term should be small and \(C_p \approx p+1\).
Can look at \(C_p\) values for all subset models of each size \(p=0,1,2,\dots,q\)
Want smallest model such that \(C_p \approx p+1\).
SC apartments
Can run the following to identify best model of each size and obtain the Mallow’s \(C_p\) statistic for each of these models.
library(leaps) # first time run install.packages("leaps")regsubsets_out <-regsubsets(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)summary(regsubsets_out) # see best model of each sizesummary(regsubsets_out)$cp # get corresponding Cp values
赤池 弘次 (あかいけひろつぐ)
Introduced Akaike’s Information Criterion (AIC).
Akaike’s Information Criterion (AIC) for comparing models
For a given model, i.e. set of covariates, AIC is defined as
\[
\operatorname{AIC}= 2(p+1) - 2~\underbrace{\ell(\hat \sigma^2, \hat \beta_0,\hat \beta_1,\dots,\hat \beta_p)}_{\text{log-likelihood}}.
\] The log-likelihood is the log of the joint pdf of the data (STAT 512).
AIC can be used to compare several models for the same data.
The “best” model is the one which minimizes AIC.
Stepwise selection based on AIC
Stepwise selection:
Backward: Begin with all the predictors and remove one at a time.
Forward: Begin with no predictors and add one at a time.
In each step remove/add predictor to get largest decrease in AIC.
If a decrease in AIC is not possible, stop.
SC apartments
Below we run backward selection with the step() function:
lm_intercept <-lm(log(price) ~1, data = scapts)lm_all <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)# backward selectionstep_back <-step(lm_all,direction ="backward",scope =formula(lm_all))