首页 > 解决方案 > 创建数据分区不能正常工作(插入符号包)

问题描述

我正在尝试使用 createDataPartition() 使用训练和测试集制作机器学习模型。我能够为训练集制作一个线性模型,但我似乎无法将新数据拟合到现有模型中,例如数据集的两个主要组成部分。

这是机器学习课程中的一个例子,它给出了一个错误。

library(kernlab); library(caret)
data(spam)

inTrain <- createDataPartition(y=spam$type, p =0.75, list = FALSE)
training <- spam[inTrain,]
testing <- spam[-inTrain,]

    #preprocess the training data by log transform for skewed 
    #variables, and then extract the first two principal components 

preProc <- preProcess(log10(training[,-58]+1), method = "pca", pcaComp 
= 2)
preProc$rotation

    #create a train-dataset based on the prediction of the two PC's 
    #from the training set

trainPC <- predict(preProc, log10(training[,-58]+1))

     #train the 'type' versus all other variables model using a GLM, 
     #but based on the trainPC dataset, containing the two principal 
     #components

modelFit <- train(training$type ~ . ,  method="glm", data = trainPC)

Error in `[.data.frame`(data, , all.vars(Terms), drop = FALSE) : 
undefined columns selected

预期的结果是 modelFit 针对 training$type 进行了训练,但使用了新创建的 trainPC 数据集,该数据集是根据预处理数据预测的。

我认为问题在于 trainPC 数据仅包含两台 PC,如果我是正确的,则在训练时无法引用:

train(training$type ~ . 

提前致谢!

亚历山大

标签: rmachine-learningr-caret

解决方案


推荐阅读