0

I am using R glmnet package for elastic net and results look like this:

     TRTREJ6M_KIY                     .           
     TXKIDE                          -1.317048e-01
     TXKIDL                           .           
     TXKIDR                           .           
     URINE_INF_DON                    .           
     VASODIL_DONN                     .           
     VASODIL_DONU                     .            
     VASODIL_DONY                     .           

I have tried this:

   print1
   function(riskcoef99){ind <- which(coef(riskcoef99) != 0)
   df <- data.frame(
   feature=rownames(coef(riskcoef99))[ind],
   coeficient=coef(riskcoef99)[ind]
   )
   kable(df)
   }

and this:

    colnames(risklevelcsv)[which(coef(riskCV99,s="lambda.min") != 0)]

The glmnet file name is riskCV99 and I have save coefs with riskcoef99 file. Please help me out to write the results into csv, excel or text file I have been trying for the last 2 days and couldnt fugure out. Thanks in advance

Dr. Turkuaz
  • 39
  • 1
  • 10

1 Answers1

0

You haven't provided enough of a reproducible example to make sure we can match your exact situation but consider this.

This function

extract <- function(o, s) { 
    index <- which(coef(o, s) != 0) 
    data.frame(name=rownames(coef(o))[index], coef=coef(o, s)[index]) 
}

can be used to extract the non-zero coefficients (given a penalty s) from a glmnet object o to a data frame. Once you have a data frame you can save it as a csv using write.csv:

 df <- extract(riskcoef99, s=0.1)
 write.csv(df, file="saved.csv", row.names=FALSE, quote=FALSE)
ekstroem
  • 5,957
  • 3
  • 22
  • 48