Answered step by step
Verified Expert Solution
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 ISLR 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 kfold 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 andor graphical Estimate the
models using training and test data split Evaluate which model or set of models performs well. Does your chosen models involve all the features in the data set? Why or why not?
Code from scartch and each part:
r
#Loading the library
libraryISLR
r
#Loading Boston Dataset
dataBoston
r
#Summary of the Boston Dataset
summaryBoston
r
# Creating random numbers that can be reproduced
set.seed
r
# Split data into training and test set split
trainindex sample:nrowBoston nrowBoston
traindata Boston trainindex,
testdata Bostontrainindex,
r
#Best Subset Selection
libraryleaps
bestsubset regsubsetscrim ~ data traindata, nvmax
summarybestsubset
r
#Backward Subset Selection
backwardsubset regsubsetscrim ~ data traindata, method "backward"
summarybackwardsubset
r
#LASSO Regression
libraryglmnet
x asmatrixtraindata # excluding the crim
y traindata$crim
lassomodel cvglmnet x y alpha
plotlassomodel
r
#Ridge Regression
ridgemodel cvglmnetx y alpha
plotridgemodel
r
#Predict Using each model
testx asmatrixtestdata
testy testdata$crim
COMPLETE THE CODE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started