Answered step by step
Verified Expert Solution
Link Copied!

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

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_2

Step: 3

blur-text-image_3

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

A Survey Of Mathematics With Applications

Authors: Allen R. Angel, Christine D. Abbott, Dennis Runde

11th Edition

0135740460, 978-0135740460

More Books

Students also viewed these Mathematics questions

Question

Would another approach to the decision have worked better?

Answered: 1 week ago

Question

In Exercises find dy/dx by implicit differentiation. xy - y = x

Answered: 1 week ago