Question: How to use the following R code to complete the questions R code library(GGally) library(lattice) library(leaps) library(car) library(boot) ### Shakespeare theater data shake.data

How to use the following R code to complete the questions R code library(GGally) library(lattice) library(leaps) library(car) library(boot) ### Shakespeare theater data shake.data <- read.table("http://www.users.miamioh.edu/smuckebj/STA463/shakespeare_data.csv", header=TRUE, sep=",") dim(shake.data) # number of rows and columns head(shake.data) shake.data$Totalsale1 <- NULL # omit duplicate column ### Use best subsets and the validation set method to choose a model train.d <- sample(1:dim(shake.data)[1],30,replace=FALSE) train <- shake.data[train.d,] holdout <- shake.data[setdiff(1:dim(shake.data)[1],train.d),] shakefits.v<-regsubsets(Totalsales~ps+Number.of.Seasons+Genre +Interval+R.Sales+Cum.Sales +ps:Number.of.Seasons+ps:R.Sales+ps:Cum.Sales +Number.of.Seasons:R.Sales+Number.of.Seasons:Cum.Sales +R.Sales:Cum.Sales,data=train,nvmax=15) # Build an X matrix for the holdout (test) data test.mat <- model.matrix(Totalsales~ps+Number.of.Seasons+Genre +Interval+R.Sales+Cum.Sales +ps:Number.of.Seasons+ps:R.Sales+ps:Cum.Sales +Number.of.Seasons:R.Sales+Number.of.Seasons:Cum.Sales +R.Sales:Cum.Sales,data=holdout) # Use a for loop to extract the coefficients from regsubsets for the best model # of each size, and multiply them by the X matrix to form the predictions # initialize error matrix val.errors <- rep(NA,15) for(i in 1:15){ # get coefficients for the best model of size i coefi <- coef(shakefits.v,id=i) # predictions for the holdout points based on the model fit from training set pred <- test.mat[,names(coefi)] %*% coefi val.errors[i] <- sqrt(mean((holdout$Totalsales-pred)^2)) }

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 Mathematics Questions!