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')
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)
Comments
Post a Comment