我正在使用prcomp
函数来计算前两个主成分。但是,我的数据中有一些NA值,因此该函数会抛出一个错误。即使在帮助文件?prcomp
中提到了na.action
定义,看起来也没有起作用。
以下是我的示例:
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版本2.15.1的R。
有人能看出为什么prcomp
会失败吗?
这是我的新示例:
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行是否可能?在我的真实数据集中,当然有多个变量列,只有其中一些缺失,并且我不想失去其他值中隐藏的剩余信息!