如何绑定/合并prcomp并预测r中的数据?

时间:2016-04-17 10:44:11

标签: r pca ggbiplot

要在ggbiplot中的训练数据集中绘制预测的验证/测试数据集作为地址here,我想绑定/合并这两个数据集。

给定的mwe是:

library(ggbiplot)
data(wine)

##pca on the wine dataset used as training data
wine.pca <- prcomp(wine, center = TRUE, scale. = TRUE)

##add the wine.classes as a column to the dataset
wine$class <- wine.class

##simulate test data by generating three new wine classes
wine.new.1 <- wine[c(sample(1:nrow(wine), 25)),]
wine.new.2 <- wine[c(sample(1:nrow(wine), 43)),]
wine.new.3 <- wine[c(sample(1:nrow(wine), 36)),]

##Predict PCs for the new classes by transforming 
#them using the predict.prcomp function
pred.new.1 <- predict(wine.pca, newdata = wine.new.1)
pred.new.2 <- predict(wine.pca, newdata = wine.new.2)
pred.new.3 <- predict(wine.pca, newdata = wine.new.3)

##simulate the classes for the new sorts
wine.new.1$class <- rep("new.wine.1", nrow(wine.new.1))
wine.new.2$class <- rep("new.wine.2", nrow(wine.new.2))
wine.new.3$class <- rep("new.wine.3", nrow(wine.new.3))

我一直在使用:

df.train.pred <- rbind(wine.pca$x, pred.new.1, pred.new.2, pred.new.3)

将两者融合但ggbiplot返回错误,因为它期望类prcomp,princomp,PCA或lda的对象

如何整合这两个以便它们成为ggbiplot接受的对象?

0 个答案:

没有答案