标签: r-caret

使用插入包但在库中出错(e1071)

这是我的代码,非常标准,但我收到错误消息:

library(caret)
set.seed(32343)
modelFit = train(type~.,data=training, method='glm')
Run Code Online (Sandbox Code Playgroud)

错误消息:

Error in library(e1071) : there is no package called ‘e1071’
Run Code Online (Sandbox Code Playgroud)

任何的想法?谢谢!

r r-caret

60
推荐指数
2
解决办法
6万
查看次数

使用插入符号完全可重复的并行模型

当我在插入符号中运行2个随机森林时,如果我设置了一个随机种子,我会得到完全相同的结果:

library(caret)
library(doParallel)

set.seed(42)
myControl <- trainControl(method='cv', index=createFolds(iris$Species))

set.seed(42)
model1 <- train(Species~., iris, method='rf', trControl=myControl)

set.seed(42)
model2 <- train(Species~., iris, method='rf', trControl=myControl)

> all.equal(predict(model1, type='prob'), predict(model2, type='prob'))
[1] TRUE
Run Code Online (Sandbox Code Playgroud)

但是,如果我注册并行后端以加速建模,每次运行模型时都会得到不同的结果:

cl <- makeCluster(detectCores())
registerDoParallel(cl)

set.seed(42)
myControl <- trainControl(method='cv', index=createFolds(iris$Species))

set.seed(42)
model1 <- train(Species~., iris, method='rf', trControl=myControl)

set.seed(42)
model2 <- train(Species~., iris, method='rf', trControl=myControl)

stopCluster(cl)

> all.equal(predict(model1, type='prob'), predict(model2, type='prob'))
[1] "Component 2: Mean relative difference: 0.01813729"
[2] "Component 3: Mean relative difference: 0.02271638"
Run Code Online (Sandbox Code Playgroud)

有什么方法可以解决这个问题吗?一个建议是使用doRNG包,但train使用嵌套循环,目前不支持:

library(doRNG)
cl …
Run Code Online (Sandbox Code Playgroud)

r reproducible-research r-caret

42
推荐指数
2
解决办法
2万
查看次数

插入符号()预测与predict.glm()非常不同

我正在尝试使用10倍交叉验证来估计逻辑回归.

#import libraries
library(car); library(caret); library(e1071); library(verification)

#data import and preparation
data(Chile)              
chile        <- na.omit(Chile)  #remove "na's"
chile        <- chile[chile$vote == "Y" | chile$vote == "N" , ] #only "Y" and "N" required
chile$vote   <- factor(chile$vote)      #required to remove unwanted levels 
chile$income <- factor(chile$income)  # treat income as a factor
Run Code Online (Sandbox Code Playgroud)

目标是估计一个glm模型,该模型预测投票"Y"或"N"的结果取决于相关的解释变量,并且基于最终模型,计算混淆矩阵和ROC曲线以掌握不同阈值水平的模型行为.

型号选择导致:

res.chileIII <- glm(vote ~
                           sex       +
                           education +
                           statusquo ,
                           family = binomial(),
                           data = chile)
#prediction
chile.pred <- predict.glm(res.chileIII, type = "response")
Run Code Online (Sandbox Code Playgroud)

产生:

> head(chile.pred)
          1           2 …
Run Code Online (Sandbox Code Playgroud)

r glm confusion-matrix r-caret

30
推荐指数
1
解决办法
2万
查看次数

当我尝试预测R-caret中的类概率时出错

我用插入符号构建了一个模型.培训结束后,我收到以下警告:

警告消息:在train.default(x,y,weights = w,...)中:至少有一个类级别不是有效的R变量名称; 如果生成类概率,这可能会导致错误,因为变量名称将转换为:X0,X1

变量的名称是:

      str(train)
'data.frame':   7395 obs. of  30 variables:
 $ alchemy_category              : Factor w/ 13 levels "arts_entertainment",..: 2 8 6 6 11 6 1 6 3 8 ...
 $ alchemy_category_score        : num  3737 2052 4801 3816 3179 ...
 $ avglinksize                   : num  2.06 3.68 2.38 1.54 2.68 ...
 $ commonlinkratio_1             : num  0.676 0.508 0.562 0.4 0.5 ...
 $ commonlinkratio_2             : num  0.206 0.289 0.322 0.1 0.222 ...
 $ commonlinkratio_3             : num  0.0471 0.2139 0.1202 0.0167 …
Run Code Online (Sandbox Code Playgroud)

r predict r-caret

27
推荐指数
3
解决办法
4万
查看次数

在R中保存和加载模型

使用插入符号时,如何在训练后保存模型,并在以后加载(例如在不同的会话中)进行预测?

r r-caret

26
推荐指数
3
解决办法
2万
查看次数

警告消息:使用rpart在插入符号train()中"重新采样性能度量中缺少值"

我正在使用插入包来训练带有"rpart"包的模型;

tr = train(y ~ ., data = trainingDATA, method = "rpart")
Run Code Online (Sandbox Code Playgroud)

数据没有缺失值或NA,但在运行命令时会出现警告消息;

    Warning message:
In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,  :
  There were missing values in resampled performance measures.
Run Code Online (Sandbox Code Playgroud)

有谁知道(或者可以指出我在哪里找到答案)这个警告意味着什么?我知道它告诉我重新采样的性能测量中缺少值 - 但这究竟意味着什么,以及如何出现这样的情况?顺便说一下,这个predict() 功能适用于合身的模型,所以这只是我的好奇心.

r rpart r-caret

26
推荐指数
3
解决办法
3万
查看次数

来自插入符号中训练数据的ROC曲线

使用R包插入符号,如何根据train()函数的交叉验证结果生成ROC曲线?

说,我做以下事情:

data(Sonar)
ctrl <- trainControl(method="cv", 
  summaryFunction=twoClassSummary, 
  classProbs=T)
rfFit <- train(Class ~ ., data=Sonar, 
  method="rf", preProc=c("center", "scale"), 
  trControl=ctrl)
Run Code Online (Sandbox Code Playgroud)

训练函数遍历一系列mtry参数并计算ROC AUC.我想看看相关的ROC曲线 - 我该怎么做?

注意:如果用于采样的方法是LOOCV,那么rfFit将在rfFit$pred插槽中包含一个非空数据帧,这似乎正是我所需要的.但是,我需要用于"cv"方法(k-fold验证)而不是LOO.

另外:不,roc曾经包含在以前版本的插入符中的函数不是答案 - 这是一个低级函数,如果您没有每个交叉验证样本的预测概率,则不能使用它.

r roc r-caret

24
推荐指数
2
解决办法
3万
查看次数

如何在插入包中制作树形图?

我正在使用插入包来使用rpart包对数据建模.

library('caret')
data(iris)
formula <- as.formula(Species ~.)
t <- train(formula,iris,method = "rpart",cp=0.002,maxdepth=8)
plot(t)
Run Code Online (Sandbox Code Playgroud)

结果我得到了对象't',我试图绘制这个对象来获得树图.但结果看起来像这样: 在此输入图像描述

有没有办法从插入火车对象制作树图?

plot r rpart r-caret

21
推荐指数
3
解决办法
2万
查看次数

ConfusionMatrix中的错误数据和参考因子必须具有相同的级别数

我用R caret训练了一个树模型.我现在正在尝试生成混淆矩阵并继续收到以下错误:

confusionMatrix.default(predictionsTree,testdata $ catgeory)出错:数据和参考因子必须具有相同的级别数

prob <- 0.5 #Specify class split
singleSplit <- createDataPartition(modellingData2$category, p=prob,
                                   times=1, list=FALSE)
cvControl <- trainControl(method="repeatedcv", number=10, repeats=5)
traindata <- modellingData2[singleSplit,]
testdata <- modellingData2[-singleSplit,]
treeFit <- train(traindata$category~., data=traindata,
                 trControl=cvControl, method="rpart", tuneLength=10)
predictionsTree <- predict(treeFit, testdata)
confusionMatrix(predictionsTree, testdata$catgeory)
Run Code Online (Sandbox Code Playgroud)

生成混淆矩阵时会发生错误.两个对象的级别相同.我无法弄清问题是什么.它们的结构和水平如下.它们应该是一样的.任何帮助将非常感谢,因为它让我破解!

> str(predictionsTree)
 Factor w/ 30 levels "16-Merchant Service Charge",..: 28 22 22 22 22 6 6 6 6 6 ...
> str(testdata$category)
 Factor w/ 30 levels "16-Merchant Service Charge",..: 30 30 7 7 7 7 7 30 7 …
Run Code Online (Sandbox Code Playgroud)

r classification machine-learning r-caret

21
推荐指数
2
解决办法
5万
查看次数

为什么插入符号列车占用了这么多记忆?

当我训练只是使用时glm,一切正常,我甚至没有接近耗尽记忆.但是当我跑步时train(..., method='glm'),我的内存耗尽.

这是因为train为交叉验证的每次迭代存储了大量数据(或者无论trControl程序是什么)?我在看,trainControl我找不到如何防止这种...任何提示?我只关心性能总结和预测的响应.

(我知道它与存储参数调整网格搜索的每次迭代中的数据无关,因为我认为没有glm的网格.)

memory r glm r-caret

20
推荐指数
3
解决办法
8129
查看次数