这是一个可重现的例子:
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$rotation
(X
训练数据矩阵在哪里)但没有首先将数据居中,但它们是等效的。在回归中居中预测变量可能很有用。
## 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
表明我们的模型有两个测试集观察错误。