Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. ## Exatract coefficients from a glmnet model
  2. ## Data set:
  3. library(glmnet)
  4. X <- matrix(rnorm(100*10), 100, 10);
  5. X[51:100, ] <- X[51:100, ] + 0.5; #artificially introduce difference in control cases
  6. rownames(X) <- paste0("observation", 1:nrow(X));
  7. colnames(X) <- paste0("feature", 1:ncol(X));
  8.  
  9. y <- factor( c(rep(1,50), rep(0,50)) ); #binary outcome class label
  10. y
  11. ## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  12. ## [51] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  13. ## Levels: 0 1
  14.  
  15. ## Perform logistic model fit:
  16. fit1 <- cv.glmnet(X, y, family="binomial", nfolds=5, type.measure="auc"); #with K-fold cross validation
  17. # fit1 <- glmnet(X, y, family="binomial") #without cross validation
  18. summary(fit1)
  19. predict(fit1,newx=X[sample(1:nrow(X), 5), ], s="lambda.min") #predict on randomly sampled 5 rows
  20. coef(fit1, s="lambda.min")[which(coef(fit1, s="lambda.min") != 0)] #includes intercept!
  21.  
  22. ## Adapted from @Mehrad Mahmoudian:
  23. myCoefs <- coef(fit1, s="lambda.min");
  24. myCoefs[which(myCoefs != 0 ) ] #coefficients: intercept included
  25. ## [1] 1.4945869 -0.6907010 -0.7578129 -1.1451275 -0.7494350 -0.3418030 -0.8012926 -0.6597648 -0.5555719
  26. ## [10] -1.1269725 -0.4375461
  27. myCoefs@Dimnames[[1]][which(myCoefs != 0 ) ] #feature names: intercept included
  28. ## [1] "(Intercept)" "feature1" "feature2" "feature3" "feature4" "feature5" "feature6"
  29. ## [8] "feature7" "feature8" "feature9" "feature10"
  30.  
  31. ## Asseble into a data.frame
  32. myResults <- data.frame(
  33. features = myCoefs@Dimnames[[1]][ which(myCoefs != 0 ) ], #intercept included
  34. coefs = myCoefs [ which(myCoefs != 0 ) ] #intercept included
  35. )
  36. myResults
  37. ## features coefs
  38. ## 1 (Intercept) 1.4945869
  39. ## 2 feature1 -0.6907010
  40. ## 3 feature2 -0.7578129
  41. ## 4 feature3 -1.1451275
  42. ## 5 feature4 -0.7494350
  43. ## 6 feature5 -0.3418030
  44. ## 7 feature6 -0.8012926
  45. ## 8 feature7 -0.6597648
  46. ## 9 feature8 -0.5555719
  47. ## 10 feature9 -1.1269725
  48. ## 11 feature10 -0.4375461
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement