2

为了在 ggbiplot 中的训练数据集中绘制预测的验证/测试数据集,如此所述,我想绑定/合并这两个数据集。

给定的 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 接受的对象?

4

0 回答 0