spatialShrunkenCentroids predict not working

66 views
Skip to first unread message

Pascal Egner

unread,
May 12, 2021, 5:31:37 AM5/12/21
to Cardinal MSI Help
Hey,

I am currently trying to use the predict function for spatialShrunkenCentroids, but i am getting the following error:

> boundingbox_ms_kmeans <- spatialKMeans(boundingbox_ms, r=1, k=3, method="gaussian")

> boundingbox_ms_ssc <- spatialShrunkenCentroids(boundingbox_ms, init=boundingbox_ms_kmeans, r=1, k=3, s=10)

> predict(object = boundingbox_ms_ssc, newx = boundingbox_ms)
calculating spatial weights...
predicting using spatial shrunken centroids...
r = 1, s = 10 
Error in log(rep(priors, each = ncol(x))) : 
  non-numeric argument to mathematical function

Am I doing something wrong or is this a bug? 

(I'm only using the same imzML image on both, the spatialShrunkenCentroids and predict function, for testing)

Melanie Föll

unread,
May 27, 2021, 12:01:23 PM5/27/21
to Cardinal MSI Help
Hi Pascal,

your code looks fine and dataset re-usage should not be an issue. Prediction from unsupervised methods is not so frequenlty performed and not necessarily advisable, so you probably discovered a bug. 
Maybe in the meantime, you can work with the segments of the k-means to perform supervised SSC analysis and prediction?

Cheers,
Melanie

Pascal Egner

unread,
May 27, 2021, 12:42:32 PM5/27/21
to Cardinal MSI Help
Hey Melanie,

thank you for your answer, it looks like I found a workaround in the last couple of days.

To use the predict function, the priors value (ssc@metadata[["priors"]]) in the SpatialShrunkenCentroids2 object must be set.  This is only the case when the "y" or "priors" argument is passed to the SpatialShrunkenCentroids function call. 
In my case I don't have these values prior to the function call, so after the SpatialShrunkenCentroids function is executed, I manually calculate and set the priors for the SpatialShrunkenCentroids2 object, based on the resultData.
So if anyone else has my problem, with the following code everything seems to work fine:

> ssc <- spatialShrunkenCentroids( boundingbox_ms, method="gaussian", r=1, s=10, k=3 )

> resultData <- ssc@resultData@listData[[1]][["class"]]

> my_priors <- table( resultData ) / length( resultData )

> ssc@metadata[["priors"]] <- my_priors

> pred <- predict( object = ssc, newx = boundingbox_ms )


Cheers,
Pascal



Melanie Föll

unread,
May 28, 2021, 3:58:01 AM5/28/21
to Cardinal MSI Help
Hi Pascal,

great to hear that you found a solution and thanks a lot for sharing it here!

Melanie
Reply all
Reply to author
Forward
0 new messages