Learn R Programming

kohonen (version 2.0.19)

predict.kohonen: Predict properties using a trained Kohonen map

Description

Map objects to a trained Kohonen map, and return for each object the property associated with the corresponding winning unit. For som and supersom maps, the unit properties are calculated using explicit arguments trainX and trainY; for xyf and bdk maps, the predicted properties are the Y-codebookvectors. Note that in the latter case only the X-space is used for prediction.

Usage

"predict"(object, newdata, trainX, trainY, unit.predictions, threshold = 0, whatmap = NULL, weights = 1, ...)

Arguments

object
Trained network.
newdata
Data matrix for which predictions are to be made. If not given, defaults to the training data (when available).
trainX
Training data for obtaining predictions for unsupervised maps; necessary for som maps trained with the keep.data = FALSE option.
trainY
Values for the dependent variable for the training data; necessary for som and supersom maps.
unit.predictions
Possible override of the predictions for each unit.
threshold
Used in class predictions; see classmat2classvec.
whatmap
For supersom maps: what layers to use in the mapping.
weights
For supersom maps: weights of layers uses in the mapping.
...
Currently not used.

Value

Returns a list with components
prediction
predicted values for the properties of interest. When multiple values are predicted, this element is a list, otherwise a vector or a matrix.
unit.classif
unit numbers to which objects in the data matrix are mapped.
unit.predictions
mean values associated with map units. Again, when multiple properties are predicted, this is a list.

See Also

som,xyf,bdk, supersom, map

Examples

Run this code
data(wines)
set.seed(7)

training <- sample(nrow(wines), 120)
Xtraining <- scale(wines[training, ])
Xtest <- scale(wines[-training, ],
               center = attr(Xtraining, "scaled:center"),
               scale = attr(Xtraining, "scaled:scale"))

som.wines <- som(Xtraining, grid = somgrid(5, 5, "hexagonal"))

som.prediction <- predict(som.wines, newdata = Xtest,
          trainX = Xtraining,
          trainY = factor(wine.classes[training]))
table(wine.classes[-training], som.prediction$prediction)

### more complicated examples
data(yeast)

### only consider complete cases
missings <- (apply(cbind(yeast$alpha, yeast$cdc15), 1,
                   function(x) any(is.na(x))))

yeast2 <- list(alpha = yeast$alpha[!missings,],
               cdc15 = yeast$cdc15[!missings,],
               class = yeast$class[!missings])

set.seed(7)
training.indices <- sample(nrow(yeast2$alpha), 300)
training <- rep(FALSE, nrow(yeast2$alpha))
training[training.indices] <- TRUE

## unsupervised mapping
yeast2.som <- som(yeast2$alpha[training,], somgrid(4, 6, "hexagonal"))
yeast2.som.prediction <- predict(yeast2.som,
                                 newdata = yeast2$alpha[!training,],
                                 trainY = yeast2$class[training])
table(yeast2$class[!training], yeast2.som.prediction$prediction)

## supervised mapping (XYF) - trainY is no longer necessary
yeast2.xyf <- xyf(yeast2$alpha[training,], yeast2$class[training],
                  somgrid(4, 6, "hexagonal"))
yeast2.xyf.prediction <- predict(yeast2.xyf,
                                 newdata = yeast2$alpha[!training,])
table(yeast2$class[!training], yeast2.xyf.prediction$prediction)

## supervised mapping (BDK)
yeast2.bdk <- bdk(yeast2$alpha[training,], yeast2$class[training],
                  somgrid(4, 6, "hexagonal"))
yeast2.bdk.prediction <- predict(yeast2.bdk,
                                 newdata = yeast2$alpha[!training,])
table(yeast2$class[!training], yeast2.bdk.prediction$prediction)

## unsupervised mapping (supersom): prediction of data layer not used
## in training
yeast2.ssom <- supersom(lapply(yeast2, function(x) subset(x, training)),
                        grid = somgrid(4, 6, "hexagonal"),
                        whatmap = 1)
yeast2.ssom.prediction <- predict(yeast2.ssom,
                                  newdata = lapply(yeast2,
                                    function(x) subset(x, !training)),
                                  trainY = list(class = yeast2$class[training]))
table(yeast2$class[!training], yeast2.ssom.prediction$prediction)

## supervised mapping (supersom): prediction of a data layer that has
## been used in training - trainY is not necessary
yeast2.ssom2 <- supersom(lapply(yeast2, function(x) subset(x, training)),
                         grid = somgrid(4, 6, "hexagonal"),
                         whatmap = c(1,3))
yeast2.ssom2.prediction <- predict(yeast2.ssom2, 
                                   newdata = lapply(yeast2,
                                     function(x) subset(x, !training)),
                                   whatmap = 1)
table(yeast2$class[!training], yeast2.ssom2.prediction$prediction)

Run the code above in your browser using DataLab