我使用插入库来计算二元分类问题的类概率和预测,使用10倍交叉验证和5次重复.
现在我有一个TRUE(每个数据点的观测值)值,PREDICTED(通过算法)值,Class 0概率和Class 1概率,它们被算法用来预测类标签.
现在我如何roc使用其中一个ROCR或pROC库创建一个对象然后计算auc值?
假设我将所有这些值存储在predictionsdataframe中.例如predictions$pred,predictions$obs分别是预测值和真值,依此类推......
我目前正在使用'randomForest'软件包开发预测模型.
适合我的模型如下
rf <- foreach(ntree=rep(10, 3), .combine= combine, .packages='randomForest') %dopar% {
randomForest(bou~.,data=train, trees=50, importance=TRUE)}
Run Code Online (Sandbox Code Playgroud)
当使用'caret'包中的'confusionMatrix'时,我得到以下结果:
我想知道是否可以在模型中将正类设置为1.我在包描述中搜索但是找不到任何关于它的信息.
非常感谢你.
编辑:我找到了.它是'caret'包中'confusionMatrix'命令的一个选项.我在错误的地方跋涉.如果需要,这是一个例子.
confusionMatrix(predicted,true_values,positive='1')
Run Code Online (Sandbox Code Playgroud)
我应该留下我的帖子还是删除它?
当我只使用mtry参数作为tuingrid,它工作,但当我添加ntree参数时,错误变为Error in train.default(x, y, weights = w, ...): The tuning parameter grid should have columns mtry.代码如下:
require(RCurl)
require(prettyR)
library(caret)
url <- "https://raw.githubusercontent.com/gastonstat/CreditScoring/master/CleanCreditScoring.csv"
cs_data <- getURL(url)
cs_data <- read.csv(textConnection(cs_data))
classes <- cs_data[, "Status"]
predictors <- cs_data[, -match(c("Status", "Seniority", "Time", "Age", "Expenses",
"Income", "Assets", "Debt", "Amount", "Price", "Finrat", "Savings"), colnames(cs_data))]
train_set <- createDataPartition(classes, p = 0.8, list = FALSE)
set.seed(123)
cs_data_train = cs_data[train_set, ]
cs_data_test = cs_data[-train_set, ]
# Define the tuned parameter
grid …Run Code Online (Sandbox Code Playgroud) 第一次在这里发布海报,为新秀错误道歉
我使用R中的插入包进行分类.我在训练集上使用重复的10倍交叉验证来拟合一些模型(GBM,线性SVM,NB,LDA).使用自定义trainControl,插入符号甚至可以为我提供一系列模型性能指标,如ROC,Spec/sens,Kappa,测试折叠的准确度.真的很棒.我希望还有一个指标:模型校准的一些衡量标准.
我注意到插入符号中有一个功能可以创建校准图来估计数据部分模型的一致性.在交叉验证的模型构建过程中,是否可以为每个测试折叠计算插入符号?或者它只能应用于我们正在进行预测的一些新的数据吗?
对于某些情况,目前我有这样的事情:
fitControl <- trainControl(method = "repeatedcv", repeats=2, number = 10, classProbs = TRUE, summaryFunction = custom.summary)
gbmGrid <- expand.grid(.interaction.depth = c(1,2,3),.n.trees = seq(100,800,by=100),.shrinkage = c(0.01))
gbmModel <- train(y= train_target, x = data.frame(t_train_predictors),
method = "gbm",
trControl = fitControl,
tuneGrid = gbmGrid,
verbose = FALSE)
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,我使用~25个数字预测器,N = 2,200,预测一个两级因子.
非常感谢您的任何帮助/建议.亚当
我正在尝试使用代码在插入符号中完全可重现的并行模型,但不了解如何设置种子对象中的向量的大小.对于gbm,我有4个调整参数,共有11个不同的级别,我的调优网格中有54行.如果我指定任何值<18作为下面"for(i in 1:10)"行中的最后一个值,我会收到一个错误:"坏种子:种子对象应该是长度为11的列表,其中包含10个整数向量大小为18,最后一个列表元素有一个整数." 为什么18?对于> 18(例如54)的值,它也会运行没有错误 - 为什么?非常感谢您的帮助.以下是基于http://topepo.github.io/caret/training.html,添加了一些内容.
library(mlbench)
data(Sonar)
str(Sonar[, 1:10])
library(caret)
library(doParallel)
set.seed(998)
inTraining <- createDataPartition(Sonar$Class, p = .75, list = FALSE)
training <- Sonar[ inTraining,]
testing <- Sonar[-inTraining,]
grid <- expand.grid(n.trees = seq(50,150,by=50), interaction.depth = seq(1,3,by=1),
shrinkage = seq(.09,.11,by=.01),n.minobsinnode=seq(8,10,by=2))
# set seed to run fully reproducible model in parallel mode using caret
set.seed(825)
seeds <- vector(mode = "list", length = 11) # length is = (n_repeats*nresampling)+1
for(i in 1:10) seeds[[i]]<- sample.int(n=1000, 11) # ...the number of …Run Code Online (Sandbox Code Playgroud) 我使用插入符号包创建nnet模型,并使用以下代码提取预测值:
nnet<-predict(my_model, newdata = my_new_data)
nnet
[1] -0.1468207
Run Code Online (Sandbox Code Playgroud)
我还创建以下输出,从而可以查看最佳模型参数,如下所示:
Resampling results across tuning parameters:
size decay RMSE Rsquared RMSE SD Rsquared SD
10 0.001 0.01867841 0.4789708 0.002538599 0.12778927
10 0.100 0.02349088 0.1233067 0.001859455 0.10188046
12 0.001 0.01826047 0.5059824 0.002630588 0.12962511
12 0.100 0.02348553 0.1238252 0.001890646 0.09851303
15 0.001 0.01795350 0.5289120 0.003021449 0.13908835
15 0.100 0.02318972 0.1429446 0.001932714 0.11156927
RMSE was used to select the optimal model using the smallest value.
The final values used for the model were size = …Run Code Online (Sandbox Code Playgroud) 我正在研究一个文本多类分类项目,我需要构建文档/术语矩阵,并用R语言进行训练和测试.
我已经有了不适合R中基矩阵的有限维度的数据集,并且需要构建大的稀疏矩阵才能对例如100k的推文进行分类.我正在使用quanteda软件包,因为它现在比包tm更有用和可靠,其中使用字典创建DocumentTermMatrix,使得过程难以置信地使用小数据集.目前,正如我所说的,我使用quanteda来构建等效的Document Term Matrix容器,稍后我将其转换为data.frame来执行培训.
我想知道是否有办法建立这么大的矩阵.我一直在阅读有关的bigmemory包,允许这种容器的,但我不知道它会与插入符号为后来的分类.总的来说,我想了解这个问题并构建一个解决方法,以便能够处理更大的数据集,因为RAM不是(大)问题(32GB),但我正试图找到一种方法来做到这一点,我觉得完全失去了关于它.
我试图更多地了解这个caret包,并遇到了一个我不确定如何解决的障碍.
#loading up libraries
library(MASS)
library(caret)
library(randomForest)
data(survey)
data<-survey
#create training and test set
split <- createDataPartition(data$W.Hnd, p=.8)[[1]]
train<-data[split,]
test<-data[-split,]
#creating training parameters
control <- trainControl(method = "cv",
number = 10,
p =.8,
savePredictions = TRUE,
classProbs = TRUE,
summaryFunction = "twoClassSummary")
#fitting and tuning model
tuningGrid <- data.frame(.mtry = floor(seq(1 , ncol(train) , length = 6)))
rf_tune <- train(W.Hnd ~ . ,
data=train,
method = "rf" ,
metric = "ROC",
trControl = control)
Run Code Online (Sandbox Code Playgroud)
不断收到错误:
Error in evalSummaryFunction(y, …Run Code Online (Sandbox Code Playgroud) 使用插入符号中的时间分割及其参数,如何使用xyz行拆分数据,每行的长度为12?
理想情况下,还要考虑60-20-20火车测试-验证比率。
我应该这样设置吗:
initialWindow = 12,horizon = 12,fixedWindow = TRUE?
我已经阅读了文档,但是对我来说仍然不清楚。
我试图在多类预测问题上拟合xgboost模型,并希望caret用来进行超参数搜索.为了测试包,我使用了以下代码,当我不train使用trainControl 提供对象时需要20秒
# just use one parameter combination
xgb_grid_1 <- expand.grid(
nrounds = 1,
eta = 0.3,
max_depth = 5,
gamma = 0,
colsample_bytree=1,
min_child_weight=1
)
# train
xgb_train_1 = train(
x = as.matrix(sparse_train),
y = conversion_tbl$y_train_c ,
trControl = trainControl(method="none", classProbs = TRUE, summaryFunction = multiClassSummary),
metric="logLoss",
tuneGrid = xgb_grid_1,
method = "xgbTree"
)
Run Code Online (Sandbox Code Playgroud)
但是,当我提供traintrainControl对象时,代码永远不会完成..或者花费很长时间(至少它完成了15分钟.
xgb_trcontrol_1 <- trainControl(
method = "cv",
number = 2,
verboseIter = TRUE,
returnData = FALSE,
returnResamp = "none", …Run Code Online (Sandbox Code Playgroud)