This example is based on the Credit data set (also used in the text). The purpose is to construct a predictive model for the variable “Balance”. We will use regsubsets() function which is a part of the leaps package. For more information visit the CRAN page: leaps: Regression Subset Selection.
library(ISLR)
library(leaps)
regfit.full <- regsubsets(Balance ~ . -ID, data = Credit)
summary(regfit.full)## Subset selection object
## Call: regsubsets.formula(Balance ~ . - ID, data = Credit)
## 11 Variables (and intercept)
## Forced in Forced out
## Income FALSE FALSE
## Limit FALSE FALSE
## Rating FALSE FALSE
## Cards FALSE FALSE
## Age FALSE FALSE
## Education FALSE FALSE
## GenderFemale FALSE FALSE
## StudentYes FALSE FALSE
## MarriedYes FALSE FALSE
## EthnicityAsian FALSE FALSE
## EthnicityCaucasian FALSE FALSE
## 1 subsets of each size up to 8
## Selection Algorithm: exhaustive
## Income Limit Rating Cards Age Education GenderFemale StudentYes
## 1 ( 1 ) " " " " "*" " " " " " " " " " "
## 2 ( 1 ) "*" " " "*" " " " " " " " " " "
## 3 ( 1 ) "*" " " "*" " " " " " " " " "*"
## 4 ( 1 ) "*" "*" " " "*" " " " " " " "*"
## 5 ( 1 ) "*" "*" "*" "*" " " " " " " "*"
## 6 ( 1 ) "*" "*" "*" "*" "*" " " " " "*"
## 7 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 8 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## MarriedYes EthnicityAsian EthnicityCaucasian
## 1 ( 1 ) " " " " " "
## 2 ( 1 ) " " " " " "
## 3 ( 1 ) " " " " " "
## 4 ( 1 ) " " " " " "
## 5 ( 1 ) " " " " " "
## 6 ( 1 ) " " " " " "
## 7 ( 1 ) " " " " " "
## 8 ( 1 ) " " "*" " "
An asterisk (*) indicates that the variable is selected in the respective model. For example, the best two-variable model contains only Income and Rating. The best three-variable model contains Income, Rating, and Student dummy.
By default only up to 8-variable models are reported. This can be changed using nvmax option:
regfit.full <- regsubsets(Balance ~ . -ID, data = Credit, nvmax = 11)
summary(regfit.full) ## Subset selection object
## Call: regsubsets.formula(Balance ~ . - ID, data = Credit, nvmax = 11)
## 11 Variables (and intercept)
## Forced in Forced out
## Income FALSE FALSE
## Limit FALSE FALSE
## Rating FALSE FALSE
## Cards FALSE FALSE
## Age FALSE FALSE
## Education FALSE FALSE
## GenderFemale FALSE FALSE
## StudentYes FALSE FALSE
## MarriedYes FALSE FALSE
## EthnicityAsian FALSE FALSE
## EthnicityCaucasian FALSE FALSE
## 1 subsets of each size up to 11
## Selection Algorithm: exhaustive
## Income Limit Rating Cards Age Education GenderFemale StudentYes
## 1 ( 1 ) " " " " "*" " " " " " " " " " "
## 2 ( 1 ) "*" " " "*" " " " " " " " " " "
## 3 ( 1 ) "*" " " "*" " " " " " " " " "*"
## 4 ( 1 ) "*" "*" " " "*" " " " " " " "*"
## 5 ( 1 ) "*" "*" "*" "*" " " " " " " "*"
## 6 ( 1 ) "*" "*" "*" "*" "*" " " " " "*"
## 7 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 8 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 9 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 10 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 11 ( 1 ) "*" "*" "*" "*" "*" "*" "*" "*"
## MarriedYes EthnicityAsian EthnicityCaucasian
## 1 ( 1 ) " " " " " "
## 2 ( 1 ) " " " " " "
## 3 ( 1 ) " " " " " "
## 4 ( 1 ) " " " " " "
## 5 ( 1 ) " " " " " "
## 6 ( 1 ) " " " " " "
## 7 ( 1 ) " " " " " "
## 8 ( 1 ) " " "*" " "
## 9 ( 1 ) "*" "*" " "
## 10 ( 1 ) "*" "*" "*"
## 11 ( 1 ) "*" "*" "*"
The output above shows us the best models for each set of models that contain \(k\) variables where \(k=1,2,...,11\). There are 10 variables in the data set but because ethnicity has three levels we created two dummies (one of them is the base group).
We have 11 model selection criteria for each model listed above. Which one should we choose? The following code chunk prints Adj.R2 on the screen:
reg.summary <- summary(regfit.full)
reg.summary$rsq ## [1] 0.7458484 0.8751179 0.9498788 0.9535800 0.9541606 0.9546879 0.9548167
## [8] 0.9548880 0.9549636 0.9550468 0.9551016
According to this output, the Model that contains 7 variables has the maximum Adj.R2=0.9548167.
Similarly, what’s the best model according to the Cp (or, equivalently, AIC) criterion?
reg.summary <- summary(regfit.full)
reg.summary$cp ## [1] 1800.308406 685.196514 41.133867 11.148910 8.131573 5.574883
## [7] 6.462042 7.845931 9.192355 10.472883 12.000000
According to the output above, the minimum Cp is 5.574883, corresponding to the Model that contains 6 variables.
We can automatize this and plot the results for easy evaluation. The following code chunk does that.
# the number of variables
p <- 11
# 2x2 plot grid
par(mfrow=c(2,2))
# plot RSS
plot(reg.summary$rss, xlab="Number of Variables", xaxt="n", ylab="RSS", type="o")
axis(1, at = seq(1, p, by = 1),las=2)
# plot Adj.R2
plot(reg.summary$adjr2, xlab="Number of Variables", xaxt="n", ylab="Adjusted RSq",type="o")
axis(1, at = seq(1, p, by = 1),las=2)
# find the model with max Adj.R2
best <- which.max(reg.summary$adjr2)
# put red dot on the graph that marks the best according to Adj.R2
points(best, reg.summary$adjr2[best], col="red", cex=2, pch=20)
# plot Cp
plot(reg.summary$cp, xlab="Number of Variables", xaxt="n", ylab="Cp",type='o')
axis(1, at = seq(1, p, by = 1),las=2)
# find the best according to Cp
bestCp <- which.min(reg.summary$cp)
points(bestCp, reg.summary$cp[bestCp], col="red", cex=2, pch=20)
# plot BICs
plot(reg.summary$bic, xlab="Number of Variables", xaxt="n", ylab="BIC", type='o')
bestBIC <- which.min(reg.summary$bic)
points(bestBIC, reg.summary$bic[bestBIC], col="red", cex=2, pch=20)
axis(1, at = seq(1, p, by = 1),las=2)Exercise: Can you write a function that automatizes the computations in the code chunk above and plots the results?
The chosen model is marked with a red dot. Adj.R2 chooses the model with 7 variables, Cp chooses the model with 6 variables, and BIC chooses the model with 4 variables. These results conform to our expectations. The BIC tends to choose smaller models in finite samples because it puts a larger penalty as models become more complex. Note that RSS is not used to select models.
Alternatively, we can use the leaps::plot.regsubsets() function to display the selected variables for the best model with a given number of predictors, ranked according to the BIC, Cp, adjusted R2, or AIC. For example,
plot(regfit.full,scale="adjr2")The top row of plot above contains a black square for each variable selected according to the optimal model associated with Adjusted Rsquared. So the optimal model contains the following variables: income, limit, rating, cards, age, gender dummy, and student dummy (7 variables plus intercept).
Similarly, using Cp
plot(regfit.full, scale="Cp")And according to BIC:
plot(regfit.full, scale="bic")The BIC chooses the smallest model that contains the following variables: income, limit, cards, and student dummy. To see that coefficient estimates associated with that model, we can run:
coef(regfit.full, 4)## (Intercept) Income Limit Cards StudentYes
## -499.7272117 -7.8392288 0.2666445 23.1753794 429.6064203
We can also use the regsubsets() function to perform forward stepwise or backward stepwise selection, using the argument method="forward" or method="backward".
regfit.fwd <- regsubsets(Balance ~ . -ID, data = Credit, nvmax=11, method="forward")
reg.fwd.summary <- summary(regfit.fwd)
reg.fwd.summary## Subset selection object
## Call: regsubsets.formula(Balance ~ . - ID, data = Credit, nvmax = 11,
## method = "forward")
## 11 Variables (and intercept)
## Forced in Forced out
## Income FALSE FALSE
## Limit FALSE FALSE
## Rating FALSE FALSE
## Cards FALSE FALSE
## Age FALSE FALSE
## Education FALSE FALSE
## GenderFemale FALSE FALSE
## StudentYes FALSE FALSE
## MarriedYes FALSE FALSE
## EthnicityAsian FALSE FALSE
## EthnicityCaucasian FALSE FALSE
## 1 subsets of each size up to 11
## Selection Algorithm: forward
## Income Limit Rating Cards Age Education GenderFemale StudentYes
## 1 ( 1 ) " " " " "*" " " " " " " " " " "
## 2 ( 1 ) "*" " " "*" " " " " " " " " " "
## 3 ( 1 ) "*" " " "*" " " " " " " " " "*"
## 4 ( 1 ) "*" "*" "*" " " " " " " " " "*"
## 5 ( 1 ) "*" "*" "*" "*" " " " " " " "*"
## 6 ( 1 ) "*" "*" "*" "*" "*" " " " " "*"
## 7 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 8 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 9 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 10 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 11 ( 1 ) "*" "*" "*" "*" "*" "*" "*" "*"
## MarriedYes EthnicityAsian EthnicityCaucasian
## 1 ( 1 ) " " " " " "
## 2 ( 1 ) " " " " " "
## 3 ( 1 ) " " " " " "
## 4 ( 1 ) " " " " " "
## 5 ( 1 ) " " " " " "
## 6 ( 1 ) " " " " " "
## 7 ( 1 ) " " " " " "
## 8 ( 1 ) " " "*" " "
## 9 ( 1 ) "*" "*" " "
## 10 ( 1 ) "*" "*" "*"
## 11 ( 1 ) "*" "*" "*"
regfit.bwd <- regsubsets(Balance ~ . -ID, data = Credit, nvmax=11, method="backward")
reg.bwd.summary <- summary(regfit.bwd)
reg.bwd.summary## Subset selection object
## Call: regsubsets.formula(Balance ~ . - ID, data = Credit, nvmax = 11,
## method = "backward")
## 11 Variables (and intercept)
## Forced in Forced out
## Income FALSE FALSE
## Limit FALSE FALSE
## Rating FALSE FALSE
## Cards FALSE FALSE
## Age FALSE FALSE
## Education FALSE FALSE
## GenderFemale FALSE FALSE
## StudentYes FALSE FALSE
## MarriedYes FALSE FALSE
## EthnicityAsian FALSE FALSE
## EthnicityCaucasian FALSE FALSE
## 1 subsets of each size up to 11
## Selection Algorithm: backward
## Income Limit Rating Cards Age Education GenderFemale StudentYes
## 1 ( 1 ) " " "*" " " " " " " " " " " " "
## 2 ( 1 ) "*" "*" " " " " " " " " " " " "
## 3 ( 1 ) "*" "*" " " " " " " " " " " "*"
## 4 ( 1 ) "*" "*" " " "*" " " " " " " "*"
## 5 ( 1 ) "*" "*" "*" "*" " " " " " " "*"
## 6 ( 1 ) "*" "*" "*" "*" "*" " " " " "*"
## 7 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 8 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 9 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 10 ( 1 ) "*" "*" "*" "*" "*" " " "*" "*"
## 11 ( 1 ) "*" "*" "*" "*" "*" "*" "*" "*"
## MarriedYes EthnicityAsian EthnicityCaucasian
## 1 ( 1 ) " " " " " "
## 2 ( 1 ) " " " " " "
## 3 ( 1 ) " " " " " "
## 4 ( 1 ) " " " " " "
## 5 ( 1 ) " " " " " "
## 6 ( 1 ) " " " " " "
## 7 ( 1 ) " " " " " "
## 8 ( 1 ) " " "*" " "
## 9 ( 1 ) "*" "*" " "
## 10 ( 1 ) "*" "*" "*"
## 11 ( 1 ) "*" "*" "*"
The forward and the best subset selection algorithms produce the same set of variables up to the models containing 3 variables. For example, compare the models with 2 variables:
coef(regfit.full, 2)## (Intercept) Income Rating
## -534.812150 -7.672124 3.949265
coef(regfit.fwd, 2) ## (Intercept) Income Rating
## -534.812150 -7.672124 3.949265
Similarly, for 3-variable models:
coef(regfit.full, 3)## (Intercept) Income Rating StudentYes
## -581.078888 -7.874931 3.987472 418.760284
coef(regfit.fwd, 3) ## (Intercept) Income Rating StudentYes
## -581.078888 -7.874931 3.987472 418.760284
But the best 4-variable models identified by the full subset selection and forward selection are different:
coef(regfit.full, 4)## (Intercept) Income Limit Cards StudentYes
## -499.7272117 -7.8392288 0.2666445 23.1753794 429.6064203
coef(regfit.fwd, 4) ## (Intercept) Income Limit Rating StudentYes
## -516.7182606 -7.9446339 0.1216825 2.1904387 422.6683787
Exercise Check if 5-variable (or larger) models are the same.
Which model is the best? Using the Cp criterion, the best model is
coef(regfit.fwd, which.min(reg.fwd.summary$cp)) ## (Intercept) Income Limit Rating Cards Age
## -493.7341870 -7.7950824 0.1936914 1.0911874 18.2118976 -0.6240560
## StudentYes
## 425.6099369
This is the same as the best subset selection algorithm:
coef(regfit.full, which.min(reg.summary$cp))## (Intercept) Income Limit Rating Cards Age
## -493.7341870 -7.7950824 0.1936914 1.0911874 18.2118976 -0.6240560
## StudentYes
## 425.6099369
However, when we use the BIC criterion:
coef(regfit.fwd, which.min(reg.fwd.summary$bic)) ## (Intercept) Income Limit Rating Cards StudentYes
## -526.1555233 -7.8749239 0.1944093 1.0879014 17.8517307 426.8501456
Compare this to the best subset selection result:
coef(regfit.full, which.min(reg.summary$bic)) ## (Intercept) Income Limit Cards StudentYes
## -499.7272117 -7.8392288 0.2666445 23.1753794 429.6064203
Similarly, using the backward selection, overall the best model according to the BIC is
coef(regfit.bwd, which.min(reg.bwd.summary$bic)) ## (Intercept) Income Limit Cards StudentYes
## -499.7272117 -7.8392288 0.2666445 23.1753794 429.6064203
Exercise Is the best 2-variable model from the backward selection is the same as the best 2-variable model from the forward selection? What about 3-variable models?
Now we must use only the training data set to select variables. If the full data set is used to perform the best subset selection step, the validation set errors and cross-validation errors that we obtain will not be accurate estimates of the test error.
To use the validation set approach, we start by splitting the data set into training and testing parts, just like we did in our previous classes.
set.seed(1)
train <- sample(c(TRUE, FALSE), nrow(Credit), replace = TRUE)
test <- (!train) The variable train equals TRUE if the corresponding observation is included in the training set. The full data set is randomly split into two parts.
Now, we apply regsubsets() to the training set in order to perform best subset selection:
regfit.best <- regsubsets(Balance ~ . -ID, data = Credit[train,], nvmax=11) What is the validation error for the best model of each model size. We first make a model matrix from the test data?
test.mat <- model.matrix(Balance ~ . -ID, data = Credit[test,]) The function model.matrix() is used to form the \(X\) matrix from the data. Now we run a loop, and for each size i, we extract the coefficients from regfit.best for the best model of that size, multiply them with the appropriate columns of the test model matrix to form the predictions, and compute the test MSE.
val.errors <- rep(NA, 11)
for(i in 1:11){
coefi <- coef(regfit.best, id = i)
pred <- test.mat[, names(coefi)] %*% coefi
val.errors[i] <- mean( (Credit$Balance[test] - pred)^2 )
}
val.errors ## [1] 53192.555 30760.192 10614.653 9955.349 10061.285 9909.393 9779.751
## [8] 9789.864 9786.823 9811.449 9809.436
Which one is the best, i.e., the smallest?
which.min(val.errors) ## [1] 7
And the best model is
coef(regfit.best, 7) ## (Intercept) Income Limit Rating Cards Age
## -477.3734655 -7.9345156 0.2170404 0.7376694 20.1619077 -0.3818065
## GenderFemale StudentYes
## -19.5683296 403.9528111
As noted in the ISLR lab in chapter 6, the procedure above is straightforward but a little bit tedious because there is no predict() function in the regsubsets(). We can write our own predict function to be used later. Remember: if you repeat the same computations over and over again, it is best to write a function instead of copying and pasting the previous code.
predict.regsubsets <- function(object, newdata, id,...){
form <- as.formula(object$call[[2]])
mat <- model.matrix(form, newdata)
coefi <- coef(object, id=id)
xvars <- names(coefi)
mat[, xvars] %*% coefi
} The validation set approach produced the best model as the one that contains 7 variables (see above). What is the best model that contains 7 variables according to the best subset selection algorithm? The following code chunk answers that question:
regfit.best <- regsubsets(Balance ~ . -ID, data=Credit, nvmax=11)
coef(regfit.best, 7) ## (Intercept) Income Limit Rating Cards Age
## -488.6158695 -7.8036338 0.1936237 1.0940490 18.1091708 -0.6206538
## GenderFemale StudentYes
## -10.4531521 426.5812620
This is the same as the model chosen by the validation set approach. Note that we used full data in this case (not training data).
Exercise If we use training data in the best subset selection algorithm, which model do we get? Is it the same?
First, we need to decide the number of folds, \(k\), over which we must perform best subset selection.
# number of folds
k <- 10
# number of obs
n <- nrow(Credit)
# number of variables
p <- 11
# set seed for replication
set.seed(1)
# create a vector that allocates each observation to one of k = 10 folds
folds <- sample(1:k, n, replace=TRUE)
# create a matrix to store the results
cv.errors <- matrix(NA,k,p, dimnames=list(NULL, paste(1:p))) In the code chunk above, the elements of folds that equal j are in the test set in the jth fold, and the remainder are in the training set.
table(folds) ## folds
## 1 2 3 4 5 6 7 8 9 10
## 42 33 41 39 47 36 38 37 46 41
Now we are ready to perform cross-validation within a loop.
for(j in 1:k){
best.fit <- regsubsets(Balance ~ . -ID, data=Credit[folds!=j,], nvmax=p)
for(i in 1:p){
pred <- predict(best.fit, Credit[folds==j,], id=i)
cv.errors[j,i] <- mean( (Credit$Balance[folds==j]-pred)^2)
}
} This gives us a \(10\times 11\) matrix:
cv.errors ## 1 2 3 4 5 6 7
## [1,] 81124.56 23362.56 11849.994 9050.738 9538.270 9986.893 9987.164
## [2,] 28667.44 13815.31 7752.963 7780.368 7677.880 7370.166 7955.655
## [3,] 60627.88 40629.17 10012.320 8763.335 8765.759 8610.799 8556.514
## [4,] 44629.07 19916.42 9936.721 9142.450 9393.875 9317.582 9709.637
## [5,] 76645.20 35569.15 12383.959 12919.508 12884.639 12537.364 12475.382
## [6,] 69924.61 34977.08 16423.316 15517.659 15603.200 15469.363 15500.844
## [7,] 35517.63 23296.33 9345.040 7027.504 7344.594 7412.651 7347.323
## [8,] 37173.69 20829.08 8201.702 9808.096 9472.902 8998.778 9119.427
## [9,] 50368.90 24329.97 11013.003 9376.528 9513.906 8998.777 8944.410
## [10,] 50751.17 28833.38 10886.628 10847.022 10493.894 10040.811 10081.902
## 8 9 10 11
## [1,] 10032.999 10058.781 10037.748 10001.909
## [2,] 8059.772 8068.802 8038.772 7948.618
## [3,] 8570.586 8773.004 8613.540 8551.449
## [4,] 9823.930 9937.931 9785.713 9716.512
## [5,] 12696.862 12711.894 12640.854 12654.023
## [6,] 15617.316 15579.726 15516.688 15465.076
## [7,] 7403.121 7430.428 7382.557 7384.592
## [8,] 8973.533 9162.901 9016.839 8923.451
## [9,] 8983.321 9055.863 9095.248 9065.961
## [10,] 10209.532 10260.934 10233.747 10173.039
We use the apply() function to average over the columns of this matrix in order to obtain a vector for which the jth element is the cross-validation error for the i-variable model.
mean.cv.errors <- apply(cv.errors, 2, mean)
mean.cv.errors ## 1 2 3 4 5 6 7 8
## 53543.015 26555.846 10780.565 10023.321 10068.892 9874.318 9967.826 10037.097
## 9 10 11
## 10104.026 10036.171 9988.463
par(mfrow=c(1,1))
best.cv <- which.min(mean.cv.errors)
plot(mean.cv.errors, type='b', xaxt="n",
xlab="Numbe of variables",
ylab="Cross-validation error")
points(best.cv, mean.cv.errors[best.cv], col="red", cex=2, pch=20)
axis(1, at = seq(1, 11, by = 1),las=2)The cross-validation approach selects a 6-variable model.
Estimate a 6-variable model using the best subset selection approach:
reg.best <- regsubsets(Balance ~ . -ID, data=Credit, nvmax=p)
coef(reg.best, best.cv) ## (Intercept) Income Limit Rating Cards Age
## -493.7341870 -7.7950824 0.1936914 1.0911874 18.2118976 -0.6240560
## StudentYes
## 425.6099369
Exercise: Compare the model selected by CV with the previous approaches.
We will use glmnet package to perform the ridge regression and the LASSO. The main function is glmnet() which only accepts vectors and matrices. We will not use y ~ x syntax. The missing values in the data set must be removed before proceeding. For more detailed information, see An Introduction to glmnet.
We first create a vector y containing response variable, and a matrix X containing variables.
x <- model.matrix(Balance ~ . -ID, Credit)[,-1]
y <- Credit$Balance Note that x matrix excludes the intercept column:
head(x) ## Income Limit Rating Cards Age Education GenderFemale StudentYes MarriedYes
## 1 14.891 3606 283 2 34 11 0 0 1
## 2 106.025 6645 483 3 82 15 1 1 1
## 3 104.593 7075 514 4 71 11 0 0 0
## 4 148.924 9504 681 3 36 11 1 0 0
## 5 55.882 4897 357 2 68 16 0 0 1
## 6 80.180 8047 569 4 77 10 0 0 0
## EthnicityAsian EthnicityCaucasian
## 1 0 1
## 2 1 0
## 3 1 0
## 4 1 0
## 5 0 1
## 6 0 1
model.matrix()[,-1] omits the first column (which is the intercept by default).
The model.matrix()function is particularly useful for creating x; not only does it produce a matrix corresponding to the 11 predictors but it also automatically transforms any qualitative variables into dummy variables. The latter property is important because glmnet() can only take numerical, quantitative inputs.
The glmnet() function has an alpha argument that determines what type of model is fit. If alpha=0 then a ridge regression model is fit, and if alpha=1 then a lasso model is fit.
library(glmnet)
grid <- 10^seq(10,-2,length=100)
ridge.mod <- glmnet(x, y, alpha=0, lambda=grid)The tuning parameter \(\lambda\) values are determined on a grid from \(\lambda=10^{10}\) to \(\lambda=10^{-2}\) (you may use the default settings as well). This set of values cover a range of possibilities from a model with only an intercept to the OLS regression. Also, glmnet() automatically standardizes variables.
We have a set of coefficients for each value of \(\lambda\).
dim(coef(ridge.mod)) ## [1] 12 100
There are 12 models in our example, one for each predictor plus an intercept model. To access a particular set of coefficients we can use
ridge.mod$lambda[50]## [1] 11497.57
coef(ridge.mod)[,50] ## (Intercept) Income Limit Rating
## 443.882328225 0.207297549 0.006255511 0.093529429
## Cards Age Education GenderFemale
## 1.094782827 -0.007423108 -0.036491175 0.727221775
## StudentYes MarriedYes EthnicityAsian EthnicityCaucasian
## 15.224286108 -0.273927934 -0.323636288 -0.088984490
We now split the sample into a training set and a test set in order to estimate the test error of ridge regression.
set.seed(1)
train <- sample(1:nrow(x), nrow(x)/2)
test <- (-train)
y.test <- y[test] Next we fit a ridge regression model on the training set, and evaluate its MSE on the test set, using \(\lambda=5\). Note the use of the predict() function again. This time we get predictions for a test set, by replacing type="coefficients" with the newx argument.
ridge.mod <- glmnet(x[train,], y[train], alpha=0, lambda=grid, thresh=1e-12)
ridge.pred <- predict(ridge.mod, s=5, newx=x[test,])
mean((ridge.pred - y.test)^2) ## [1] 10364.53
The test MSE is 10364.53 We can compare this to a model without any x variable. In that case, we would predict each observation in the test set using the average of y variable in the training set.
mean((mean(y[train]) - y.test)^2) ## [1] 194031
We’d obtain the same result if we used a very large value for lambda:
ridge.pred <- predict(ridge.mod, s=1e10, newx=x[test,])
mean((ridge.pred-y.test)^2) ## [1] 194030.9
Is there any benefit to using the ridge regression with \(\lambda=5\) instead of plain OLS regression? The ridge regression with \(\lambda=0\) gives the OLS result, so:
ridge.pred <- predict(ridge.mod, s=0, newx = x[test,], exact = T,
x = x[train,], y = y[train])
mean((ridge.pred - y.test)^2) ## [1] 10690.72
It looks like the ridge regression indeed provides improvement over OLS regression. Compare coefficient estimates from lm() function and glmnet() with s=0:
lm(y ~ x, subset = train)##
## Call:
## lm(formula = y ~ x, subset = train)
##
## Coefficients:
## (Intercept) xIncome xLimit
## -442.7079 -7.6232 0.2617
## xRating xCards xAge
## 0.1148 20.8071 -1.1848
## xEducation xGenderFemale xStudentYes
## -0.1258 -10.8190 426.2268
## xMarriedYes xEthnicityAsian xEthnicityCaucasian
## -8.5852 -2.0410 -3.8183
predict(ridge.mod, s = , exact = T, type = "coefficients",
x = x[train, ], y = y[train])[1:12, ] ## 12 x 100 sparse Matrix of class "dgCMatrix"
##
## (Intercept) 5.228849e+02 5.228849e+02 5.228848e+02 5.228848e+02
## Income 3.100554e-07 4.098749e-07 5.418305e-07 7.162680e-07
## Limit 7.972649e-09 1.053937e-08 1.393243e-08 1.841785e-08
## Rating 1.189378e-07 1.572288e-07 2.078472e-07 2.747617e-07
## Cards 1.107865e-06 1.464532e-06 1.936025e-06 2.559311e-06
## Age -1.068734e-07 -1.412804e-07 -1.867643e-07 -2.468915e-07
## Education -2.141268e-07 -2.830631e-07 -3.741927e-07 -4.946606e-07
## GenderFemale -6.037613e-07 -7.981369e-07 -1.055090e-06 -1.394767e-06
## StudentYes 1.730791e-05 2.288004e-05 3.024607e-05 3.998352e-05
## MarriedYes 2.161341e-06 2.857165e-06 3.777004e-06 4.992976e-06
## EthnicityAsian 1.939281e-07 2.563616e-07 3.388952e-07 4.479997e-07
## EthnicityCaucasian 3.869795e-07 5.115641e-07 6.762576e-07 8.939726e-07
##
## (Intercept) 5.228847e+02 5.228846e+02 5.228845e+02 5.228843e+02
## Income 9.468640e-07 1.251698e-06 1.654671e-06 2.187377e-06
## Limit 2.434731e-08 3.218571e-08 4.254761e-08 5.624542e-08
## Rating 3.632188e-07 4.801538e-07 6.347350e-07 8.390821e-07
## Cards 3.383259e-06 4.472469e-06 5.912340e-06 7.815765e-06
## Age -3.263760e-07 -4.314499e-07 -5.703514e-07 -7.539711e-07
## Education -6.539122e-07 -8.644332e-07 -1.142730e-06 -1.510621e-06
## GenderFemale -1.843799e-06 -2.437394e-06 -3.222090e-06 -4.259413e-06
## StudentYes 5.285586e-05 6.987233e-05 9.236710e-05 1.221039e-04
## MarriedYes 6.600419e-06 8.725364e-06 1.153441e-05 1.524781e-05
## EthnicityAsian 5.922298e-07 7.828938e-07 1.034941e-06 1.368134e-06
## EthnicityCaucasian 1.181779e-06 1.562242e-06 2.065191e-06 2.730060e-06
##
## (Intercept) 5.228841e+02 5.228839e+02 5.228835e+02 5.228830e+02
## Income 2.891583e-06 3.822501e-06 5.053119e-06 6.679920e-06
## Limit 7.435312e-08 9.829042e-08 1.299341e-07 1.717651e-07
## Rating 1.109217e-06 1.466319e-06 1.938386e-06 2.562431e-06
## Cards 1.033198e-05 1.365827e-05 1.805542e-05 2.386820e-05
## Age -9.967056e-07 -1.317587e-06 -1.741772e-06 -2.302522e-06
## Education -1.996951e-06 -2.639850e-06 -3.489724e-06 -4.613206e-06
## GenderFemale -5.630691e-06 -7.443439e-06 -9.839783e-06 -1.300760e-05
## StudentYes 1.614141e-04 2.133799e-04 2.820757e-04 3.728874e-04
## MarriedYes 2.015670e-05 2.664595e-05 3.522435e-05 4.656447e-05
## EthnicityAsian 1.808597e-06 2.390866e-06 3.160599e-06 4.178150e-06
## EthnicityCaucasian 3.608976e-06 4.770850e-06 6.306776e-06 8.337175e-06
##
## (Intercept) 5.228824e+02 5.228815e+02 5.228804e+02 5.228789e+02
## Income 8.830453e-06 1.167332e-05 1.543142e-05 2.039938e-05
## Limit 2.270632e-07 3.001639e-07 3.967985e-07 5.245434e-07
## Rating 3.387380e-06 4.477912e-06 5.919528e-06 7.825253e-06
## Cards 3.155234e-05 4.171031e-05 5.513854e-05 7.288983e-05
## Age -3.043800e-06 -4.023727e-06 -5.319136e-06 -7.031594e-06
## Education -6.098379e-06 -8.061685e-06 -1.065705e-05 -1.408795e-05
## GenderFemale -1.719527e-05 -2.273110e-05 -3.004913e-05 -3.972308e-05
## StudentYes 4.929351e-04 6.516309e-04 8.614174e-04 1.138742e-03
## MarriedYes 6.155539e-05 8.137244e-05 1.075693e-04 1.421998e-04
## EthnicityAsian 5.523311e-06 7.301566e-06 9.652371e-06 1.276009e-05
## EthnicityCaucasian 1.102123e-05 1.456939e-05 1.925981e-05 2.546022e-05
##
## (Intercept) 5.228769e+02 5.228743e+02 5.228709e+02 5.228664e+02
## Income 2.696670e-05 3.564811e-05 4.712440e-05 6.229515e-05
## Limit 6.934139e-07 9.166484e-07 1.211750e-06 1.601853e-06
## Rating 1.034450e-05 1.367477e-05 1.807716e-05 2.389681e-05
## Cards 9.635595e-05 1.273767e-04 1.683841e-04 2.225933e-04
## Age -9.295369e-06 -1.228796e-05 -1.624401e-05 -2.147371e-05
## Education -1.862338e-05 -2.461888e-05 -3.254450e-05 -4.302154e-05
## GenderFemale -5.251141e-05 -6.941665e-05 -9.176419e-05 -1.213059e-04
## StudentYes 1.505349e-03 1.989981e-03 2.630635e-03 3.477539e-03
## MarriedYes 1.879789e-04 2.484955e-04 3.284938e-04 4.342448e-04
## EthnicityAsian 1.686849e-05 2.229988e-05 2.948036e-05 3.897345e-05
## EthnicityCaucasian 3.365672e-05 4.449187e-05 5.881504e-05 7.774899e-05
##
## (Intercept) 5.228604e+02 5.228524e+02 5.228419e+02 5.228281e+02
## Income 8.234958e-05 1.088597e-04 1.439032e-04 1.902265e-04
## Limit 2.117539e-06 2.799233e-06 3.700371e-06 4.891586e-06
## Rating 3.158993e-05 4.175960e-05 5.520299e-05 7.297380e-05
## Cards 2.942541e-04 3.889846e-04 5.142114e-04 6.797513e-04
## Age -2.838713e-05 -3.752637e-05 -4.960811e-05 -6.557980e-05
## Education -5.687130e-05 -7.517937e-05 -9.938071e-05 -1.313720e-04
## GenderFemale -1.603577e-04 -2.119806e-04 -2.802210e-04 -3.704270e-04
## StudentYes 4.597091e-03 6.077066e-03 8.033492e-03 1.061975e-02
## MarriedYes 5.740379e-04 7.588302e-04 1.003104e-03 1.326001e-03
## EthnicityAsian 5.152436e-05 6.811874e-05 9.006042e-05 1.190746e-04
## EthnicityCaucasian 1.027778e-04 1.358630e-04 1.795974e-04 2.374076e-04
##
## (Intercept) 5.228098e+02 5.227855e+02 5.227535e+02 5.227112e+02
## Income 2.514595e-04 3.323994e-04 4.393857e-04 5.807953e-04
## Limit 6.466236e-06 8.547717e-06 1.129911e-05 1.493595e-05
## Rating 9.646479e-05 1.275168e-04 1.685628e-04 2.228179e-04
## Cards 8.985808e-04 1.187853e-03 1.570240e-03 2.075709e-03
## Age -8.669404e-05 -1.146069e-04 -1.515078e-04 -2.002918e-04
## Education -1.736599e-04 -2.295576e-04 -3.034431e-04 -4.011015e-04
## GenderFemale -4.896676e-04 -6.472854e-04 -8.556272e-04 -1.131008e-03
## StudentYes 1.403859e-02 1.855802e-02 2.453232e-02 3.242977e-02
## MarriedYes 1.752820e-03 2.316993e-03 3.062698e-03 4.048303e-03
## EthnicityAsian 1.574446e-04 2.081937e-04 2.753266e-04 3.641523e-04
## EthnicityCaucasian 3.138220e-04 4.148249e-04 5.483227e-04 7.247606e-04
##
## (Intercept) 5.226553e+02 5.225813e+02 5.224836e+02 5.223545e+02
## Income 7.676956e-04 1.014706e-03 1.341132e-03 1.772462e-03
## Limit 1.974303e-05 2.609664e-05 3.449389e-05 4.559131e-05
## Rating 2.945310e-04 3.893156e-04 5.145876e-04 6.801415e-04
## Cards 2.743869e-03 3.627064e-03 4.794470e-03 6.337491e-03
## Age -2.647870e-04 -3.500555e-04 -4.627925e-04 -6.118537e-04
## Education -5.301758e-04 -7.007623e-04 -9.261935e-04 -1.224071e-03
## GenderFemale -1.494986e-03 -1.976038e-03 -2.611777e-03 -3.451867e-03
## StudentYes 4.286936e-02 5.666921e-02 7.491063e-02 9.902268e-02
## MarriedYes 5.350915e-03 7.072364e-03 9.347100e-03 1.235256e-02
## EthnicityAsian 4.817142e-04 6.373677e-04 8.435581e-04 1.116873e-03
## EthnicityCaucasian 9.579341e-04 1.266059e-03 1.673177e-03 2.211005e-03
##
## (Intercept) 5.221839e+02 5.219585e+02 5.216608e+02 5.212676e+02
## Income 2.342331e-03 3.095095e-03 4.089214e-03 5.401652e-03
## Limit 6.025581e-05 7.963152e-05 1.052278e-04 1.390346e-04
## Rating 8.989095e-04 1.187961e-03 1.569812e-03 2.074147e-03
## Cards 8.376889e-03 1.107218e-02 1.463402e-02 1.934051e-02
## Age -8.089556e-04 -1.069603e-03 -1.414320e-03 -1.870291e-03
## Education -1.617622e-03 -2.137479e-03 -2.824012e-03 -3.730368e-03
## GenderFemale -4.561856e-03 -6.028219e-03 -7.964959e-03 -1.052224e-02
## StudentYes 1.308938e-01 1.730194e-01 2.286961e-01 3.022783e-01
## MarriedYes 1.632280e-02 2.156633e-02 2.848942e-02 3.762644e-02
## EthnicityAsian 1.479477e-03 1.961084e-03 2.601693e-03 3.455435e-03
## EthnicityCaucasian 2.921359e-03 3.859317e-03 5.097342e-03 6.730625e-03
##
## (Intercept) 5.207486e+02 5.200636e+02 5.191603e+02 5.179698e+02
## Income 7.133600e-03 9.417875e-03 1.242839e-02 1.639216e-02
## Limit 1.836726e-04 2.425899e-04 3.203156e-04 4.227869e-04
## Rating 2.740066e-03 3.619004e-03 4.778529e-03 6.307210e-03
## Cards 2.555863e-02 3.377236e-02 4.461951e-02 5.893978e-02
## Age -2.473534e-03 -3.271815e-03 -4.328536e-03 -5.727955e-03
## Education -4.926424e-03 -6.503888e-03 -8.582847e-03 -1.132004e-02
## GenderFemale -1.389762e-02 -1.835061e-02 -2.422143e-02 -3.195483e-02
## StudentYes 3.995165e-01 5.280017e-01 6.977501e-01 9.219703e-01
## MarriedYes 4.967904e-02 6.556658e-02 8.649010e-02 1.140126e-01
## EthnicityAsian 4.596048e-03 6.124798e-03 8.182117e-03 1.096500e-02
## EthnicityCaucasian 8.883955e-03 1.172048e-02 1.545270e-02 2.035611e-02
##
## (Intercept) 5.164026e+02 5.143419e+02 5.116371e+02 508.094791894
## Income 2.160432e-02 2.844640e-02 3.740799e-02 0.049111002
## Limit 5.577654e-04 7.353617e-04 9.686833e-04 0.001274617
## Rating 8.320832e-03 1.097022e-02 1.445092e-02 0.019014808
## Cards 7.783717e-02 1.027607e-01 1.356079e-01 0.178855726
## Age -7.582215e-03 -1.004086e-02 -1.330375e-02 -0.017638720
## Education -1.491923e-02 -1.964380e-02 -2.583169e-02 -0.033912150
## GenderFemale -4.213017e-02 -5.549848e-02 -7.302699e-02 -0.095950647
## StudentYes 1.218066e+00 1.608947e+00 2.124722e+00 2.804893556
## MarriedYes 1.501576e-01 1.975264e-01 2.594321e-01 0.340039665
## EthnicityAsian 1.475342e-02 1.995100e-02 2.714850e-02 0.037223414
## EthnicityCaucasian 2.678550e-02 3.519376e-02 4.615224e-02 0.060369900
##
## (Intercept) 503.469400311 497.452863985 489.665753600 479.651841171
## Income 0.064334709 0.084037127 0.109364915 0.141638122
## Limit 0.001674736 0.002196298 0.002873234 0.003746956
## Rating 0.024983711 0.032764192 0.042862350 0.055895809
## Cards 0.235725341 0.310382890 0.408179713 0.535926612
## Age -0.023405756 -0.031090180 -0.041348088 -0.055067947
## Education -0.044422980 -0.058025314 -0.075510582 -0.097790428
## GenderFemale -0.125827589 -0.164592297 -0.214593282 -0.278592631
## StudentYes 3.701152237 4.880908624 6.431668441 8.466312791
## MarriedYes 0.444493136 0.578988509 0.750723159 0.967610414
## EthnicityAsian 0.051497254 0.071982912 0.101770531 0.145618799
## EthnicityCaucasian 0.078706655 0.102172083 0.131895790 0.169049704
##
## (Intercept) 466.88055639 450.763313902 430.691086047 406.10006214
## Income 0.18228858 0.232721866 0.294065688 0.36676837
## Limit 0.00486668 0.006288813 0.008074843 0.01028714
## Rating 0.07259845 0.093811179 0.120450224 0.15344419
## Cards 0.70218739 0.917558464 1.194873442 1.54923237
## Age -0.07345235 -0.098122207 -0.131240024 -0.17563581
## Education -0.12585543 -0.160682925 -0.203070496 -0.25337434
## GenderFemale -0.35969091 -0.461124568 -0.585868729 -0.73597807
## StudentYes 11.12921362 14.602853216 19.114141192 24.93884134
## MarriedYes 1.23759813 1.567387716 1.960352675 2.41357172
## EthnicityAsian 0.21083531 0.308530366 0.455285156 0.67514083
## EthnicityCaucasian 0.21469847 0.269558170 0.333667730 0.40603978
##
## (Intercept) 376.56761536 341.92993960 302.39578619 258.6134816
## Income 0.45002995 0.54109583 0.63452055 0.7215979
## Limit 0.01298239 0.01620298 0.01996797 0.0242664
## Rating 0.19363518 0.24164953 0.29776100 0.3617881
## Cards 1.99770541 2.55850692 3.24939401 4.0850470
## Age -0.23489496 -0.31333050 -0.41572075 -0.5466723
## Education -0.31114833 -0.37471744 -0.44078005 -0.5042014
## GenderFemale -0.91163238 -1.10994979 -1.32381045 -1.5411847
## StudentYes 32.40138066 41.86591897 53.71338511 68.2993037
## MarriedYes 2.91421106 3.43606930 3.93783452 4.3650806
## EthnicityAsian 1.00153466 1.47835906 2.15879215 3.1002479
## EthnicityCaucasian 0.48447269 0.56586167 0.64746021 0.7294282
##
## (Intercept) 211.64350928 162.81034928 113.45644052 64.67633424
## Income 0.79020373 0.82525285 0.80981827 0.72676801
## Limit 0.02905629 0.03427161 0.03983641 0.04568171
## Rating 0.43307532 0.51059148 0.59313488 0.67957974
## Cards 5.07329656 6.21035922 7.47580157 8.82871192
## Age -0.70951318 -0.90477519 -1.12858534 -1.37155352
## Education -0.55819750 -0.59506753 -0.60748478 -0.59013169
## GenderFemale -1.74569853 -1.91921567 -2.04682946 -2.12366056
## StudentYes 85.89026197 106.58419851 130.23076416 156.37842383
## MarriedYes 4.65753015 4.76106279 4.64078984 4.28926497
## EthnicityAsian 4.35422034 5.95134704 7.88447809 10.09475335
## EthnicityCaucasian 0.81842342 0.93090017 1.09363097 1.33881177
##
## (Intercept) 17.13317739 -28.97020938 -73.76263660 -117.4826037
## Income 0.56066435 0.29969888 -0.06245305 -0.5249855
## Limit 0.05175867 0.05804221 0.06452317 0.0711915
## Rating 0.76906783 0.86106173 0.95522819 1.0511851
## Cards 10.20716911 11.53294010 12.72194056 13.6986190
## Age -1.61882906 -1.85174205 -2.05087082 -2.1997431
## Education -0.54124038 -0.46348895 -0.36380970 -0.2520265
## GenderFemale -2.16140680 -2.19142273 -2.26136333 -2.4247950
## StudentYes 184.27689611 212.95040629 241.33099249 268.4148685
## MarriedYes 3.72506427 2.98206382 2.09561034 1.0936300
## EthnicityAsian 12.46621693 14.83271821 16.99768242 18.7640248
## EthnicityCaucasian 1.69292106 2.16209929 2.72031250 3.3072998
##
## (Intercept) -1.602542e+02 -201.94044351 -242.11050372 -280.1156689
## Income -1.078733e+00 -1.70590387 -2.38123611 -3.0748195
## Limit 7.801606e-02 0.08493006 0.09182842 0.0985792
## Rating 1.148201e+00 1.24496081 1.33949261 1.4293248
## Cards 1.441032e+01 14.83709090 14.99389976 14.9248928
## Age -2.288060e+00 -2.31346310 -2.28141479 -2.2033869
## Education -1.387272e-01 -0.03311597 0.05844770 0.1328966
## GenderFemale -2.726645e+00 -3.18998773 -3.80925091 -4.5521562
## StudentYes 2.933949e+02 315.73522792 335.18045447 351.7149794
## MarriedYes -3.168308e-03 -1.17443909 -2.39289348 -3.6217193
## EthnicityAsian 1.996891e+01 20.51523340 20.39093032 19.6686637
## EthnicityCaucasian 3.839908e+00 4.23393513 4.42791224 4.3988313
##
## (Intercept) -315.2368744 -346.8439344 -374.5149752 -398.0875172
## Income -3.7561546 -4.3983478 -4.9812793 -5.4929948
## Limit 0.1050486 0.1111296 0.1167654 0.1219604
## Rating 1.5118004 1.5844450 1.6452532 1.6928135
## Cards 14.6921369 14.3627849 13.9983587 13.6483123
## Age -2.0940381 -1.9682827 -1.8390012 -1.7157849
## Education 0.1900106 0.2314802 0.2599028 0.2779946
## GenderFemale -5.3689132 -6.2045295 -7.0098262 -7.7482846
## StudentYes 365.4983900 376.7988531 385.9373678 393.2470193
## MarriedYes -4.8181078 -5.9402927 -6.9544162 -7.8385749
## EthnicityAsian 18.4851804 17.0076828 15.3990350 13.7922359
## EthnicityCaucasian 4.1639778 3.7701931 3.2770772 2.7414908
##
## (Intercept) -417.6434374 -433.4493709 -445.8819494 -455.3616546
## Income -5.9293441 -6.2923790 -6.5882918 -6.8254359
## Limit 0.1267812 0.1313466 0.1358140 0.1403559
## Rating 1.7262381 1.7449944 1.7486919 1.7370415
## Cards 13.3481958 13.1210925 12.9807414 12.9342321
## Age -1.6046839 -1.5086699 -1.4284262 -1.3631736
## Education 0.2881094 0.2920377 0.2909894 0.2857056
## GenderFemale -8.3981803 -8.9510625 -9.4084024 -9.7778631
## StudentYes 399.0465191 403.6249269 407.2346694 410.0893992
## MarriedYes -8.5833859 -9.1898662 -9.6661090 -10.0240866
## EthnicityAsian 12.2783476 10.9067162 9.6925015 8.6276071
## EthnicityCaucasian 2.2076856 1.7038910 1.2434906 0.8288095
##
## (Intercept) -462.2959086 -467.0596673 -469.9804185 -471.3434530
## Income -7.0129363 -7.1595021 -7.2729922 -7.3601552
## Limit 0.1451790 0.1504607 0.1563595 0.1629865
## Rating 1.7093063 1.6650032 1.6036809 1.5253060
## Cards 12.9868594 13.1404570 13.3955403 13.7500872
## Age -1.3113295 -1.2710238 -1.2403745 -1.2176479
## Education 0.2764700 0.2633941 0.2464595 0.2256720
## GenderFemale -10.0703223 -10.2974200 -10.4703615 -10.5992078
## StudentYes 412.3681381 414.2153184 415.7465693 417.0516586
## MarriedYes -10.2766724 -10.4365687 -10.5153607 -10.5236520
## EthnicityAsian 7.6878025 6.8442739 6.0671506 5.3302059
## EthnicityCaucasian 0.4539148 0.1097583 -0.2140888 -0.5271422
##
## (Intercept) -471.4046307 -470.4056800 -468.5861383 -466.1871216
## Income -7.4266047 -7.4769107 -7.5147396 -7.5430035
## Limit 0.1703806 0.1784883 0.1871540 0.1961299
## Rating 1.4306324 1.3215405 1.2012138 1.0740279
## Cards 14.1980569 14.7278576 15.3215202 15.9552746
## Age -1.2013180 -1.1900693 -1.1827747 -1.1784704
## Education 0.2011918 0.1734391 0.1431501 0.1113559
## GenderFemale -10.6926627 -10.7581235 -10.8018431 -10.8291011
## StudentYes 418.1972965 419.2295545 420.1763934 421.0508555
## MarriedYes -10.4716072 -10.3696407 -10.2289219 -10.0614200
## EthnicityAsian 4.6138226 3.9068639 3.2073065 2.5213777
## EthnicityCaucasian -0.8364943 -1.1458249 -1.4550027 -1.7603941
##
## (Intercept) -463.44517661 -460.57820837 -457.76947927 -4.551554e+02
## Income -7.56399711 -7.57951417 -7.59094266 -7.599344e+00
## Limit 0.20510788 0.21376693 0.22182316 2.290683e-01
## Rating 0.94511416 0.81967560 0.70226795 5.962435e-01
## Cards 16.60175822 17.23347607 17.82643614 1.836296e+01
## Age -1.17633791 -1.17569677 -1.17600185 -1.176839e+00
## Education 0.07927825 0.04816252 0.01910104 -7.103813e-03
## GenderFemale -10.84432913 -10.85117976 -10.85256488 -1.085070e+01
## StudentYes 421.85514551 422.58533508 423.23584341 4.238029e+02
## MarriedYes -9.87938730 -9.69439713 -9.51629255 -9.352381e+00
## EthnicityAsian 1.86120268 1.24138937 0.67551166 1.733514e-01
## EthnicityCaucasian -2.05587088 -2.33432017 -2.58921139 -2.815827e+00
##
## (Intercept) -452.82215402 -450.80956171 -449.12140740 -447.73687344
## Income -7.60551795 -7.61006096 -7.61341151 -7.61588999
## Limit 0.23538529 0.24074570 0.24518999 0.24880452
## Rating 0.50353250 0.42469866 0.35924097 0.30594705
## Cards 18.83274853 19.23261034 19.56485784 19.83550245
## Age -1.17791513 -1.17903723 -1.18009266 -1.18102517
## Education -0.02999414 -0.04944364 -0.06558431 -0.07872043
## GenderFemale -10.84715535 -10.84299431 -10.83885294 -10.83507501
## StudentYes 424.28635187 424.68995212 425.02061562 425.28719835
## MarriedYes -9.20717425 -9.08256929 -8.97843097 -8.89324472
## EthnicityAsian -0.26033953 -0.62584668 -0.92738583 -1.17173643
## EthnicityCaucasian -3.01180658 -3.17713482 -3.31362478 -3.42428578
##
## (Intercept) -446.62182222 -445.7363891 -445.0411859 -444.5000111
## Income -7.61772928 -7.6190986 -7.6201211 -7.6208865
## Limit 0.25169780 0.2539850 0.2557748 0.2571647
## Rating 0.26325368 0.2294843 0.2030464 0.1825104
## Cards 20.05239472 20.2239984 20.3583731 20.4627669
## Age -1.18181513 -1.1824651 -1.1829885 -1.1834035
## Education -0.08924065 -0.0975601 -0.1040723 -0.1091302
## GenderFemale -10.83181042 -10.8290890 -10.8268776 -10.8251130
## StudentYes 425.49921368 425.6660105 425.7960716 425.8967952
## MarriedYes -8.82476816 -8.7704680 -8.7278772 -8.6947481
## EthnicityAsian -1.36680701 -1.5207072 -1.6409646 -1.7342434
## EthnicityCaucasian -3.51266252 -3.5824067 -3.6369162 -3.6792036
##
## (Intercept) -444.0816709 -443.7598713 -443.5133837 -443.3251069
## Income -7.6214608 -7.6218924 -7.6222172 -7.6224620
## Limit 0.2582371 0.2590608 0.2596912 0.2601723
## Rating 0.1666609 0.1544837 0.1451647 0.1380513
## Cards 20.5433461 20.6052606 20.6526456 20.6888172
## Age -1.1837286 -1.1839813 -1.1841762 -1.1843260
## Education -0.1130334 -0.1160321 -0.1183268 -0.1200783
## GenderFemale -10.8237241 -10.8226415 -10.8218040 -10.8211596
## StudentYes 425.9743588 426.0338498 426.0793199 426.1139945
## MarriedYes -8.6691528 -8.6494725 -8.6344028 -8.6228948
## EthnicityAsian -1.8061584 -1.8613664 -1.9035908 -1.9358067
## EthnicityCaucasian -3.7118096 -3.7368430 -3.7559904 -3.7706000
In the computations above we used an arbitrary value for \(\lambda\). We can use cross-validation to choose the tuning parameter. The function cv.glmnet() can be used to perform k-fold cross-validation (default is 10-fold)
set.seed(1)
cv.out <- cv.glmnet(x[train,], y[train], alpha=0, lambda=grid)
plot(cv.out)bestlam <- cv.out$lambda.min
bestlam ## [1] 0.01
The value of the tuning parameter that gives the smallest cross-validation error is \(\lambda=0.01\). The test MSE associated with that value is
ridge.pred <- predict(ridge.mod, s=bestlam, newx=x[test,])
mean((ridge.pred - y.test)^2) ## [1] 10679.98
The final model:
out <- glmnet(x, y, alpha=0)
predict(out, type="coefficients", s=bestlam)[1:12,] ## (Intercept) Income Limit Rating
## -400.8600617 -5.1751854 0.1144898 1.6607396
## Cards Age Education GenderFemale
## 15.8061272 -0.9565674 -0.4740526 -4.8551984
## StudentYes MarriedYes EthnicityAsian EthnicityCaucasian
## 381.6895970 -12.0976162 13.1298333 9.1146561
Note that none of the coefficients are zero. The ridge regression does not perform variable selection.
The function glmnet() performs the LASSO when we set alpha=1.
lasso.mod <- glmnet(x[train,], y[train], alpha=1, lambda=grid)
# plot(lasso.mod) Perform cross-validation:
set.seed(1)
cv.out <- cv.glmnet(x[train,], y[train], alpha=1, lambda=grid)
plot(cv.out)bestlam <- cv.out$lambda.min
bestlam## [1] 1.519911
lasso.pred <- predict(lasso.mod, s=bestlam, newx=x[test,])
mean((lasso.pred - y.test)^2) ## [1] 10518.35
The resulting model is
# out <- glmnet(x,y,alpha=1,lambda=grid)
out <- glmnet(x,y,alpha=1)
lasso.coef <- predict(out, type="coefficients", s=bestlam)[1:12,]
lasso.coef ## (Intercept) Income Limit Rating
## -483.4343080 -7.6007750 0.1711165 1.3839377
## Cards Age Education GenderFemale
## 15.6017715 -0.5638059 -0.5551434 -7.1021432
## StudentYes MarriedYes EthnicityAsian EthnicityCaucasian
## 419.0895716 -5.6051725 8.3482838 2.7633677
None of the coefficients are zero:
lasso.coef[lasso.coef!=0] ## (Intercept) Income Limit Rating
## -483.4343080 -7.6007750 0.1711165 1.3839377
## Cards Age Education GenderFemale
## 15.6017715 -0.5638059 -0.5551434 -7.1021432
## StudentYes MarriedYes EthnicityAsian EthnicityCaucasian
## 419.0895716 -5.6051725 8.3482838 2.7633677
Note that for sufficiently large lambda, the LASSO can perform variable selection. Consider for example:
out <- glmnet(x,y,alpha=1)
lasso.coef <- predict(out, type="coefficients", s=10)[1:12,]
lasso.coef ## (Intercept) Income Limit Rating
## -469.8929099 -6.4884298 0.1278235 1.7706408
## Cards Age Education GenderFemale
## 8.1430126 -0.2372500 0.0000000 0.0000000
## StudentYes MarriedYes EthnicityAsian EthnicityCaucasian
## 386.3920701 0.0000000 0.0000000 0.0000000
In that case, the final model would be
lasso.coef[lasso.coef!=0] ## (Intercept) Income Limit Rating Cards Age
## -469.8929099 -6.4884298 0.1278235 1.7706408 8.1430126 -0.2372500
## StudentYes
## 386.3920701
This example is partly based on the exercise 8 on p.262 of the ISLR text. We will first simulate a data set in which true relationship is a cubic polynomial. Nevertheless, the data set contains powers of x up to 10 degree. See the simulation part below. We will then perform variable selection procedures.
set.seed(99) # for replication
n <- 400
x <- rnorm(n, mean=0, sd=1)
eps <- rnorm(n, mean=0, sd=1)
f <- 5 +1*x - 2*x^2 - 1*x^3
y <- f + eps
df <- data.frame(y, x1=x, x2=x^2, x3=x^3, x4=x^4, x5=x^5, x6=x^6,
x7=x^7, x8=x^8, x9=x^9, x10=x^10)
# df <- data.frame(y, x1=x, x2=x^2, x3=x^3, x4=x^4, x5=x^5, x6=rnorm(n),
# x7=rnorm(n), x8=rnorm(n), x9=rnorm(n), x10=rnorm(n),
# x11=runif(n), x12=runif(n), x13=runif(n), x14=runif(n),
# x15=runif(n))
head(df) ## y x1 x2 x3 x4 x5
## 1 5.817430 0.2139625 0.045779952 0.0097951932 2.095804e-03 4.484235e-04
## 2 5.643896 0.4796581 0.230071926 0.1103558709 5.293309e-02 2.538979e-02
## 3 4.927971 0.0878287 0.007713881 0.0006775002 5.950397e-05 5.226156e-06
## 4 7.489333 0.4438585 0.197010375 0.0874447309 3.881309e-02 1.722752e-02
## 5 4.195846 -0.3628379 0.131651357 -0.0477681045 1.733208e-02 -6.288736e-03
## 6 6.260835 0.1226740 0.015048918 0.0018461114 2.264699e-04 2.778198e-05
## x6 x7 x8 x9 x10
## 1 9.594581e-05 2.052881e-05 4.392395e-06 9.398077e-07 2.010836e-07
## 2 1.217842e-02 5.841477e-03 2.801912e-03 1.343960e-03 6.446413e-04
## 3 4.590065e-07 4.031395e-08 3.540722e-09 3.109770e-10 2.731271e-11
## 4 7.646581e-03 3.394000e-03 1.506456e-03 6.686532e-04 2.967874e-04
## 5 2.281792e-03 -8.279206e-04 3.004010e-04 -1.089969e-04 3.954820e-05
## 6 3.408127e-06 4.180887e-07 5.128862e-08 6.291782e-09 7.718383e-10
library(ggplot2)
ggplot(df, aes(x=x1,y=y))+
geom_point() +
geom_line(aes(x1,f))library(leaps)
regfit.full <- regsubsets(y ~ ., data = df, nvmax = 10)
#summary(regfit.full) reg.summary <- summary(regfit.full)
# 2x2 plot grid
par(mfrow=c(2,2))
# plot RSS
plot(reg.summary$rss, xlab="Number of Variables", xaxt="n", ylab="RSS", type="o")
axis(1, at = seq(1, 10, by = 1),las=2)
# plot Adj.R2
plot(reg.summary$adjr2, xlab="Number of Variables", xaxt="n", ylab="Adjusted RSq",type="o")
axis(1, at = seq(1, 10, by = 1),las=2)
# find the model with max Adj.R2
best <- which.max(reg.summary$adjr2)
# put red dot on the graph that marks the best according to Adj.R2
points(best, reg.summary$adjr2[best], col="red", cex=2, pch=20)
# plot Cp
plot(reg.summary$cp, xlab="Number of Variables", xaxt="n", ylab="Cp",type='o')
axis(1, at = seq(1, 10, by = 1),las=2)
# find the best according to Cp
bestCp <- which.min(reg.summary$cp)
points(bestCp, reg.summary$cp[bestCp], col="red", cex=2, pch=20)
# plot BICs
plot(reg.summary$bic, xlab="Number of Variables", xaxt="n", ylab="BIC", type='o')
bestBIC <- which.min(reg.summary$bic)
points(bestBIC, reg.summary$bic[bestBIC], col="red", cex=2, pch=20)
axis(1, at = seq(1, 10, by = 1),las=2)regfit.fwd <- regsubsets(y ~ ., data = df, nvmax=10, method="forward")
reg.fwd.summary <- summary(regfit.fwd)
reg.fwd.summary## Subset selection object
## Call: regsubsets.formula(y ~ ., data = df, nvmax = 10, method = "forward")
## 10 Variables (and intercept)
## Forced in Forced out
## x1 FALSE FALSE
## x2 FALSE FALSE
## x3 FALSE FALSE
## x4 FALSE FALSE
## x5 FALSE FALSE
## x6 FALSE FALSE
## x7 FALSE FALSE
## x8 FALSE FALSE
## x9 FALSE FALSE
## x10 FALSE FALSE
## 1 subsets of each size up to 10
## Selection Algorithm: forward
## x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
## 1 ( 1 ) " " " " "*" " " " " " " " " " " " " " "
## 2 ( 1 ) " " "*" "*" " " " " " " " " " " " " " "
## 3 ( 1 ) "*" "*" "*" " " " " " " " " " " " " " "
## 4 ( 1 ) "*" "*" "*" " " "*" " " " " " " " " " "
## 5 ( 1 ) "*" "*" "*" " " "*" " " " " " " "*" " "
## 6 ( 1 ) "*" "*" "*" " " "*" " " "*" " " "*" " "
## 7 ( 1 ) "*" "*" "*" "*" "*" " " "*" " " "*" " "
## 8 ( 1 ) "*" "*" "*" "*" "*" "*" "*" " " "*" " "
## 9 ( 1 ) "*" "*" "*" "*" "*" "*" "*" " " "*" "*"
## 10 ( 1 ) "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
regfit.bwd <- regsubsets(y ~ ., data = df, nvmax=10, method="backward")
reg.bwd.summary <- summary(regfit.bwd)
reg.bwd.summary## Subset selection object
## Call: regsubsets.formula(y ~ ., data = df, nvmax = 10, method = "backward")
## 10 Variables (and intercept)
## Forced in Forced out
## x1 FALSE FALSE
## x2 FALSE FALSE
## x3 FALSE FALSE
## x4 FALSE FALSE
## x5 FALSE FALSE
## x6 FALSE FALSE
## x7 FALSE FALSE
## x8 FALSE FALSE
## x9 FALSE FALSE
## x10 FALSE FALSE
## 1 subsets of each size up to 10
## Selection Algorithm: backward
## x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
## 1 ( 1 ) " " " " "*" " " " " " " " " " " " " " "
## 2 ( 1 ) " " "*" "*" " " " " " " " " " " " " " "
## 3 ( 1 ) "*" "*" "*" " " " " " " " " " " " " " "
## 4 ( 1 ) "*" "*" "*" " " " " " " " " " " "*" " "
## 5 ( 1 ) "*" "*" "*" " " " " " " " " " " "*" "*"
## 6 ( 1 ) "*" "*" "*" " " " " " " " " "*" "*" "*"
## 7 ( 1 ) "*" "*" "*" " " " " "*" " " "*" "*" "*"
## 8 ( 1 ) "*" "*" "*" " " " " "*" "*" "*" "*" "*"
## 9 ( 1 ) "*" "*" "*" " " "*" "*" "*" "*" "*" "*"
## 10 ( 1 ) "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
coef(regfit.full, 3)## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.fwd, 3) ## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.bwd, 3)## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
Which model is the best? Using the Cp criterion, the best model is
coef(regfit.full, which.min(reg.summary$cp))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.fwd, which.min(reg.fwd.summary$cp)) ## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.bwd, which.min(reg.bwd.summary$cp))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
According to the BIC criterion:
coef(regfit.full, which.min(reg.summary$bic))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.fwd, which.min(reg.fwd.summary$bic)) ## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.bwd, which.min(reg.bwd.summary$bic))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
According to the adj.Rsq criterion:
coef(regfit.full, which.max(reg.summary$adjr2))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.fwd, which.max(reg.fwd.summary$adjr2)) ## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
coef(regfit.bwd, which.max(reg.bwd.summary$adjr2))## (Intercept) x1 x2 x3
## 5.007799 1.051778 -1.996604 -1.008776
x <- model.matrix(y ~ ., df)[,-1]
y <- df$y set.seed(666)
train <- sample(1:nrow(x), nrow(x)/2)
test <- (-train)
y.test <- y[test] library(glmnet)
lasso.mod <- glmnet(x[train,], y[train], alpha=1)
plot(lasso.mod) Perform cross-validation to choose lambda:
set.seed(1)
cv.out <- cv.glmnet(x[train,], y[train], alpha=1)
plot(cv.out)bestlam <- cv.out$lambda.min
bestlam## [1] 0.00817102
lasso.pred <- predict(lasso.mod, s=bestlam, newx=x[test,])
mean((lasso.pred - y.test)^2) ## [1] 0.9984801
The variables x4-x10 are all zeros:
out <- glmnet(x, y, alpha=1)
lasso.coef <- predict(out, type="coefficients", s=bestlam)[1:11,]
lasso.coef ## (Intercept) x1 x2 x3 x4 x5
## 4.9951622 1.0081146 -1.9844867 -0.9959831 0.0000000 0.0000000
## x6 x7 x8 x9 x10
## 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# sprintf("%.5f",lasso.coef[lasso.coef!=0])
lasso.coef[abs(lasso.coef)>0.0001]## (Intercept) x1 x2 x3
## 4.9951622 1.0081146 -1.9844867 -0.9959831
Set the mixing parameter \(\alpha=0.5\) and use glmnet:
set.seed(1)
cv.out <- cv.glmnet(x[train,], y[train], alpha=0.5)
plot(cv.out)bestlam <- cv.out$lambda.min
bestlam## [1] 0.01236215
elnet.mod <- glmnet(x[train,], y[train], alpha=0.5)
plot(elnet.mod)elnet.pred <- predict(lasso.mod, s=bestlam, newx=x[test,])
mean((elnet.pred - y.test)^2) ## [1] 0.9784411
Which coefficients are set to zero?
out <- glmnet(x, y, alpha=1)
lasso.coef <- predict(out, type="coefficients", s=bestlam)[1:11,]
lasso.coef ## (Intercept) x1 x2 x3 x4 x5
## 4.9888401 0.9864748 -1.9784137 -0.9895979 0.0000000 0.0000000
## x6 x7 x8 x9 x10
## 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
MSE <- numeric(11)
lambda <- rep(NA, 11)
alpha <- (0:10)/10
set.seed(1)
for (i in 1:11) {
fit <- cv.glmnet(x[train,], y[train], type.measure="mse",
alpha=alpha[i], family="gaussian")
yhat <- predict(fit, s=fit$lambda.1se, newx=x[test,])
lambda[i] <- fit$lambda.1se
MSE[i] <- mean((y[test] - yhat)^2)
}For each \(\alpha\) value we have chosen \(\lambda\) using cross validation. Here is the result:
cbind(alpha, lambda, MSE)## alpha lambda MSE
## [1,] 0.0 2.93651234 3.1240702
## [2,] 0.1 0.02024024 1.1595783
## [3,] 0.2 0.01218972 1.1203282
## [4,] 0.3 0.03600536 1.0688107
## [5,] 0.4 0.02700402 1.0512317
## [6,] 0.5 0.02855818 1.0239753
## [7,] 0.6 0.02866536 1.0028133
## [8,] 0.7 0.03564731 0.9893176
## [9,] 0.8 0.03757014 0.9819981
## [10,] 0.9 0.03042890 0.9715583
## [11,] 1.0 0.03005611 0.9692169
The minimum MSE is achieved when alpha=1 which corresponds to the LASSO.
In this application we will use the Barro-Lee (1994) growth data set which is available as part of the hdm package by Belloni et al., hdm: High-Dimensional Metrics
# install.packages("hdm")
library(hdm)The data set is available as
data("GrowthData")There are \(n=90\) countries and \(p=60\) predictors in the data set including many macroeconomic indicators.
The standard income convergence model can be written as \[Y_{i,T}=\beta_{0}+\beta_{1} Y_{i,0}+\sum_{j=2}^{p} \beta_{j} X_{i j}+\varepsilon_{i},\quad i=1,\dots,n,\] where \(Y_{i,T}\) is the growth rate of GDP per capita over the periods 1965-1975 and 1975-1985. \(Y_{i,0}\) is the natural logarithm of the initial GDP of country \(i\). \(X_{i j}\) represent factors that may affect the growth performance of countries. Because there are many potentially important variables, the selection becomes especially important. The convergence hypothesis states that \(\beta_1<0\).
# standard lasso using glmnet
y <- GrowthData$Outcome
x <- model.matrix(Outcome ~ . -intercept, data=GrowthData)[,-1]
library(glmnet)
lasso_fit1 <- glmnet(x, y, alpha=1)
plot(lasso_fit1)cv.out <- cv.glmnet(x, y, alpha=1)
plot(cv.out)bestlam <- cv.out$lambda.min
bestlam## [1] 3.050841e-05
out <- glmnet(x, y, alpha=1, lambda=bestlam)
coefficients(out)## 62 x 1 sparse Matrix of class "dgCMatrix"
## s0
## (Intercept) 9.502145e-01
## gdpsh465 -2.251857e-03
## bmp1l -5.751675e-02
## freeop 1.039681e-01
## freetar -3.447043e-01
## h65 -1.893088e+00
## hm65 8.963261e-01
## hf65 1.056803e+00
## p65 3.355455e-01
## pm65 9.431682e-02
## pf65 -2.733082e-01
## s65 5.625479e-02
## sm65 -7.487775e-02
## sf65 -1.441654e-01
## fert65 -1.094875e-02
## mort65 2.394424e-01
## lifee065 -1.776998e-01
## gpop1 -3.934365e-01
## fert1 1.426031e-02
## mort1 -9.507152e-01
## invsh41 9.337134e-03
## geetot1 3.256478e-01
## geerec1 .
## gde1 2.438063e-01
## govwb1 1.223166e-01
## govsh41 -1.753571e-01
## gvxdxe41 .
## high65 .
## highm65 6.401939e-04
## highf65 -4.512413e-03
## highc65 .
## highcm65 1.425886e-02
## highcf65 -1.840872e-02
## human65 .
## humanm65 -6.829672e-03
## humanf65 1.246143e-02
## hyr65 .
## hyrm65 4.492256e-02
## hyrf65 -1.528539e-01
## no65 -4.433035e-04
## nom65 -2.053851e-04
## nof65 .
## pinstab1 -2.654122e-02
## pop65 5.918999e-08
## worker65 4.519079e-02
## pop1565 -3.256893e-01
## pop6565 -1.384587e+00
## sec65 .
## secm65 -5.741042e-03
## secf65 2.352343e-03
## secc65 .
## seccm65 1.381822e-02
## seccf65 -1.764522e-02
## syr65 .
## syrm65 .
## syrf65 1.030268e-01
## teapri65 -4.492751e-04
## teasec65 -9.301841e-06
## ex1 -5.744466e-01
## im1 5.363024e-01
## xr65 -4.030031e-05
## tot1 -6.570651e-02
From the output we see that the initial income is not included in the final model. We want to keep it in the model but also want to apply variable selection.
The Post-Double Selection LASSO procedure by Belloni, Chernozhukov, and Hansen attempts to solve this problem. The algorithm works as follows (for further details see How to do model selection with inference in mind)
Step 1: Use LASSO (or other shrinkage methods) to select \(x_j\) for predicting \(y_T\).
Step 2: Use LASSO (or other shrinkage methods) to select \(x_j\) for predicting \(y_0\).
Step 3: Refit the model by OLS after selection (use either \(x_j\) selected in both steps)
# prepare data
library(hdm)
yT <- GrowthData$Outcome
y0 <- GrowthData$gdpsh465
x <- model.matrix(Outcome ~ . -intercept -gdpsh465 , data=GrowthData)[,-1]double_lasso_fit <- rlassoEffect(x = x, y = yT, d = y0,
method = "double selection")
summary(double_lasso_fit)## [1] "Estimates and significance testing of the effect of target variables"
## Estimate. Std. Error t value Pr(>|t|)
## d1 -0.05001 0.01579 -3.167 0.00154 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Selected coefficients
double_lasso_fit$coefficients.reg## (Intercept) xd1 xbmp1l xfreetar xhm65
## -0.4064512995 -0.0500058545 -0.0782423426 -0.5746764184 0.0511528988
## xsf65 xlifee065 xhumanf65 xpop6565
## -0.0470218317 0.2122793760 -0.0003760384 0.1343892586
Now the coefficient estimate on the initial income \(Y_0\) is \(\hat{\beta}_1=-0.05\) with heteroskedasticity-robust standard error 0.016. This result supports the neoclassical convergence argument.
References