This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to do clustering using PCA method in R?

Hello,

I got a count Matrix from RSEM output and I want to do subgrouping/clustering the cells, Is there any easy documentation/ tutorial to do in R (or some other software)!

Please help! I am new to sequence analysis! Thanks in Advance!

rna-seq rna-seq r r

Thank you genomax, I would like to know how to do from RSEM read count matrix, This page says to normalize the data, How to do that?

I get this error while running

project.pca<-prcomp(t(DATA))
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

1 answer

over all code

y <- t(x) # x have all fpkm value which is transversed 
mypca -> ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE) # to draw pc

make sure you dont have any NAs else you have to use in

na.omit() #function

in

prcomp(na.omit(y))

thanks Kririka, Should I use expected read count or FPKM values? What is mean by NAs? I never used R before, please help

yes you can use any normalized form (i.e. FPKM TPM RPKM). NAs means "not available" some while importing data into R it will fill up all blank value with NAs So PCA willl not be generated over NAs So you need to give command that ignore NAs it will treat is as no value

thank you kritika, I ran the code you have written I got errors, then I modulated some, those are below, what is wrong? please help

          > x <- read.table("tpm_matrix.txt")
            > dim(x)
            [1] 24834    65
            > x[1:5, 1:5]
                  V1   V2   V3    V4   V5
            1   gene CN10 CN11  CN12 CN13
            2   Xkr4    0    0 64.02    0
            3  Sulf1    0    0     0    0
            4 Rhbdd1    0 5.89     0    0
            5  Ermap 0.18    0     0 0.79

> my.pca -> ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error: object 'my.pca' not found

> mypca <- ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error: could not find function "ggbiplot"

> mypca -> ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error: object 'mypca' not found

> mypca <- ggbiplot(prcomp(t(x[24833,65])), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error: could not find function "ggbiplot"

> my.pca <- biplot(prcomp(t(x[24833,65])), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error in scores[, choices] : subscript out of bounds
my.pca **<-** ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)

Error: object 'my.pca' not found You are assigning all the methods functions and there results to the new object that is my.pca so you need to use <- sign Error: could not find function "ggbiplot" ggbiplot is package you need to install and call for the same by using library(ggbiplot) please check in google how you need to download

Thank you so much.......!

Hi, I am back with another error as shown below!

> x <- read.table("tpmat.txt",header=T)
> y <- t(x[24833,65])
> my.pca <- ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(u[, choices], 2, d[choices]^obs.scale, FUN = "*") :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

If I run without specifying the no of columns and rows I get the following error

> x <- read.table("tpmat.txt",header=T)
> y <- t(x)
> my.pca <- ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
> x <- read.table("tpmat.txt",header=T)
> y <- t(x)
> my.pca <- ggbiplot(prcomp(y), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = 
TRUE,circle = TRUE)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

This should be

 my.pca <- ggbiplot(prcomp(na.omit(y)), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = 
TRUE,circle = TRUE)

or

 my.pca <- ggbiplot(prcomp(is.numeric(y)), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = 
TRUE,circle = TRUE)

Thanks for your continuous helping! Again I get the error!

1. is.numeric:

    > my.pca <- ggbiplot(prcomp(is.numeric(y)), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
    In addition: Warning message:
        In sweep(u[, choices], 2, d[choices]^obs.scale, FUN = "*") :
          STATS is longer than the extent of 'dim(x)[MARGIN]'

2. na.omit:

    >  my.pca <- ggbiplot(prcomp(na.omit(y)), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
    Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

If I do both,

> my.pca <- ggbiplot(prcomp(is.numeric(na.omit(y))), obs.scale = 1, varname.abbrev = FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(u[, choices], 2, d[choices]^obs.scale, FUN = "*") :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

can you show the file

my.pca <- ggbiplot(prcomp(is.numeric(y[,-1])), obs.scale = 1, varname.abbrev =    FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)

Try this

HI I am getting the same error, I was struggling with the single line code for more than one week!

my.pca <- ggbiplot(prcomp(is.numeric(y[,-1])), obs.scale = 1, varname.    FALSE,var.axes = FALSE,pc.biplot = TRUE,circle = TRUE)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(u[, choices], 2, d[choices]^obs.scale, FUN = "*") :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

Should I do any preprocessing? I just took the tpm values present in the RSEM output!

first try running prcomp

yeah I tries, it is fine, its doing well

But I couldn't plot after that.

Hello Kevin,

> summary(project.pca)
Importance of components:
                       PC1
Standard deviation       0
Proportion of Variance NaN
Cumulative Proportion   NA

This is what I get

Log in to answer this question.