Juli Avlokhashvili
Juli Avlokhashvili

Reputation: 33

neural net - undefined column selected

Data Partition

I have several factor variables so I decided to use dummy variables instead for ANN.

set.seed(222)
m <- model.matrix( ~price+month+year+area+larea+otype+cid+rid+did+renovation+nrooms+nbeds+nbaths+nbalcs+wfloor+status, data = data)
ind <- sample(2, nrow(m), replace = TRUE, prob = c(0.8, 0.2))
training <- m[ind==1,]
testing <- m[ind==2,]

neural net

n <- neuralnet(price~.,
                data = training,
                hidden = 1,
                err.fct = "sse",
                linear.output = FALSE)

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

I use dot because I have 94 explanatory variables. when I run price on two explanatory variables to see what the problem was it worked. There is some problem with dot, is it only used for linear regression and I got it wrong? how can I solve this problem?

Upvotes: 2

Views: 3052

Answers (1)

akrun
akrun

Reputation: 887158

The model.matrix is a matrix and not a data.frame. According to ?neuralnet

data - a data frame containing the variables specified in formula.


So, we may need to convert to data.frame with as.data.frame as this works correctly with data.frame input

library(neuralnet)
data(iris)
nn <- neuralnet(Species~ ., iris, 
        linear.output = FALSE, hidden = 1, err.fct = "sse")
nn$weights
#[[1]]
#[[1]][[1]]
#           [,1]
#[1,]  -9.900398
#[2,]  -1.527258
#[3,] -13.201669
#[4,]  11.624309
#[5,]  17.896367

#[[1]][[2]]
#          [,1]      [,2]      [,3]
#[1,]  32.51005 -4.014045 -4.303671
#[2,] -65.72300  4.013259  4.304652

In the OP's code, the issue is that model.matrix creates column names with nonstandard names. It can be converted to standard names with make.unique

names(training) <- make.names(names(training))
n <- neuralnet(price ~ ., 
            data = training,
            hidden = 1,
            err.fct = "sse", 
      linear.output = FALSE)



n$weights
#[[1]]
#[[1]][[1]]
#              [,1]
# [1,]  -0.62625018
# [2,]   1.39124245
# [3,]  -2.59472834
# [4,]   0.27773897
# [5,]   7.15830865
# [6,]  -2.93583230
# ...

Upvotes: 2

Related Questions