Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions