(This article was first published on Yet Another Blog in Statistical Computing » S+/R, and kindly contributed to R-bloggers)
Julia Code
using RDatasets, DataFrames, GLMNet data = dataset("MASS", "Boston"); y = array(data[:, 14]); x = array(data[:, 1:13]); cv = glmnetcv(x, y); cv.path.betas[:, indmin(cv.meanloss)]; result = DataFrame(); result[:Vars] = names(data)[1:13]; result[:Beta] = cv.path.betas[:, indmin(cv.meanloss)]; result # | Row | Vars | Beta | # |-----|---------|------------| # | 1 | Crim | -0.0983463 | # | 2 | Zn | 0.0414416 | # | 3 | Indus | 0.0 | # | 4 | Chas | 2.68519 | # | 5 | NOx | -16.3066 | # | 6 | Rm | 3.86694 | # | 7 | Age | 0.0 | # | 8 | Dis | -1.39602 | # | 9 | Rad | 0.252687 | # | 10 | Tax | -0.0098268 | # | 11 | PTRatio | -0.929989 | # | 12 | Black | 0.00902588 | # | 13 | LStat | -0.5225 |
R Code
library(glmnet) data(Boston, package = "MASS") x <- as.matrix(Boston[, 1:13]) y <- as.matrix(Boston[, 14]) cv <- cv.glmnet(x, y, nfolds = 10) mdl <- glmnet(x, y, lambda = cv$lambda.min) mdl$beta # crim -0.098693203 # zn 0.041588291 # indus . # chas 2.681633344 # nox -16.354590598 # rm 3.860035926 # age . # dis -1.399697121 # rad 0.255484621 # tax -0.009935509 # ptratio -0.931031828 # black 0.009031422 # lstat -0.522741592
To leave a comment for the author, please follow the link and comment on their blog: Yet Another Blog in Statistical Computing » S+/R.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...