Extract the dataset and create a new category “crime factor”

set.seed(123)
trainingind<-sample(nrow(College), 0.75*nrow(College))
head(trainingind)
## [1] 415 463 179 526 195 118

A) Train/Test Split

train_size <- nrow(College) * 0.75
inTrain <- sample(1:nrow(College), size = train_size)
train <- College[inTrain,]
test <- College[-inTrain,]
dim(College)
## [1] 777  18
dim(train)
## [1] 582  18
dim(test)
## [1] 195  18

B) Linear Model using least squares, report on the test error.

lm.fit=lm(Apps~., data=train)
summary(lm.fit)
## 
## Call:
## lm(formula = Apps ~ ., data = train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3455.4  -430.4     3.2   353.8  7461.9 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.705e+02  4.639e+02   0.368 0.713382    
## PrivateYes  -5.865e+02  1.603e+02  -3.658 0.000278 ***
## Accept       1.678e+00  4.494e-02  37.346  < 2e-16 ***
## Enroll      -8.518e-01  2.136e-01  -3.988 7.54e-05 ***
## Top10perc    5.549e+01  6.139e+00   9.040  < 2e-16 ***
## Top25perc   -1.821e+01  5.114e+00  -3.562 0.000399 ***
## F.Undergrad  6.956e-03  3.969e-02   0.175 0.860943    
## P.Undergrad  5.436e-02  3.560e-02   1.527 0.127363    
## Outstate    -7.068e-02  2.121e-02  -3.332 0.000920 ***
## Room.Board   8.548e-02  5.569e-02   1.535 0.125393    
## Books        4.811e-03  2.638e-01   0.018 0.985456    
## Personal     5.399e-02  7.718e-02   0.700 0.484509    
## PhD         -9.477e+00  5.768e+00  -1.643 0.100935    
## Terminal    -1.866e+00  6.151e+00  -0.303 0.761773    
## S.F.Ratio   -1.763e+00  1.500e+01  -0.118 0.906478    
## perc.alumni  7.212e-01  4.666e+00   0.155 0.877203    
## Expend       5.305e-02  1.328e-02   3.996 7.29e-05 ***
## Grad.Rate    8.141e+00  3.390e+00   2.402 0.016632 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1024 on 564 degrees of freedom
## Multiple R-squared:  0.9345, Adjusted R-squared:  0.9326 
## F-statistic: 473.6 on 17 and 564 DF,  p-value: < 2.2e-16

R-squared is 91.93%, this model describes that % of variance. Training Seed 1. R-squared is 93.45%, this model describes that % of variance. Training Seed 123.

Test error rate

lm.pred=predict(lm.fit, newdata=test)
lm.err = mean((test$Apps-lm.pred)^2)
lm.err
## [1] 1273695

Test error rate: 1783326. Training Seed 1. Test error rate: 1273695. Training Seed 123.

C) Fit Ridge regression model, with lambda value selected by CV, report on the test error.

library(glmnet)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Loaded glmnet 4.1-7
xtrain = model.matrix(Apps~.,data= train[,-1])
ytrain = train$Apps
xtest = model.matrix(Apps~.,data = test[,-1])
ytest = test$Apps

ridge.mod=glmnet(xtrain,ytrain,alpha=0)

cv.out=cv.glmnet(xtrain,ytrain,alpha=0)
plot(cv.out)

Selecting best lambda

bestlam =cv.out$lambda.min
bestlam
## [1] 373.1894

The best lambda at training seed 1 is 318.83, we store it and use it for prediction with the test split. The best lambda at training seed 123 is 373.1894, we store it and use it for prediction with the test split.

Prediction with best lambda for Ridge Regression

ridge.pred=predict (ridge.mod ,s=bestlam, newx=xtest)
ridge.err = mean((ridge.pred -ytest)^2)
ridge.err
## [1] 1161871

The test error obtained is 3229280 at training seed 1, it almost doubles the error rate of the linear model The test error obtained is 1161871 at training seed 123, it’s lower than the error rate of the linear model

Fit lasso model, with lambda value selected by CV, report on the test error.

lasso.mod=glmnet(xtrain,ytrain,alpha=1)
cv.out=cv.glmnet(xtrain,ytrain,alpha=1)
plot(cv.out)

Selecting best lambda

bestlam =cv.out$lambda.min
bestlam
## [1] 2.185778

The best lambda at training seed 1 is 10.93737 The best lambda at training seed 123 is 2.185778.

Lasso error rate

lasso.pred=predict (lasso.mod ,s=bestlam ,newx=xtest)
lasso.err = mean((lasso.pred -ytest)^2)
lasso.err
## [1] 1267300

The test error obtained at training seed 1 is 1868774, it almost doubles’s still higher than the error rate of the linear model The test error obtained at training seed 123 is 1267300, it’s slightly lower than the error rate of the linear model

My conclusion is this test was highly reliant on the random seed value, therefore I’m not sure if it’s valid.