% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/GLMnet.R
\name{GLMnet}
\alias{GLMnet}
\title{Formula interface for glmnet}
\usage{
GLMnet(
  formula,
  data,
  lambda = NULL,
  alpha = 1,
  cv = TRUE,
  nfolds = 10,
  type.measure = "deviance",
  selector = "min",
  family,
  penalty.factor,
  ...
)
}
\arguments{
\item{formula}{Formula where the left hand side specifies either a single variable (continuous, binary or categorical),
or a survival outcome (time, event), and the right hand side specifies the linear predictor.
Survival outcome can either be specified via \link[survival]{Surv} or via \link[prodlim]{Hist}.}

\item{data}{The data used to fit the model.}

\item{lambda}{A hyperparameter passed to glmnet. If set to NULL, then lambda is chosen by cross-validation,
via the function \link[glmnet]{cv.glmnet}.}

\item{alpha}{The elasticnet mixing parameter, with 0<=alpha<= 1. \code{alpha =1} is the lasso penalty, and \code{alpha=0} the ridge penalty.
If \code{alpha} is also provided via \code{...}, the value in \code{...} takes precedence.}

\item{cv}{Whether to use cross-validation or not. Default is TRUE.}

\item{nfolds}{Passed on to \link[glmnet]{cv.glmnet}. Number of folds for cross-validation. The default is 10.}

\item{type.measure}{Passed on to \link[glmnet]{cv.glmnet}. Loss to use for cross-validation. Default is deviance.}

\item{selector}{One of \code{'min'}, \code{'1se'}, \code{'undersmooth'} where the first two are described in the help page of \link[glmnet]{cv.glmnet}
and the latter is the smallest lambda value where the model could fit. Default is \code{'min'}.
When \code{'undersmooth'} is specified, no cross-validation is performed.}

\item{family}{Passed on to \link[glmnet]{glmnet} and \link[glmnet]{cv.glmnet}. For binary outcome the default is \code{"binomial"} and for survival \code{"cox"}.}

\item{penalty.factor}{The penalty factor which is passed on to \link[glmnet]{glmnet} and \link[glmnet]{cv.glmnet}. It must be a named
vector where the names are the column names of the design matrix. To see the column names of the design matrix perform a dryrun without setting
argument \code{penalty.factor} and check element \code{fit$penalty.factor}.}

\item{\dots}{Additional arguments passed on to \link[glmnet]{glmnet} and \link[glmnet]{cv.glmnet}.}
}
\value{
A GLMnet object (class \code{"GLMnet"}) containing:
\itemize{
  \item \code{fit}: glmnet or cv.glmnet fit
  \item \code{x}: design matrix used for fitting
  \item \code{y}: response used for fitting
  \item \code{terms}: terms object with specials
  \item \code{penalty.factor}: named vector aligned with \code{colnames(x)}
  \item \code{rcs.parameters}: named vector with parameters for the rcs terms
}
}
\description{
Fit glmnet models via a formula and a data set for use with \code{\link{predictRisk}}.

Supports special terms on the right-hand side:
\itemize{
  \item \code{unpenalized(x)}: exclude variable \code{x} from penalization.
  \item \code{pen(x, w)}: apply a relative penalty weight \code{w} to variable \code{x}
        (e.g., \code{pen(x,2)} more penalty; \code{pen(z,0)} unpenalized).
  \item \code{rcs(x, ...)}: restricted cubic splines for numeric \code{x}. This is handled as a
        *special marker* (see Details). Knots are estimated from the training data (unless specified)
        and stored in the fitted object.
  \item \code{strata(x)} (survival outcome only): stratified baseline hazards.
}
}
\details{
For \code{rcs()}, GLMnet will compute the spline basis using
\link[rms]{rcs} with knots derived from the data. In this application, rcs has
two arguments: the number of knots \code{nknots} (defaults to 3) and the penalty factor \code{pf} (defaults to 2).
}
\examples{
library(glmnet)
library(riskRegression)
library(survival)
data(Melanoma)
fit <- GLMnet(Surv(time,status==1)~pen(invasion,4)+epicel+ulcer+logthick+pen(sex,0)+age,
             data=Melanoma)
}
\seealso{
\link[glmnet]{glmnet}
}
