Adding labels on curves in glmnet plot in R -


i using glmnet package following graph mtcars dataset (regression of mpg on other variables):

library(glmnet) fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1]) plot(fit, xvar='lambda') 

enter image description here

how can add names of variables each curve, either @ beginning of each curve or @ maximal y point (maximum away x-axis)? tried , can add legend usual not labels on each curve or @ start. help.

as labels hard coded perhaps easier write quick function. quick shot, can changed more thorough. note when using lasso there lot of variables there lot of overlap of labels (as seen in small example)

lbs_fun <- function(fit, ...) {         l <- length(fit$lambda)         x <- log(fit$lambda[l])         y <- fit$beta[, l]         labs <- names(y)         text(x, y, labels=labs, ...) }  # plot plot(fit, xvar="lambda")  # label lbs_fun(fit) 

enter image description here


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -