1
votes

I used caret package to train a glmnet model. I standardized the predictors. It seems like glmnet automatically standardize the predictors for me. So whether I use preProcess or not does not affect my result.

library(caret)
fit<- caret::train (price~carat+depth+cut,
              data=diamonds[1:200,],
              method= 'glmnet',
              preProcess=c('center', 'scale'),
                metric = "Rsquared")

I need to know the effect of predictor on my target variable. I can get the variable coefficients from code below.

coef(fit$finalModel, fit$bestTune$lambda)

My question is: Are these variable coefficient from the output in original scale or standardized scale? thanks.

1

1 Answers

0
votes

The variable coefficients are in the original scale. You can look at them, if they are presented wrt to the scaled predictors, it's unlikely to see 1000+ for 1 coefficient (means you have to increase 1000* sd !! ) and -17 for the other.

coef(fit$finalModel, fit$bestTune$lambda)
7 x 1 sparse Matrix of class "dgCMatrix"
                     1
(Intercept) 1717.36000
carat       1092.16045
depth        -17.86985
cut.L         51.31784
cut.Q          .      
cut.C          .      
cut^4          .   

One way to be certain is to check the fitted values:

preProcValues <- preProcess(diamonds[1:200,], method = c("center", "scale"))

pred_scaled = predict(fit,predict(preProcValues,diamonds[1:200,]))
identical(pred_scaled,fitted(fit))
[1] FALSE
pred_orig = predict(fit,diamonds[1:200,])
identical(pred_orig,fitted(fit))
[1] TRUE