Kanishk Jain
Kanishk Jain

Reputation: 103

Why am I getting Error in `[.data.frame`(data, , model.list$variables) : undefined columns selected in R?

I am getting undefined columns error in R. Here is a description of tran_data

train_data <- train[c("DURATION","HOURLY_WAGE", "WAGE_RATE_OF_PAY_FROM_HOUR", "OCCUPATION_NUM","CASE_STATUS_1.0","AGENT_PRESENT_1.0")]
> str(train_data)
data.frame: 70000 obs. of  6 variables:
 $ DURATION                  : num  0.0674 0.0674 0.0449 0.0562 0.0674 ...
 $ HOURLY_WAGE               : num  0.378 0.298 0.387 0.333 0.34 ...
 $ WAGE_RATE_OF_PAY_FROM_HOUR: num  0.396 0.302 0.391 0.333 0.354 ...
 $ OCCUPATION_NUM            : num  0.3 0.3 0.3 0.3 0.3 0.3 0.1 0.1 0.1 0.3 ...
 $ CASE_STATUS_1.0           : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ AGENT_PRESENT_1.0         : Factor w/ 2 levels "0","1": 2 1 1 2 2 1 2 2 2 1 ...

Code that is throwing an error

n <- neuralnet(train_data$AGENT_PRESENT_1.0~train_data$HOURLY_WAGE+
             train_data$DURATION+
             train_data$WAGE_RATE_OF_PAY_FROM_HOUR+
             train_data$CASE_STATUS_1.0+
             train_data$OCCUPATION_NUM,
             data=train_data,hidden = 1)

ERROR: Error in [.data.frame(data, , model.list$variables) : undefined columns selected

I tried unlist function but I am getting the same error. Any help on how to solve this?

Upvotes: 1

Views: 9344

Answers (1)

akrun
akrun

Reputation: 887158

The issue would be the formula needs the unquoted column names and not the values

library(neuralnet)
n <- neuralnet(`AGENT_PRESENT_1.0` ~ HOURLY_WAGE+
                  DURATION+
                  WAGE_RATE_OF_PAY_FROM_HOUR+
                  `CASE_STATUS_1.0`+
                   OCCUPATION_NUM,
                 data=train_data, hidden = 1)

Using a reproducible example

data(iris)

this works

n1 <- neuralnet(Species ~ Petal.Length + Petal.Width, iris, hidden = 1)

this results in error

n1 <- neuralnet(iris$Species ~ iris$Petal.Length + iris$Petal.Width, iris, hidden = 1)

Error in [.data.frame(data, , model.list$variables) : undefined columns selected

Upvotes: 1

Related Questions