library(ISLR)
library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 4.1-8
##2. For parts (a) through (c), indicate which of i. through iv. is correct. Justify your answer.
The correct answer is iii. The lasso method introduces a penalty on the size of the coefficients. The reduction in model complexity will lead to an increase in bias and a decrease in the variance.
The correct answer is iii. similar to lasso, it also introduces a penalty to the coefficients of the predictors.
The correct answer is i. Non-linear methods are more flexible than linear methods by nature.
In this exercise, we will predict the number of applications received using the other variables in the College data set.
data(College)
set.seed(123)
training_size <- round(nrow(College) * 0.75)
training_indices <- sample(seq_len(nrow(College)), size = training_size)
College_train <- College[training_indices, ]
College_test <- College[-training_indices, ]
According to my results, I got Test Mean Squared Error of 1790402.7
college_lm <- lm(Apps ~ ., data = College_train)
college_test_predictions <- predict(college_lm, College_test)
test_mse <- mean((College_test$Apps - college_test_predictions)^2)
print(paste("Test Mean Squared Error:", test_mse))
## [1] "Test Mean Squared Error: 1790402.69639585"
My Ridge Regression Test Mean Squared Error (MSE) of approximately 3,266,590.62, which is very high, It suggests that the performant might not be as effective in predicting the number of applications.
x_train <- model.matrix(Apps ~ . - 1, data = College_train) # -1 to omit intercept
y_train <- College_train$Apps
x_test <- model.matrix(Apps ~ . - 1, data = College_test)
y_test <- College_test$Apps
set.seed(123)
cv_ridge <- cv.glmnet(x_train, y_train, alpha = 0, nfolds = 10) # alpha = 0 for ridge
plot(cv_ridge)
ridge_pred <- predict(cv_ridge, s = "lambda.min", newx = x_test)
ridge_test_mse <- mean((y_test - ridge_pred)^2)
print(paste("Ridge Regression Test Mean Squared Error:", ridge_test_mse))
## [1] "Ridge Regression Test Mean Squared Error: 3266590.61524968"
According to my result, my Lasso Regression Test Mean Squared Error is 1881128.3 and Number of Non-Zero Coefficients is 15
x_train <- model.matrix(Apps ~ . - 1, data = College_train) # -1 to omit intercept
y_train <- College_train$Apps
x_test <- model.matrix(Apps ~ . - 1, data = College_test)
y_test <- College_test$Apps
set.seed(123)
cv_lasso <- cv.glmnet(x_train, y_train, alpha = 1, nfolds = 10)
plot(cv_lasso)
lasso_pred <- predict(cv_lasso, s = "lambda.min", newx = x_test)
lasso_test_mse <- mean((y_test - lasso_pred)^2)
print(paste("Lasso Regression Test Mean Squared Error:", lasso_test_mse))
## [1] "Lasso Regression Test Mean Squared Error: 1881128.30942324"
lasso_coef <- coef(cv_lasso, s = "lambda.min")
non_zero_coefs <- sum(lasso_coef != 0)
print(paste("Number of Non-Zero Coefficients:", non_zero_coefs))
## [1] "Number of Non-Zero Coefficients: 15"
according to my results here is what I found:
I can see that my linear model worked the best, then Lasso took the second place and finally ridge was the worst one.