Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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