knitr::opts_chunk$set( collapse = TRUE, cache = FALSE, comment = "# ", fig.path = "tools/README-", dpi = 67 )
fwelnet
is a package that fits the feature-weighted elastic net (fwelnet), a variant of the elastic net which has feature-specific penalties. These penalties are based on additional information that the user has on the features. fwelnet
works with continuous and binary responses. For details, please see the preprint. For a short tutorial on how to use the package, please see the vignette in the vignettes/
folder.
Here is a simple example to illustrate how to use this package. First, let's generate some data. In this example, we assume that we have 40 features, and that these features come in 4 groups of 10. The response is a linear combination of the features from the first 2 groups with additive Gaussian noise.
set.seed(1) n <- 100 p <- 40 groups <- list(1:10, 11:20, 21:30, 31:40) # which features belong to which group x <- matrix(rnorm(n * p), nrow = n, ncol = p) beta <- matrix(rep(1:0, each = 20), ncol = 1) y <- x %*% beta + rnorm(n)
In order to fit a fwelnet model, we have to define a feature information matrix. In our example, we have $\mathbf{Z} \in \mathbb{R}^{40 \times 4}$, with $z_{jk} = 1{ \text{feature } j \text{ belongs to group } k }$.
# generate Z matrix z <- matrix(0, nrow = p, ncol = length(groups)) for (i in 1:length(groups)) { z[groups[[i]], i] <- 1 }
Once z
is specified, we can fit the fwelnet model with fwelnet()
.
library(fwelnet) fit <- fwelnet(x, y, z)
"fwelnet" objects are equipped with predict
and coef
methods which allow the user to make predictions on new data and to view the model coefficients. By default predictions and coefficients are returned for the whole lambda
path.
# predictions for first 5 observations at 20th lambda value predict(fit, x[1:5, ])[, 20] # coefficients at the 20th lambda value (including intercept) as.numeric(coef(fit)[, 20])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.