question archive 1) Validation set approach

1) Validation set approach

Subject:CommunicationsPrice: Bought3

1) Validation set approach. We begin by using the sample() function to split the set of observations into two halves, by selecting a random subset of 196 observations out of the original 392 observations. We refer to these observations as the training set.

> install.packages("ISLR")

> library (ISLR)

> auto=read.csv("Auto.csv")

> attach(auto)

> set.seed (1)

> train=sample (392 ,196)

 

We then use the subset option in lm() to fit a linear regression using only the observations corresponding to the training set.

> lm.fit =lm(mpg∼horsepower ,data=auto ,subset =train )

 

We now use the predict() function to estimate the response for all 392 observations, and we use the mean() function to calculate the MSE of the 196 observations in the validation set. Note that the -train index below selects only the observations that are not in the training set.

> attach (auto)

> mean((mpg -predict (lm.fit ,auto))[-train ]^2)

[1] 26.14142

 

We can use the poly() function to estimate the test error for the quadratic and cubic regressions.

> lm.fit2=lm(mpg∼poly(horsepower ,2) ,data=auto ,subset =train )

> mean((mpg -predict (lm.fit2 ,auto))[-train ]^2)

[1] 19.82259

> lm.fit3=lm(mpg∼poly(horsepower ,3) ,data=auto ,subset =train )

> mean((mpg -predict (lm.fit3 ,auto))[-train ]^2)

[1] 19.78252

 

Questions:

Implement the same validation test for different split of the data into training and validation subsets. by running the random generation number 100 times.

The MSE for the linear model is

a.   22.134          b. 22.132           c.    26.134                d. None of above The MSE for the quadratic model is

a.   21.617                     b.  21.364            c.  21.123                d. None of above

The MSE for the cubic model is

a.   21.701                      b.  21.034           c.  20.272             d. None of above

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE