Question: Use the bBoston dataset from the ISLR 2 library ( R Studio ) to predict per capita crime rate using the regression methods you learnt

Use the bBoston dataset from the ISLR2 library (R Studio) to predict per capita crime rate using the regression methods you learnt in the class particularly best subsets selection, backward subset selection, lasso, and ridge regression. For your ridge and lasso, be sure to use k-fold cross validation as we covered in the lab but please choose your k and explain why youve chosen this number.Assume you are submitting this analysis to someone who is seeing this data for the first time. Please provide an overview of what each variable represents and include summary statistics for
both continuous and categorical variables (tabular and/or graphical). Estimate the
models using training and test data (80-20 split). Evaluate which model (or set of models) performs well. Does your chosen model(s) involve all the features in the data set? Why or why not?
Code from scartch and each part:
```{r}
#Loading the library
library(ISLR2)
```
```{r}
#Loading Boston Dataset
data("Boston")
```
```{r}
#Summary of the Boston Dataset
summary(Boston)
```
```{r}
# Creating random numbers that can be reproduced
set.seed(123)
```
```{r}
# Split data into training and test set (80-20 split)
train_index <- sample(1:nrow(Boston),0.8* nrow(Boston))
train_data <- Boston [train_index,]
test_data <- Boston[-train_index,]
```
```{r}
#Best Subset Selection
library(leaps)
best_subset <- regsubsets(crim ~ ., data = train_data, nvmax =13)
summary(best_subset)
```
```{r}
#Backward Subset Selection
backward_subset <- regsubsets(crim ~ ., data = train_data, method = "backward")
summary(backward_subset)
```
```{r}
#LASSO Regression
library(glmnet)
x <- as.matrix(train_data[,-1]) # excluding the crim
y <- train_data$crim
lasso_model <- cv.glmnet (x, y, alpha =1)
plot(lasso_model)
```
```{r}
#Ridge Regression
ridge_model <- cv.glmnet(x, y, alpha =0)
plot(ridge_model)
```
```{r}
#Predict Using each model
test_x <- as.matrix(test_data[,-1])
test_y <- test_data$crim
```
COMPLETE THE CODE

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!