即使允许使用NA,R函数prcomp也无法使用NA的值 [英] R function prcomp fails with NA's values even though NA's are allowed

查看:102
本文介绍了即使允许使用NA,R函数prcomp也无法使用NA的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用函数prcomp计算前两个主成分.但是,我的数据具有一些NA值,因此该函数引发错误.即使在帮助文件?prcomp

I am using the function prcomp to calculate the first two principal components. However, my data has some NA values and therefore the function throws an error. The na.action defined seems not to work even though it is mentioned in the help file ?prcomp

这是我的例子:

d <- data.frame(V1 = sample(1:100, 10), V2 = sample(1:100, 10))

prcomp(d, center = TRUE, scale = TRUE, na.action = na.omit)

d$V1[5] <- NA
d$V2[7] <- NA

prcomp(d, center = TRUE, scale = TRUE, na.action = na.omit)

我正在为Mac OS X使用最新的R版本2.15.1.

I am using the newest R version 2.15.1 for Mac OS X.

prcomp失败时,有人可以看到原因吗?

Can anybody see the reason while prcomp fails?

这是我的新示例:

d <- data.frame(V1 = sample(1:100, 10), V2 = sample(1:100, 10))

result <- prcomp(d, center = TRUE, scale = TRUE, na.action = na.omit)

result$x

d$V1[5] <- NA

result <- prcomp(~V1+V2, data=d, center = TRUE, scale = TRUE, na.action = na.omit)

result$x

是否可以在PC1和PC2中保留第5行?在我的真实数据集中,我当然有多于两列的变量,并且其中只有一些丢失了,我不想丢失隐藏在其他值中的其余信息!

is it possible to retain row 5 in PC1 and PC2? In my real data set I have of course more than two columns of variables and only some of them are missing and I do not want to lose the remaining information hidden in the other values!

推荐答案

是的,除非您使用formula接口,否则它看起来像一个功能"(错误),完全忽略了na.action.在进行R开发之前,已经列表.

Yeah, it looks like a "feature" (bug) that na.action is completely ignored unless you use the formula interface. This has been brought up before on the R Development list.

我认为应该对此进行记录或标记为错误.

I think that this should be documented or flagged as a bug.

请明确一点,这将起作用,因为它可以访问公式界面:

Just to be clear, this would work because it accesses the formula interface:

prcomp(~V1+V2, data=d, center = TRUE, scale = TRUE, na.action = na.omit)

这篇关于即使允许使用NA,R函数prcomp也无法使用NA的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆