1
$\begingroup$

I was trying to solve one problem based on Linear Regression (Predicting the sales which is a continuous variable). I have used Linear Regression for the problem but there is one suggested solution using Lasso Regression. However, the user has used Train function. I am unable to understand why train function has been used. Is it the syntax for Lasso? Similar function has been used for Ridge Regression.

my_control = trainControl(method="cv", number=5)

Grid = expand.grid(alpha = 0, lambda = seq(0.001,0.1,by = 0.0002))

lasso_linear_reg_mod3 = train(x = Train[, -c(1,2)], y = Train$Item_Outlet_Sales, method='glmnet', trControl= my_control, tuneGrid = Grid)
$\endgroup$

2 Answers 2

2
$\begingroup$

The user has divided it's original dataset into two sets: train set and test set.

The model i.e., lasso regression should be trained using train set. Then, the trained model is used to test the model on the test set.

In fact, when you train your model you are trying to find the optimal hyperparameters such as C and regularization (in your code, Grid) via cross validation (in your code, cv).

After finding the optimal hyperparameters, you should train your recently constructed model on the whole train set to find the coefficients (weights) of regression.

Finally, the model is tested on the test set.

$\endgroup$
2
$\begingroup$

The train function is part of the caret package in R, which accommodates several machine learning techniques and lets users train models, cross validate and tune popular parameters.

Using the train function from caret is not the only way to train your LASSO model or any model. There are other ways you could do that - you could use the glmnet or lars packages to train your model on LASSO. But you would have to create your own grid search (parameter tuning) and cross validation functions, if you find it necessary. I would recommend caret as it has all of these already in place.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.