
Predictions and fitted values from sparse regression models
Source:R/model_predictions.R
model_predictions.RdS3 prediction methods for L0glm.fit(), L0nnpois() and sparseglm(),
including negative-binomial, multinomial and right-censored Cox models.
Usage
# S3 method for class 'L0glm_fit'
predict(
object,
newdata = NULL,
type = "response",
offset = NULL,
times = NULL,
...
)
# S3 method for class 'L0glm_extended_fit'
predict(
object,
newdata = NULL,
type = "response",
offset = NULL,
times = NULL,
...
)
# S3 method for class 'sparseglm'
predict(
object,
newdata = NULL,
type = "response",
offset = NULL,
times = NULL,
...
)
# S3 method for class 'L0glm_fit'
fitted(object, ...)
# S3 method for class 'sparseglm'
fitted(object, ...)Arguments
- object
A fitted model or
sparseglmcomparison.- newdata
Predictor matrix, sparse or dense, or numeric data frame. Omit the intercept added by
intercept=TRUE. Supply all original predictor columns, including columns excluded by starting zeroes. Complete unique column names are matched to the training names; unnamed data use training column order. A numeric vector denotes one row, or multiple observations for a one-predictor model.NULLuses the stored training design.- type
Default
"response": GLM/NB means, multinomial probabilities, or Cox relative risks."link"returns linear predictors (row-centered CLR scores for multinomial)."probabilities"is also supported for binomial, quasibinomial and multinomial. Multinomial"class"returns a factor; exact ties choose the first stored category. Cox additionally supports"risk"and"survival"; the latter requirestimes.- offset
Prediction offsets: a scalar, vector with one value per predicted row, or matrix with that many rows and one or one-per-response columns. Training predictions default to the stored offsets. New-data predictions default to zero; supply new offsets explicitly, even if the new rows happen to equal the training rows. Multinomial offsets are on the category logits; channel/category columns follow the stored response order. Row-constant shifts have no effect on multinomial probabilities or CLR scores.
- times
Finite nonnegative evaluation times for Cox survival prediction.
- ...
For
fitted(), prediction arguments such astype,offsetandtimes. Usepredict()for new data. Furtherpredict()arguments are currently unused.
Value
GLM/NB means, binomial probabilities, multinomial probabilities and
all link predictions are matrices, including for one response or one row.
Cox risks are numeric vectors; Cox survival is an observations-by-times
matrix. Multinomial classes are factors with the stored category levels.
sparseglm methods return named lists in method order, retaining NULL
entries for failed or unavailable methods.
Details
Predictions use coefficients on their original scale: $$\eta_{ik}=x_i^T\widehat\beta_k+o_{ik},\qquad \widehat\mu_{ik}=g^{-1}(\eta_{ik}).$$ Internal centering/normalization is already undone in the returned coefficients and is not reapplied to new data. Designs retain their dense or sparse storage during multiplication. No model is refitted and no numerical scoring floor is applied to predictions. Binomial outputs are success probabilities, not trial counts.
Multinomial probabilities use stable softmax evaluation, $$\widehat p_{ik}=\frac{\exp(\eta_{ik})}{\sum_l\exp(\eta_{il})},$$ in the stored category order. Count-matrix fits still return probabilities summing to one, rather than expected counts at the training trial totals.
Cox risk is \(\exp(\eta_i)\), relative to a zero linear predictor rather than centered at the training mean. Survival predictions are \(\widehat S(t|x_i)=\exp\{-\widehat H_0(t)\exp(\eta_i)\}\) using the training Breslow baseline. A comparison model's baseline is calculated on demand if it has not been stored. Log cumulative hazards keep this calculation stable under large common offset shifts.
fitted() evaluates the training rows with stored offsets, and accepts the
same prediction types. Collection methods apply these rules to each selected
full-data model; these are not out-of-fold predictions.
Examples
set.seed(5)
X <- matrix(rnorm(240),60,4)
y <- 2*X[,1] + rnorm(60)
fit <- L0glm.fit(X,y,lambda="gic",parallelize=FALSE)
head(fitted(fit))
predict(fit,X[1:3,,drop=FALSE])
comparison <- sparseglm(X,y,method=c("lasso","relaxed_lasso"),nlambda=4)
predict(comparison,X[1:3,,drop=FALSE])
if (requireNamespace("mgcv",quietly=TRUE)) {
labels <- factor(rep(c("A","B","C"),20))
multi <- L0glm.fit(X,labels,family=mgcv::multinom(K=2),lambda="gic")
predict(multi,X[1:3,,drop=FALSE],type="probabilities")
}