0

I'm practicing for logistic regression with R language. Instruction says USE ENTRIE DATASET AS TRAIN DATA, so I wrote full data on train with zero test data. As I run Caret package for confusion matrix, I can't receive result. what did I miss?

rm(list = ls())
Admin.df <- read.csv("SystemAdministrators.csv")

library(dummies)
Admin.df<-data.frame(Completed=Admin.df$Completed,dummy.data.frame(Admin.df, names = "Completed",sep="_",
                                                                  dummy.classes =c("Yes", "No")))

selected.var <- c(1:3)

set.seed(2)
train.index <- sample(c(1:dim(Admin.df)[1]), dim(Admin.df)[1]*1)
train.Admin.df <- Admin.df[train.index, selected.var]
valid.Admin.df <- Admin.df[-train.index, selected.var]

logit.reg <- glm(Admin.df$Completed_Yes ~ ., data = train.Admin.df, family = "binomial") 
options(scipen=999) 
summary(logit.reg)

logit.reg.pred <- predict(logit.reg, train.Admin.df, type = "response") # not use valid data since no data
logit.reg.pred.class<-ifelse(logit.reg.pred > 0.5, 1, 0)


library(caret)
confusionMatrix(as.factor(logit.reg.pred.class), as.factor(train.Admin.df$Completed_Yes))
confusionMatrix(as.factor(ifelse(logit.reg.pred > 0.5, "Completed_Yes", "Completed_No")), 
                as.factor(ifelse(train.Admin.df$Completed_Yes==1,"Completed_Yes", "Completed_No")))
Leah
  • 1
  • Hi, Leah, Welcome to Stack Overflow. Thanks for including the code you are using. You say that you can't get the result. What happens when you run this code? – jessi Sep 25 '19 at 15:42
  • it runs everything before Caret package. when I tried to run confusionMatrix, the error message shows Error in confusionMatrix.default(as.factor(logit.reg.pred.class), as.factor(train.Admin.df$Completed_Yes)) : the data cannot have more levels than the reference. ---- Since I use entire train data, there has zero valid data. – Leah Sep 25 '19 at 17:35
  • This is not about how much of your data set you have used as the training portion. I think this question may help you. [data cannot have more levels than the reference](https://stackoverflow.com/questions/38741997/how-to-solve-the-data-cannot-have-more-levels-than-the-reference-error-when-us) – jessi Sep 26 '19 at 12:24

0 Answers0