Nov. 21 2016

Main Steps of the App.

  • Use the fileInput to load data file (csv file), default data is mtcars
  • Use the selectInput to Choose the predictor from the variables of the input data
  • Use the slideInput to choose the percentage of sampling training dataset
  • Use the radioButtons to choose menual or cross-validation
  • Use the numericInput to set regularization parameter lambda or k value of the k-fold cross-validation
  • Output regularization parameter(lambda), mean squared error of the model, and linear model coefficients
  • Use Plotly to show the differences between test data and model prediction

Linear Regression Code Using glmnet

suppressMessages(library(glmnet))
library(ISLR)
rdata = mtcars
name_y = "mpg"
y <- rdata[, name_y]
x = model.matrix(as.formula(paste(name_y, "~.")), rdata)[,-1]
set.seed(1)
train = sample(1:nrow(x), nrow(x)*0.7)
test = (-train)
y.test = y[test]
grid = 10^seq(10,-2, length =100)
ridge.mod <- glmnet(x[train,], y[train], alpha = 0, 
                    lambda = grid, thresh = 1e-12)
cv.out = cv.glmnet(x[train,], y[train], nfolds = 5, alpha =0)
lambda =cv.out$lambda.min

mtcars is used as an example to show the implementation

Mean Squared Error and Linear Model Coefficients

ridge.pred <- predict(ridge.mod, s=lambda, newx=x[test,])
mean((ridge.pred - y.test)^2)
## [1] 2.516433
predict(ridge.mod, s=lambda, type ="coefficients")[0:dim(x)[2]+1,]
##  (Intercept)          cyl         disp           hp         drat 
## 22.379222271 -0.389436662 -0.005790755 -0.010664773  0.895387953 
##           wt         qsec           vs           am         gear 
## -1.074553189  0.123651582  0.804708637  1.266567563  0.492339730 
##         carb 
## -0.507766268

Comparison of the Test Data with Model Prediction