stackavg: Stacking for predictions models

View source: R/stackavg.R

stackavgR Documentation

Stacking for predictions models

Description

Model stacking (here = averaging) for regression (quantitative response) and discrimination (qualitative response) models.

Functions stackavg and stackavgcla calculate "averages", eventually with a priori weighting, of univariate predictions returned by a set of different models:

- Arithmetic mean of the quantitative predictions for regression models;

- Vote of the predicted classes for discrimination models.

A weighting of the predictions in the mean and vote is allowed (argument weights).

Usage


stackavg(fit, y = NULL, formula, nam = NULL, weights = NULL)

stackavgcla(fit, y = NULL, formula, nam = NULL, weights = NULL)

Arguments

fit

Data frame of predicted values.

y

Data frame of y-data. If the y-data do not exist, set to NULL (default).

formula

A right-hand-side formula defing the aggregation levels on which the predictions are averaged.

nam

Name (character string) of the column to consider in fit and y for calculating the averaging. If NULL (default), the last column of fit and y are considered.

weights

A vector (of length the number of rows in y and fit) of eventual weights to apply for the averaging or vote. If NULL (default), this is the standard mean or vote (all weights are set to 1).

Value

A list of outputs (see examples), such as:

y

Responses for the test data.

fit

Predictions for the test data.

r

Residuals for the test data.

Examples


############## Regression

data(datcass)

Xr <- datcass$Xr
yr <- datcass$yr
Xu <- datcass$Xu
yu <- datcass$yu

ncomp <- 25
fm <- plsr(Xr, yr, Xu, yu, ncomp = ncomp)

z <- mse(fm, ~ ncomp)
z[z$rmsep == min(z$rmsep), ]
plotmse(z)

w <- ifelse(fm$y$ncomp >= 5 & fm$y$ncomp <= 20, 1, 0)
zfm <- stackavg(fm$fit, fm$y, formula = ~ rownum + rownam, weights = w)
names(zfm)
head(zfm$y)
head(zfm$fit)
head(zfm$r)

mse(zfm)


############## Discrimination

data(datforages)

Xr <- datforages$Xr
yr <- datforages$yr
Xu <- datforages$Xu
yu <- datforages$yu

ncomp <- 20
fm <- plsdalm(Xr, yr, Xu, yu, ncomp = ncomp)
z <- err(fm, ~ ncomp)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp")

w <- ifelse(fm$y$ncomp >= 8 & fm$y$ncomp <= 20, 1, 0)
zfm <- stackavgcla(fm$fit, fm$y, formula = ~ rownum + rownam, weights = w)
names(zfm)
head(zfm$y)
head(zfm$fit)
head(zfm$r)

err(zfm)


mlesnoff/rnirs documentation built on April 24, 2023, 4:17 a.m.