Search code examples
rplotinteractionglmnetlasso-regression

plotting interaction effects for LASSO models in R


I fitted a lasso logistic model with interaction terms. Then i wanted to visualize those interactions using a interaction plot. I tried to find some R function that will plot interactions for glmnet models and i couldnt find any .

Is there any R package that will plot interactions for LASSO ?

Since i couldnt find any, i tried to do it manually , by plotting the predicted values. But i am getting some errors.

My code is as follows,

 require(ISLR)
    require(glmnet)
    y <- Smarket$Direction
    x <- model.matrix(Direction ~ Lag1 + Lag4* Volume, Smarket)[, -1]

    lasso.mod <- cv.glmnet(x, y, alpha=1,family="binomial",nfolds = 5, type.measure="class",
                           lambda = seq(0.001,0.1,by = 0.001))

     lasso.mod$lambda.min

     pred = expand.grid(Lag1 = median(Smarket$Lag1),
                            Lag4 = c(-0.64,0.0385,0.596750),
                            Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100)) 





      lasso.mod1 <- glmnet(x, y, alpha=1,family="binomial",
                            lambda = lasso.mod$lambda.min)

     pred$Direction = predict(lasso.mod1, newx=pred, 
type="response", s= lasso.mod$lambda.min) 

i am getting this error :

Error in cbind2(1, newx) %*% nbeta : 
  not-yet-implemented method for <data.frame> %*% <dgCMatrix>

Can any suggest anything to fix this issue ?

Thank you


Solution

  • predict.glmnet says newx must be a matrix. And you need to give interaction value by yourself.

    library(dplyr)
    
    pred = expand.grid(Lag1 = median(Smarket$Lag1),
                       Lag4 = c(-0.64,0.0385,0.596750),
                       Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100))  %>% 
      mutate(`Lag4:Volume` = Lag4 * Volume)     # preparing interaction values
    
    
    pred$Direction = predict(lasso.mod1, newx = as.matrix(pred),   # convert to matrix
                             type = "link", s= lasso.mod$lambda.min) 
    

    [EDITED]
    Oh, I overlooked more general, better way.

    pred = expand.grid(Lag1 = median(Smarket$Lag1),
                       Lag4 = c(-0.64,0.0385,0.596750),
                       Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100)) 
    
    pred$Direction = predict(lasso.mod1, 
                             newx = model.matrix( ~ Lag1 + Lag4* Volume, pred)[, -1], 
                             type="response", s= lasso.mod$lambda.min)