# Using deviation coding (effect coding) of factors in glmnet LASSO in R

Various sources have instructed me how to use deviation coding (aka effects coding) in R (see here, here, and here).

My question though, is how to go about doing this for LASSO regression using glmnet?

To properly create a matrix to feed glmnet for LASSO regression, I use model.matrix() (as recommended by here, here, here, etc.).

According to this CV answer (and this one), however, you should remove the intercept from the resulting matrix before feeding the matrix into a glmnet function because glmnet auto generates its own intercept.

1. What does this mean? (How is the intercept defined in glmnet? -- see below for concern)

In my case, I have a 62-level factor as an independent variable. However, none of the levels serve as a reference level. So I wanted to use deviation coding (ala contrasts = 'contr.sum' in model.matrix) so that no factor is used as a reference. (this is opposed to the standard 'treatment' contrasts used in R).

1. My main question: will glmnet create an intercept that would otherwise be identical to the one I created using contr.sum (deviation coding) in creating my matrix?

• My concern is that my dummy variables will all be based on grand means in a deviation coding system, but my regression will use an intercept that thinks that all my factor dummy variables are relative to a reference level.

• Do I need to suppress the intercept in glmnet?

• Do I need to introduce a contrasts argument into glmnet?

2. Follow-up question: When creating my model matrix using deviation coding, do I need to modify my equation?

• Ex. Currently I have y ~ factor(x) + z. Do I need to modify this equation any when using deviation coding vs. strict treatment dummy coding? (e.g., y ~ -1 + factor(x) + z)?

Context: I'm creating a predictive model and am less concerned about interpretability (though, the model of course still needs to be valid!).

• I have a 62-level factor as an independent variable. However, none of the levels serve as a reference level. So I wanted to use deviation coding. Contrast of any type needs a reference category, deviation type too. In this answer (showing how one can prepare contrasts to use in a regression program when he doesn't have specialized ANOVA program) I considered various contrast types, all of them needs a reference group. – ttnphns Oct 6 '16 at 17:01
• note: you can suppress the intercept in glmnet, according to this CV answer and confirmed by me. Just set argument intercept = FALSE. – theforestecologist Oct 6 '16 at 18:24