R中的主成分分析

Joh*_*ith 2 r pca

使用该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
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 6

这是一个可重复的例子:

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])
Run Code Online (Sandbox Code Playgroud)

现在请注意,这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)
Run Code Online (Sandbox Code Playgroud)

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

test.p <- predict(pc, newdata = test[, 1:4])
Run Code Online (Sandbox Code Playgroud)

现在用它来预测班级

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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