Plotting predict function

When using the predict function, you get a table-like output for the classification. It would be nice if there was a way to overlay the predicted points on the plotIndiv with the background shading. This would be a nice way to visualize the data.

Hi @wanani,

Thanks for the nice suggestion. The predict function does calculate the variates needed for the visualisation and it is just a matter of tweaking the plotIndiv function to perform your desired behaviour. We’ll certainly look into that and its utility and will update you. In the meantime, you’re more than welcome to share your code through a pull request if you happen to implement it.

Best,

Al

Hello, I would also like to combine the predictions of the test data with the background shading of the model to visualise how the new data points are predicted. Is there a possibility to do this?
Thanks!

There is not an implemented way to do this yet, however, you can get it to work nonetheless. Here is a code chunk which should achieve what you’re after. I haven’t included the resulting plot just because the image refuses to render in this forum for some reason - however, on my machine I was able to achieve expected results.

Note that we first generate our predictions in the normal way (generate a training model and run the predict() function on testing samples). Then we generate a “testing model”. This would not usually be done - but it will make sense why we do this shortly. Using this testing model, we can generate our prediction background. The crucial step is then replacing the variates of the testing model with our predicted variates from the training model.

This is why we generated this “testing model” - plotIndiv() requires a mixOmics type object and really only uses the variates component. So we replace the variates with our predicted variates, et voila!

# read in data
data("nutrimouse") 

# extract X and Y
X <- nutrimouse$gene 
Y <- nutrimouse$diet

#  subset data into training and testing sets
X.train <- X[1:35,]
X.test <- X[36:40,]

Y.train <- Y[1:35]
Y.test <- Y[36:40]

# form training model
model.train <- plsda(X.train, Y.train)

# make predictions using training model on testing samples
preds <- predict(model.train, X.test)

# generating testing model
model.test <- plsda(X.test, Y.test)

# using testing model to form background predictions
bgp <- background.predict(model.test, comp.predicted = 2)

# IMPORTANT STEP: replace the variates of the testing model with the 
#                 predicted variates using the training model
model.test$variates$X <- preds$variates

# plot
plotIndiv(model.test, background = bgp)