I would like to know how can I draw a ROC plot with R. I have created a logistic regression model with k-fold cross validation.
dt3
- main dataset
dt3Training
- training split made from main dataset
dt3Test
- test split made from main dataset
Below is the code that used for logistic regression:
ctrl<- trainControl (method="repeatedcv", number = 10, repeats =5, savePredictions="TRUE"
modelfit <- train (Attrition~., data=dt3, method="glm", family="binomial", trControl=ctrl)
pred = predict (modelfit, newdata=dt3Test)
confusionMatrix(data=pred, dt3Test$Attrition)
My problem is that, pred
does not show up as a prediction, instead it shows as a data table. Therefore below code gives an error.
perf1 <- performance(pred,"tpr","fpr")
plot(perf1)
I would be really grateful if you can help me with this.
UPDATE: After viewing k-fold cross validation - how to get the prediction automatically? I changed my code to below:
library("caret", lib.loc="~/R/win-library/3.4")
load (df) ## load main dataset "df"
tc <- trainControl("cv",10,savePred=T) ##create folds
(fit<-train(Attrition~.,data=df,method="glm",family="binomial",trControl=tc)) ##train model, predict Attrition with all other variables
I would like to try code below by Claus Wilke however I got confused as I only have my main data (df) and my model (fit).
data.frame(predictor = predict(fit, df),
known.truth = fit$Attrition,
model = "fit")
or
data.frame(predictor = predict(fit, tc),
known.truth = tc$Attrition,
model = "fit")
Sorry if I am asking a really stupid question, but I don't have much time left for my project to finish. And I don't have previous experience with R.