In this example we assume the existence of the variables xtrain, ytrain, xttrain, yttrain, xitrain and yitrain, created in Example 1. In adition, we asume that ssc package is already loaded. 

We explore now another way to train a semi-supervised model. For this we need to compute the distance matrix between all training examples. The proxy package offers facilities to cumpute several distances. We use the function dist from this package specifying the Euclidean distance. 

library(proxy)
dtrain <- as.matrix(dist(x = xtrain, method = "euclidean",
                         by_rows = TRUE))
ditest <- as.matrix(dist(x = xitest, y = xtrain,
                         method = "euclidean", by_rows = TRUE))
dttest <- as.matrix(dist(x = xttest, y = xtrain,
                         method = "euclidean", by_rows = TRUE))

 Now we can train the model using the computing matrices. Once again, we train the self-training model. When a distance matrix is passed as x argument, the parameter dist is used with its default value “matrix”.

m.selfT2 <- selfTraining(x = dtrain, y = ytrain, dist = "matrix") 


To perform the classification task it necesary to supply the distance between the selected training instances and the instances to be classified. The selected training instances are selected during the training process. This instances are returned in the parameter included.insts contained in the model trained. Attending to this we select the columns of interest in the precalculated matrix ditest.

d <- ditest[, m.selfT2$included.insts]
p.selfT2 <- predict(m.selfT2, d)
resultI <- confusionMatrix(table(p.selfT2, yitest))$overall[1:2]


Since we use ditest the classification performed is considered as inductive. The results obtained are equal to the results in Example 1.
We only change the way of train and test the model. Training from a distance matrix can speedup the training process, avoiding the
distance calculations inside the training functions. 

To test the transductive capabilities of the model we perform the classification in a similar way.

d <- dttest[, m.selfT2$included.insts]
p.selfT2 <- predict(m.selfT2, d)
resultT <- confusionMatrix(table(p.selfT2, yttest))$overall[1:2]