3

使用该prcomp函数,我如何使用从同一数据集上的数据集派生的无监督主成分拆分为测试和训练?

train <- sample(1:nrow(auto), 60000)
x <- as.matrix(auto[,-1])  ##Covariates
y <- auto[,1]                   ##Response
pc <- prcomp(x)             ##Find Principal Components

data <- data.frame(y=y, (x %*% pc$rotation[,1:9]))
fit <- glm(y ~ ., data=data[train,], family="binomial")   ##Train It

prediction <- predict(fit, newdata=data) > 0  ##Prediction on Entire Data Set

error <- mean(y[-train]] != prediction[-train])  ##Mean out of Sample error
4

2 回答 2

7

这是一个可重现的例子:

set.seed(1)
want <- sample(50, 40)
Iris <- iris[c(51:100, 101:150), ] ## only keep versicolor and virginica
## take our training and test sets
train <- droplevels(Iris[c((1:50)[want], (51:100)[want]), , drop = FALSE])
test <- droplevels(Iris[c((1:50)[-want], (51:100)[-want]), , drop = FALSE])

## fit the PCA
pc <- prcomp(train[, 1:4])

现在请注意,这pc$x是旋转后的数据。您使用X %*% pc$rotationX训练数据矩阵在哪里)但没有首先将数据居中,但它们是等效的。在回归中居中预测变量可能很有用。

## create data frame for logistic regression
mydata <- data.frame(Species = train[, "Species"], pc$x)
## ...and fit the model
mod <- glm(Species ~ PC1, data = mydata, family = binomial)

预测 PC1 上测试集数据的分数;也就是说,使用与形成训练数据的 PC 相同的旋转来旋转测试集。为此,我们可以使用predict()类的方法"prcomp"

test.p <- predict(pc, newdata = test[, 1:4])

现在用它来预测类

pred <- predict(mod, newdata = data.frame(test.p), type = "response")
pred

> pred
         56          66          67          71          72 
0.080427399 0.393133104 0.092661480 0.395813527 0.048277608 
         74          76          82          87          95 
0.226191156 0.333553423 0.003860679 0.617977807 0.029469167 
        106         116         117         121         122 
0.999648054 0.922145431 0.924464339 0.989271655 0.318477762 
        124         126         132         137         145 
0.581235903 0.995224501 0.999770995 0.964825109 0.988121496 
> 1 - pred
          56           66           67           71           72 
0.9195726006 0.6068668957 0.9073385196 0.6041864731 0.9517223918 
          74           76           82           87           95 
0.7738088439 0.6664465767 0.9961393215 0.3820221934 0.9705308332 
         106          116          117          121          122 
0.0003519463 0.0778545688 0.0755356606 0.0107283449 0.6815222382 
         124          126          132          137          145 
0.4187640970 0.0047754987 0.0002290047 0.0351748912 0.0118785036

pred包含测试观察是Iris virginica的概率。请注意,glm()当响应是一个因素(如本例中)时,该因素的第一级(此处versicolor)被视为失败或0,第二个和后续级别指示成功或1。由于在这个例子中只有两个类,模型是根据versicolor; 1 - pred将给出 的预测概率virginica

我不遵循您在问题中包含的错误计算,因此将由您自行解决。然而,可以通过以下方式生成模型成功的交叉分类表:

> predSpecies <- factor(ifelse(pred >= 0.5, "virginica", "versicolor"))
> table(test$Species, predSpecies)
            predSpecies
             versicolor virginica
  versicolor          9         1
  virginica           1         9

表明我们的模型有两个测试集观察错误。

于 2012-06-04T12:46:30.420 回答
2

作为第一步,您需要将数据拆分为训练和测试:否则 PC 分数远非独立。

即 PCA 旋转仅从 x [train,] 计算!

然后将相同的旋转应用于 x [test,]

正如@Joran 所说,对于其他一切,都需要可重现的代码。

于 2012-06-04T10:47:44.247 回答