2

I'm using the caret package in R to fit a LASSO regression model. My code runs fine, however I would like to extract the Intercept for the final model so I can build a scoring key using the selected predictors and coefficients.

For example, if "Extraversion" is the variable I am trying to model using survey items, I would like to produce the following scoring key:

 Intercept + Survey_Item_1*Slope + Survey_Item_2*Slope + and so on

FWIW, I am able to extract the coefficients for the predictors.

My code for reference:

##Create Training & test set
set.seed(9808)
ind <- sample(0:1, nrow(df), replace=T, prob=c(.75,.25))
train <- df[ind==0,]
test <- df[ind==1,] 
ctrl <- trainControl(method = "repeatedcv", number=5, repeats = 5)

##Train Lasso model
fit.lasso <- train(Extraversion ~., , data=train, method="lasso", preProc=c('scale','center','nzv'), trControl=ctrl)
fit.lasso
predict.enet(fit.lasso$finalModel, type='coefficients', s=fit.lasso$bestTune$fraction, mode='fraction')

##Fit models to test data
lasso_test<- predict(fit.lasso, newdata=test, na.action="na.pass")
postResample(pred = lasso_test, obs = test[,c(1)])
RMAkh
  • 123
  • 1
  • 10
  • 1
    See this question for a good answer in the comments https://stackoverflow.com/questions/41797792/how-should-i-get-the-coefficients-of-lasso-model . In general, you can extract the underlying package's object with caret_output$finalModel, which is usually easier to work with. – Johannes Jan 28 '18 at 14:32

0 Answers0