Programming Homework Help. University of Missouri Kansas City Volker Campus R Question
r1 = Carseats$Sales
p1 = data.matrix(Carseats[, c(‘CompPrice’, ‘Income’, ‘Price’,
‘Age’, ‘ShelveLoc’)])
p11=data.frame(p1)
##now apply ridge
Y <- p1
MM <- model.matrix(p11$CompPrice ~ ., data = p11) # the predictors as a datamatrix
ridge.mod <- glmnet(MM, p11$CompPrice, alpha = 1, lambda = 14)
# Apply cross validation (to pick the best value of lambda):
cv.out <- cv.glmnet(MM,p11$CompPrice , alpha = 1)
bestlam <- cv.out$lambda.1se
print(“ridge CV best value of lambda (one standard error)”)
print(bestlam)
ridge.coef <- predict(ridge.mod, type = “coefficients”, s = bestlam)
print(ridge.coef)