转置数据上的 PCA [英] PCA on transposed data
问题描述
我正在使用 R 进行一些 PCA 分析.一切正常,直到我想到我应该处理我的数据集的转置.然而,当我尝试对转置数据集进行 PCA 时,我无法解决它!
I am using R to do some PCA analysis. Everything was working fine until it occurred to me that I should be dealing with the transpose of my data set. However when I tried to do PCA on the transposed data set I could not get it to work out!
> sum(is.na(data_t))
[1] 1367
> dim(data_t)
[1] 599 9505
> data_t[1:4,1:4]
2'-PDE 7A5 A1BG A2M
TCGA.A1.A0SD.01A.11R.A115.07 0.0153750 2.4105 0.9493333 0.24200
TCGA.A1.A0SE.01A.11R.A084.07 0.4669375 0.3635 0.2798333 1.03850
TCGA.A1.A0SH.01A.11R.A084.07 -0.0295625 1.8550 0.7486667 1.16050
TCGA.A1.A0SJ.01A.11R.A084.07 0.7919375 1.4080 0.7500000 1.67775
> pca2<-princomp(~.,data=data_t, na.action=na.omit)
Error in `[.data.frame`(mf, , x) : undefined columns selected
> pca2<-princomp(data_t, na.action=na.omit)
Error in princomp.default(data_t, na.action = na.omit) :
'princomp' can only be used with more units than variables
事实证明,如果变量多于单位,则不能使用 princomp.但是您可以使用 prcomp(参见 R - 'princomp' 只能用于比变量更多的单位),但我仍然遇到错误!
Turns out that you cannot use princomp if you have more variables than units. But you can use prcomp (see R - 'princomp' can only be used with more units than variables) but I still get errors with that!
> pca2<-prcomp(data_t,na.action=na.omit)
Error in svd(x, nu = 0) : infinite or missing values in 'x'
> pca2<-prcomp(~ ., data=data_t, na.action=na.omit, scale=TRUE)
Error in `[.data.frame`(mf, , x) : undefined columns selected
推荐答案
我遇到了同样的问题.对我来说,当我为我的 data.frame
分配列名(非数字)时,它起作用了.例如,当 colnames(mydf)
是 (1,2,3,4,5)
时,我得到这个错误:
I had the same problem. For me, it worked when I assigned column names (other than numeric) to my data.frame
.
For example, when colnames(mydf)
was (1,2,3,4,5)
, I got this error:
Error in [.data.frame(mf, , x) : undefined columns selected
我所做的是:
colnames(mydf) <- paste("var", 1:5, sep="")
然后运行princomp
函数:
mypca <- princom(~. , data=myrdf, cor=F, na.action=na.exclude)
并且没有问题.
这篇关于转置数据上的 PCA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!