Search code examples
rglmnet

Change coefficient of cv.glmnet fitted regression in R


I know how to "extract" coefficient from cv.glmnet. But is there a way to change the coefficient of a fitted object? I know it's kind of a hack, but I want to manually tweak the coefficients of a fitted object just for my own experiment.

Extracting coefficient variable names from glmnet into a data.frame


Solution

  • The coefficients for glmnet are stored under $glmnet.fit$beta :

    library(glmnet)
    library(Matrix)
    
    fit = cv.glmnet(x=as.matrix(mtcars[,-1]),y=mtcars[,1])
    
    head(fit$glmnet.fit$beta)
    6 x 79 sparse Matrix of class "dgCMatrix"
       [[ suppressing 79 column names ‘s0’, ‘s1’, ‘s2’ ... ]]
                                                                            
    cyl  . -0.01192151 -0.1447790 -0.2658654 -0.376195 -0.4770942 -0.5686616
    disp .  .           .          .          .         .          .        
    hp   .  .           .          .          .         .          .        
    drat .  .           .          .          .         .          .        
    wt   . -0.45776130 -0.7006176 -0.9218541 -1.123436 -1.3065806 -1.4739808
    qsec .  .           .          .          .         .          .        
    

    It's one column for every lambda tested and when you call coefficients(), by default you pull out the column corresponding to fit$lambda.1se. Let's say we want to change that column:

    coefficients(fit,s=fit$lambda.1se)
    
    11 x 1 sparse Matrix of class "dgCMatrix"
                           1
    (Intercept) 33.940487806
    cyl         -0.843038418
    disp         .          
    hp          -0.006965929
    drat         .          
    wt          -2.365917424
    qsec         .          
    vs           .          
    am           .          
    gear         .          
    carb         .       
    
    wh = which(fit$lambda==fit$lambda.1se)
    fit$glmnet.fit$beta[,wh] = runif(nrow(fit$glmnet.fit$beta))
    
    coefficients(fit,s=fit$lambda.1se)
    
    11 x 1 sparse Matrix of class "dgCMatrix"
                          1
    (Intercept) 33.94048781
    cyl          0.45636267
    disp         0.28286532
    hp           0.04186184
    drat         0.55084730
    wt           0.35273817
    qsec         0.96165338
    vs           0.79227125
    am           0.01036681
    gear         0.47738589
    carb         0.17170791
    

    And other columns and the intercept remains unchanged:

    11 x 1 sparse Matrix of class "dgCMatrix"
                          1
    (Intercept) 36.38102033
    cyl         -0.87610600
    disp         .         
    hp          -0.01377978
    drat         .         
    wt          -2.75934250
    qsec         .         
    vs           .         
    am           0.16806977
    gear         .         
    carb        -0.01384960
    

    It is really weird to do it. If you want to change the coefficients for prediction purposes, you can always pull out coefficients, alter and multiply them with your data matrix again.