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 example (cont)
lm_out <-lm(log(price) ~ city + nbath + nbed +log(sqft) + pets +log(sqft):city, data = scapts)summary(lm_out)
Exercise: 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.430\). Use this information to fill in the entire ANOVA table:
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 example (cont)
Check whether the number of bedrooms or bathrooms contributes significantly to the rent.
That is test \(H_0\): \(\beta_{\operatorname{nbed}} = \beta_{\operatorname{nbath}} = 0\).
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)s <-2# significance of two covariates 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}} = 5.078\) and \(F_{s,n-(p+1),0.05} = 3.04\), and the p-value is 0.007.
SC apartments example (cont)
Check whether the city makes any difference in the price.
lm_red <-lm(log(price) ~log(sqft) + pets, 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 <-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}} = 8.566\) and \(F_{s,n-(p+1),0.05} = 2.143\), and the p-value is 0.
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, 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.
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.2602945 = |T_{\operatorname{test}}|\). The p value is 0.7949004. 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\).
VIFs in SC apartment prices example
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)
plot(scaptsx)
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\).
Mallow’s \(C_p\) on the rental properties data
Compute Mallow’s \(C_p\) for a single subset model:
Predict wage from 28 covariates? Too many sub-models to consider!
hist(fifa$wage_eur)
The wage distribution has some high outlying observations.
hist(log(fifa$wage_eur))
Perhaps better to consider the log of the wage.
lm_wage <-lm(wage_eur ~ ., data = fifa)plot(lm_wage,which =1)
lm_logwage <-lm(log(wage_eur) ~ ., data = fifa)plot(lm_logwage,which =1)
赤池 弘次 (あかいけひろつぐ)
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.
The extractAIC() function
The extractAIC function in R returns a modified version of AIC: \[
\operatorname{AIC}^* = 2(p+1) + n\log(\operatorname{SS}_{\operatorname{Error}}/n)
\]
lm_out <-lm(log(wage_eur) ~ age + potential, data = fifa)extractAIC(lm_out) # gives value p + 1 as well as AIC value
[1] 3.0000 -860.2624
# compute it "manually"n <-nrow(fifa)p <-22*(p+1) + n *log(sum(lm_out$residuals^2)/n)
[1] -860.2624
Comparing models using AIC
Compare two models for the FIFA data with AIC:
lm1 <-lm(log(wage_eur) ~ age + potential + height_cm, data = fifa)extractAIC(lm1)
[1] 4.0000 -858.5413
lm2 <-lm(log(wage_eur) ~ height_cm + overall, data = fifa)extractAIC(lm2)
[1] 3.000 -1377.386
The second model has a smaller value of AIC, so it is better according to this criterion.
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.
Forward and backward stepwise selection may give different models!
LASSO selection
The LASSO estimators \(\hat \beta_0^L,\hat \beta_1^L,\dots,\hat \beta_p^L\) are obtained by minimizing \[
Q_\lambda(b_0,b_1,\dots,b_p) = \sum_{i=1}^n(Y_i - (b_0 + b_1 x_{i1} + \dots + b_p x_{ip}))^2 + \lambda \sum_{j=1}^p|b_j|,
\] where \(\lambda > 0\) is a tuning parameter.
The penalty term \(\lambda \sum_{j=1}^p|b_j|\) can cause \(\hat \beta^L_j = 0\) for some \(j\).
For \(\lambda\) large enough, all the \(\hat \beta^L_j\) will be equal to zero.
So LASSO performs variable selection and estimation simultaneously.
Drawback: Hard to build CIs based on \(\hat \beta_0^L,\hat \beta_1^L,\dots,\hat \beta_p^L\).
Effect of LASSO penalty on the objective function
# simulate some data with centered X and centered y (eliminates intercept)n <-500;p <-2X <-scale(matrix(rnorm(n*p),n,p)); b <-c(2,1/4); e <-rnorm(n)y <-drop(X %*% b) + e -mean(e)# define least squares and LASSO objective functionsQ <-function(b,X,y) mean((y - X %*% b)^2)Qlambda <-function(b,X,y,lambda) Q(b,X,y) + lambda *sum(abs(b))# set LASSO penalty parameterlambda <-1# evaluate Q and Qlambda over a grid of b1 and b2 valuesb1seq <-seq(b[1]-2,b[1]+2,length=200)b2seq <-seq(b[2]-2,b[2]+2,length=200)Q_vals <- Qlambda_vals <-matrix(0,length(b1seq),length(b2seq))for(i in1:length(b1seq))for(j in1:length(b2seq)){ Q_vals[i,j] <-Q(b=c(b1seq[i],b2seq[j]),X,y) Qlambda_vals[i,j] <-Qlambda(b=c(b1seq[i],b2seq[j]),X,y,lambda) }
# compute least squares and lasso estimatorbhat <-coef(lm(y~X-1)) bhat_lambda <-optim(par =c(0,0),fn = Qlambda,X = X, y = y,lambda = lambda)$par# make contour plots of least-squares and LASSO objective functionspar(mfrow=c(1,2))contour(z = Q_vals, x = b1seq, y = b2seq, main ="lambda = 0",xlab ="b1", ylab ="b2")points(x = bhat[1],y = bhat[2]);abline(v = bhat[1], lty =3);abline(h = bhat[2], lty =3)contour(z = Qlambda_vals, x = b1seq, y = b2seq, main =paste( "lambda =",lambda), xlab ="b1", ylab ="b2")points(x = bhat_lambda[1],y = bhat_lambda[2]); abline(v = bhat_lambda[1],lty =3);abline(h = bhat_lambda[2],lty =3)
LASSO on the FIFA data
Use cv.ncvreg() function from R package ncvreg.
Runs crossvalidation to choose the best value of \(\lambda\).
library(ncvreg) # first time run install.packages("ncvreg")
Warning: package 'ncvreg' was built under R version 4.4.1
# prepare response vector and design matrixy <-log(fifa$wage_eur)X <- fifa[,-c(1,5)]X$nationality <-ifelse(fifa$nationality_name =="usa",1,0)# crossvalidation to choose lambda lasso <-cv.ncvreg(X,y,penalty ="lasso")
lasso$fit$beta[,lasso$min] # estimates under the "best" lambda