This package provides functions to compute the non-negative garrote estimator with (or without) a penalized initial estimator.
You can install the stable version on R CRAN.
install.packages("nnGarrote", dependencies = TRUE)
You can install the development version from GitHub.
library(devtools)
::install_github("AnthonyChristidis/nnGarrote") devtools
Here is some code to compute the non-negative garrote estimator with ridge regression as an initial estimator, and compare it with ridge regression without the additional garrote shrinkage.
# Setting the parameters
<- 100
p <- 500
n <- 5000
n.test <- 0.2
sparsity <- 0.5
rho <- 3
SNR set.seed(0)
# Generating the coefficient
<- floor(p*sparsity)
p.active <- 4*log(n)/sqrt(n)
a <- 0.2
neg.prob <- (-1)^(rbinom(p.active, 1, neg.prob))*(a + abs(rnorm(p.active)))
nonzero.betas <- c(nonzero.betas, rep(0, p-p.active))
true.beta # Two groups correlation structure
<- matrix(0, p, p)
Sigma.rho 1:p.active, 1:p.active] <- rho
Sigma.rho[diag(Sigma.rho) <- 1
<- as.numeric(sqrt((t(true.beta) %*% Sigma.rho %*% true.beta)/SNR))
sigma.epsilon
# Simulate some data
library(mvnfast)
<- mvnfast::rmvn(n, mu=rep(0,p), sigma=Sigma.rho)
x.train <- 1 + x.train %*% true.beta + rnorm(n=n, mean=0, sd=sigma.epsilon)
y.train <- mvnfast::rmvn(n.test, mu=rep(0,p), sigma=Sigma.rho)
x.test <- 1 + x.test %*% true.beta + rnorm(n.test, sd=sigma.epsilon)
y.test
# Applying the NNG with Ridge as an initial estimator
<- cv.nnGarrote(x.train, y.train, intercept=TRUE,
nng.out initial.model=c("LS", "glmnet")[1],
lambda.nng=NULL, lambda.initial=NULL, alpha=0,
nfolds=5)
<- predict(nng.out, newx=x.test)
nng.predictions mean((nng.predictions-y.test)^2)/sigma.epsilon^2
# Ridge Regression
<- glmnet::cv.glmnet(x.train, y.train, alpha=0)
cv.ridge <- glmnet::glmnet(x.train, y.train, alpha=0, lambda=cv.ridge$lambda.min)
ridge <- predict(ridge, newx=x.test)
ridge.predictions mean((ridge.predictions-y.test)^2)/sigma.epsilon^2
# Comparisons of the coefficients
coef(nng.out)
coef(ridge)
Note that the prediction accuracy is improved for the non-negative garrote in comparison to the ridge regression estimate. Also, the non-negative garrote output for the coefficient is much closer to the true one than the ridge regression output (in terms of the recall and precision).
This package is free and open source software, licensed under GPL (>= 2).