'Rtsne: Perplexity is too large

I am trying to use tSNE on a gene expression matrix that has the following dimensions: 7x5000. I have removed low variance, low expression and duplicated values:

     ENSMUSG00000022037 ENSMUSG00000064351 ENSMUSG00000047517 ENSMUSG00000101111
852_1           18.04494           16.58238          14.760356           14.72078
852_2           18.33979           16.08849          15.846886           14.13721
852_3           17.27803           16.63105          13.483438           14.78686
852_4           18.08123           16.17240          13.854479           13.97815
853_1           15.87570           16.43745          10.016808           14.47457
853_2           14.13963           18.19087           8.654636           16.73305
853_3           17.95099           16.66351          17.109841           14.49093

Here is how I run tSNE:

tsne_out <- Rtsne(mat, dims = 3) 

But it gives me the following error:

Error in Rtsne.default(unique(t(highly_variable)), dims = 3) : 
  Perplexity is too large.

Can someone advise on what am I doing wrong?

Thanks!



Solution 1:[1]

I'm 2 years late, but after reading the @sm925's comment I went and checked the documentation (?Rtsne) and found:

perplexity    numeric; Perplexity parameter (should not be bigger
              than 3 * perplexity < nrow(X) - 1, see details for
              interpretation)

So basically we can reverse-calculate the highest acceptable perplexity:

my_Rtsne <- Rtsne(X = data.matrix(data),
                  perplexity = floor((nrow(data) - 1) / 3),
                  dims = 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mehrad Mahmoudian