2

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?

1 Answers1

1

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
# ...
akrun
  • 789,025
  • 32
  • 460
  • 575
  • I have already convert matrix to data frame bus still did not work. Is there any other way I can use? – Juli Avlokhashvili Apr 15 '20 at 21:53
  • @JuliAvlokhashvili when you convert the model.matrix to data.frame, please check the column names because if I am not mistaken, it adds a prefix – akrun Apr 15 '20 at 21:54
  • @JuliAvlokhashvili the column names would be changed i.e. `colnames(model.matrix(~ Species -1, iris))# [1] "Speciessetosa" "Speciesversicolor" "Speciesvirginica" ` – akrun Apr 15 '20 at 21:55