Description Usage Arguments Details Value Author(s) Examples
Interfaces to glmnet
functions that can be used
in a pipeline implemented by magrittr
.
1 2 |
data |
data frame, tibble, list, ... |
... |
Other arguments passed to the corresponding interfaced function. |
Interfaces call their corresponding interfaced function.
Object returned by interfaced function.
Roberto Bertolusso
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | ## Not run:
library(intubate)
library(magrittr)
library(glmnet)
## NOTE: glmnet package does not implement formula interface. As I need it
## for teaching purposes, this is the first pure non-formula library
## included, as a proof of concept that not only the functions with
## formula + data variant can be successfully interfaced to use in
## a pipeline.
library(ISLR)
data("Hitters")
Hitters <- na.omit(Hitters)
dta <- list(x = model.matrix(Salary ~ ., Hitters)[, -1], ## Remove intercept
y = model.frame(Salary ~ ., Hitters)[, 1])
grid <- 10^seq(10, -2, length = 100)
## ntbt_glmnet: fit a GLM with lasso or elasticnet regularization
## Original function to interface
attach(dta)
## Ridge Regression
ridge <- glmnet(x, y, alpha = 0, lambda = grid)
plot(ridge)
## The Lasso
lasso <- glmnet(x, y, alpha = 1, lambda = grid)
plot(lasso)
detach()
## The interface puts data as first parameter
## Ridge Regression
ridge <- ntbt_glmnet(dta, x, y, alpha = 0, lambda = grid)
plot(ridge)
## The Lasso
lasso <- ntbt_glmnet(dta, x, y, alpha = 1, lambda = grid)
plot(lasso)
## so it can be used easily in a pipeline.
## Ridge Regression
dta %>%
ntbt_glmnet(x, y, alpha = 0, lambda = grid) %>%
plot()
## The Lasso
dta %>%
ntbt_glmnet(x, y, alpha = 1, lambda = grid) %>%
plot()
## ntbt_cv.glmnet: Cross-validation for glmnet
## Original function to interface
attach(dta)
## Ridge Regression
set.seed(1)
cv.ridge <- cv.glmnet(x, y, alpha = 0)
plot(cv.ridge)
## The Lasso
cv.lasso <- cv.glmnet(x, y, alpha = 1)
plot(cv.lasso)
detach()
## The interface puts data as first parameter
## Ridge Regression
set.seed(1)
cv.ridge <- ntbt_cv.glmnet(dta, x, y, alpha = 0)
plot(cv.ridge)
## The Lasso
cv.lasso <- ntbt_cv.glmnet(dta, x, y, alpha = 1)
plot(cv.lasso)
## so it can be used easily in a pipeline.
## Ridge Regression
set.seed(1)
dta %>%
ntbt_cv.glmnet(x, y, alpha = 0) %>%
plot()
## The Lasso
dta %>%
ntbt_cv.glmnet(x, y, alpha = 1) %>%
plot()
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.