Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R caret rpart returns Error in `[.data.frame`(m, labs) : undefined columns selected

I am running a classification for rpart. I needed to prepare the data into a sparse format to run multiple models to it .

When I run the rpart method, using this call:

control <- trainControl(method="repeatedcv", number=10, repeats=3)
#Metric Measurement for Model Performance
fitmetric <- "Accuracy"
set.seed(seed)

ptm <- proc.time()
adultFit.cart <- train(response~., data=adultTraining, method="rpart", metric=fitmetric, trControl=control,
                  parms = list( split = "information"),control=rpart.control(cp = 0.04))
proc.time() - ptm

I am getting this message:

`[.data.frame`(m, labs) : undefined columns selected

Can't seemed to figure out what is causing this since it runs good for all the other models

Here is the definition of df that I am using to test the function and the sample below:

> str(adultTraining)
'data.frame':   22793 obs. of  57 variables:
 $ age                                : num  53 37 42 37 30 23 34 25 32 43 ...
 $ fnlwgt                             : num  234721 284582 159449 280464 141297 ...
 $ educationnum                       : num  7 14 13 10 13 13 4 9 9 14 ...
 $ maritalstatus.Divorced             : num  0 0 0 0 0 0 0 0 0 1 ...
 $ maritalstatus.Married-AF-spouse    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ maritalstatus.Married-civ-spouse   : num  1 1 1 1 1 0 1 0 0 0 ...
 $ maritalstatus.Married-spouse-absent: num  0 0 0 0 0 0 0 0 0 0 ...
 $ maritalstatus.Never-married        : num  0 0 0 0 0 1 0 1 1 0 ...
 $ maritalstatus.Separated            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ maritalstatus.Widowed              : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.?                       : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Adm-clerical            : num  0 0 0 0 0 1 0 0 0 0 ...
 $ occupation.Armed-Forces            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Craft-repair            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Exec-managerial         : num  0 1 1 1 0 0 0 0 0 1 ...
 $ occupation.Farming-fishing         : num  0 0 0 0 0 0 0 1 0 0 ...
 $ occupation.Handlers-cleaners       : num  1 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Machine-op-inspct       : num  0 0 0 0 0 0 0 0 1 0 ...
 $ occupation.Other-service           : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Priv-house-serv         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Prof-specialty          : num  0 0 0 0 1 0 0 0 0 0 ...
 $ occupation.Protective-serv         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Sales                   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Tech-support            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ occupation.Transport-moving        : num  0 0 0 0 0 0 1 0 0 0 ...
 $ race.Amer-Indian-Eskimo            : num  0 0 0 0 0 0 1 0 0 0 ...
 $ race.Asian-Pac-Islander            : num  0 0 0 0 1 0 0 0 0 0 ...
 $ race.Black                         : num  1 0 0 1 0 0 0 0 0 0 ...
 $ race.Other                         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ race.White                         : num  0 1 1 0 0 1 0 1 1 1 ...
 $ sex.Female                         : num  0 1 0 0 0 1 0 0 0 1 ...
 $ sex.Male                           : num  1 0 1 1 1 0 1 1 1 0 ...
 $ hoursperweek                       : num  40 40 40 80 40 30 45 35 40 45 ...
 $ cntrymap.British-Commonwealth      : num  0 0 0 0 1 0 0 0 0 0 ...
 $ cntrymap.China                     : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.Euro-1                    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.Euro-2                    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.Latin-America             : num  0 0 0 0 0 0 1 0 0 0 ...
 $ cntrymap.Other                     : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.SoutEast-Asia             : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.South-America             : num  0 0 0 0 0 0 0 0 0 0 ...
 $ cntrymap.United-States             : num  1 1 1 1 0 1 0 1 1 1 ...
 $ relationship_new.Not-in-family     : num  0 0 0 0 0 0 0 0 0 0 ...
 $ relationship_new.Other-relative    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ relationship_new.Own-child         : num  0 0 0 0 0 1 0 1 0 0 ...
 $ relationship_new.Spouse            : num  1 1 1 1 1 0 1 0 0 0 ...
 $ relationship_new.Unmarried         : num  0 0 0 0 0 0 0 0 1 1 ...
 $ workclass_new.?                    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workclass_new.Federal-gov          : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workclass_new.Local-gov            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workclass_new.Never-worked         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workclass_new.Private              : num  1 1 1 1 0 1 1 0 1 0 ...
 $ workclass_new.Self-emp-inc         : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workclass_new.Self-emp-not-inc     : num  0 0 0 0 0 0 0 1 0 1 ...
 $ workclass_new.State-gov            : num  0 0 0 0 1 0 0 0 0 0 ...
 $ capitalgainloss                    : num  0 0 5178 0 0 ...
 $ response                           : Factor w/ 2 levels "GT50K","LE50K": 2 2 1 1 1 2 2 2 2 1 ...

SAMPLE DATA: As recommended by MFlick here is sample of data

dput(head(adultTraining))

structure(list(age = c(53, 37, 42, 37, 30, 23), fnlwgt = c(234721, 
284582, 159449, 280464, 141297, 122272), educationnum = c(7, 
14, 13, 10, 13, 13), maritalstatus.Divorced = c(0, 0, 0, 0, 0, 
0), `maritalstatus.Married-AF-spouse` = c(0, 0, 0, 0, 0, 0), 
    `maritalstatus.Married-civ-spouse` = c(1, 1, 1, 1, 1, 0), 
    `maritalstatus.Married-spouse-absent` = c(0, 0, 0, 0, 0, 
    0), `maritalstatus.Never-married` = c(0, 0, 0, 0, 0, 1), 
    maritalstatus.Separated = c(0, 0, 0, 0, 0, 0), maritalstatus.Widowed = c(0, 
    0, 0, 0, 0, 0), `occupation.?` = c(0, 0, 0, 0, 0, 0), `occupation.Adm-clerical` = c(0, 
    0, 0, 0, 0, 1), `occupation.Armed-Forces` = c(0, 0, 0, 0, 
    0, 0), `occupation.Craft-repair` = c(0, 0, 0, 0, 0, 0), `occupation.Exec-managerial` = c(0, 
    1, 1, 1, 0, 0), `occupation.Farming-fishing` = c(0, 0, 0, 
    0, 0, 0), `occupation.Handlers-cleaners` = c(1, 0, 0, 0, 
    0, 0), `occupation.Machine-op-inspct` = c(0, 0, 0, 0, 0, 
    0), `occupation.Other-service` = c(0, 0, 0, 0, 0, 0), `occupation.Priv-house-serv` = c(0, 
    0, 0, 0, 0, 0), `occupation.Prof-specialty` = c(0, 0, 0, 
    0, 1, 0), `occupation.Protective-serv` = c(0, 0, 0, 0, 0, 
    0), occupation.Sales = c(0, 0, 0, 0, 0, 0), `occupation.Tech-support` = c(0, 
    0, 0, 0, 0, 0), `occupation.Transport-moving` = c(0, 0, 0, 
    0, 0, 0), `race.Amer-Indian-Eskimo` = c(0, 0, 0, 0, 0, 0), 
    `race.Asian-Pac-Islander` = c(0, 0, 0, 0, 1, 0), race.Black = c(1, 
    0, 0, 1, 0, 0), race.Other = c(0, 0, 0, 0, 0, 0), race.White = c(0, 
    1, 1, 0, 0, 1), sex.Female = c(0, 1, 0, 0, 0, 1), sex.Male = c(1, 
    0, 1, 1, 1, 0), hoursperweek = c(40, 40, 40, 80, 40, 30), 
    `cntrymap.British-Commonwealth` = c(0, 0, 0, 0, 1, 0), cntrymap.China = c(0, 
    0, 0, 0, 0, 0), `cntrymap.Euro-1` = c(0, 0, 0, 0, 0, 0), 
    `cntrymap.Euro-2` = c(0, 0, 0, 0, 0, 0), `cntrymap.Latin-America` = c(0, 
    0, 0, 0, 0, 0), cntrymap.Other = c(0, 0, 0, 0, 0, 0), `cntrymap.SoutEast-Asia` = c(0, 
    0, 0, 0, 0, 0), `cntrymap.South-America` = c(0, 0, 0, 0, 
    0, 0), `cntrymap.United-States` = c(1, 1, 1, 1, 0, 1), `relationship_new.Not-in-family` = c(0, 
    0, 0, 0, 0, 0), `relationship_new.Other-relative` = c(0, 
    0, 0, 0, 0, 0), `relationship_new.Own-child` = c(0, 0, 0, 
    0, 0, 1), relationship_new.Spouse = c(1, 1, 1, 1, 1, 0), 
    relationship_new.Unmarried = c(0, 0, 0, 0, 0, 0), `workclass_new.?` = c(0, 
    0, 0, 0, 0, 0), `workclass_new.Federal-gov` = c(0, 0, 0, 
    0, 0, 0), `workclass_new.Local-gov` = c(0, 0, 0, 0, 0, 0), 
    `workclass_new.Never-worked` = c(0, 0, 0, 0, 0, 0), workclass_new.Private = c(1, 
    1, 1, 1, 0, 1), `workclass_new.Self-emp-inc` = c(0, 0, 0, 
    0, 0, 0), `workclass_new.Self-emp-not-inc` = c(0, 0, 0, 0, 
    0, 0), `workclass_new.State-gov` = c(0, 0, 0, 0, 1, 0), capitalgainloss = c(0, 
    0, 5178, 0, 0, 0), response = structure(c(2L, 2L, 1L, 1L, 
    1L, 2L), .Label = c("GT50K", "LE50K"), class = "factor")), .Names = c("age", 
"fnlwgt", "educationnum", "maritalstatus.Divorced", "maritalstatus.Married-AF-spouse", 
"maritalstatus.Married-civ-spouse", "maritalstatus.Married-spouse-absent", 
"maritalstatus.Never-married", "maritalstatus.Separated", "maritalstatus.Widowed", 
"occupation.?", "occupation.Adm-clerical", "occupation.Armed-Forces", 
"occupation.Craft-repair", "occupation.Exec-managerial", "occupation.Farming-fishing", 
"occupation.Handlers-cleaners", "occupation.Machine-op-inspct", 
"occupation.Other-service", "occupation.Priv-house-serv", "occupation.Prof-specialty", 
"occupation.Protective-serv", "occupation.Sales", "occupation.Tech-support", 
"occupation.Transport-moving", "race.Amer-Indian-Eskimo", "race.Asian-Pac-Islander", 
"race.Black", "race.Other", "race.White", "sex.Female", "sex.Male", 
"hoursperweek", "cntrymap.British-Commonwealth", "cntrymap.China", 
"cntrymap.Euro-1", "cntrymap.Euro-2", "cntrymap.Latin-America", 
"cntrymap.Other", "cntrymap.SoutEast-Asia", "cntrymap.South-America", 
"cntrymap.United-States", "relationship_new.Not-in-family", "relationship_new.Other-relative", 
"relationship_new.Own-child", "relationship_new.Spouse", "relationship_new.Unmarried", 
"workclass_new.?", "workclass_new.Federal-gov", "workclass_new.Local-gov", 
"workclass_new.Never-worked", "workclass_new.Private", "workclass_new.Self-emp-inc", 
"workclass_new.Self-emp-not-inc", "workclass_new.State-gov", 
"capitalgainloss", "response"), row.names = c(4L, 6L, 10L, 11L, 
12L, 13L), class = "data.frame")
like image 331
E B Avatar asked Sep 22 '17 14:09

E B


2 Answers

I had the same issue. The reason was that I was using invalid column names.

Before you create your Train and Test data frames, try this:

# Make Valid Column Names 
colnames(df) <- make.names(colnames(df))
like image 61
R overflow Avatar answered Nov 08 '22 11:11

R overflow


I think the issue is that rpart is having difficulties with non-standard variable names that are created using the formula method (such as cntrymap.South-America). Try using the non-formula method:

set.seed(12311)
adultFit.cart <-
  train(
    x = adultTraining[, names(adultTraining) != "response"],
    y = adultTraining$response,
    method = "rpart",
    metric = fitmetric,
    trControl = control,
    parms = list(split = "information")
  )

Also note that you are trying to both tune the complexity parameter (method="rpart") and set it (rpart.control(cp = 0.04))

like image 45
topepo Avatar answered Nov 08 '22 10:11

topepo