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?

  • can you please check whether `price` is one of the column names in 'm'.. Without a small reproducible example, it is difficult to know what happened – akrun Apr 15 '20 at 21:35
  • I have already checked and this is the exact name, with small p etc. I think there is something wrong with dot. – Juli Avlokhashvili Apr 15 '20 at 21:39
  • I typed it, I edited my post if it helps. – Juli Avlokhashvili Apr 15 '20 at 21:45
  • What is `data` in the post. WIthout that, we get error – akrun Apr 15 '20 at 21:46
  • at first the data name was "data", then when I use dummy variables it is called m, but for ann I need test and training data therefore, for neural net I use training data. – Juli Avlokhashvili Apr 15 '20 at 21:48
  • I can't reproduce with `data(iris);nn <- neuralnet(Species~ ., iris, linear.output = FALSE, hidden = 1, err.fct = "sse")` – akrun Apr 15 '20 at 21:48
  • okay, now got the idea, `m` is a `matrix` and not a data.frame. according to `?neuralnet` `data - a data frame containing the variables specified in formula.` – akrun Apr 15 '20 at 21:49
  • ~~~ library(neuralnet) set.seed(333) m<-as.data.frame(m) training<-as.data.frame(training) testing<-as.data.frame(testing) n <- neuralnet(price ~ ., data = training, hidden = 1, err.fct = "ce", linear.output = FALSE) ~~~ – Juli Avlokhashvili Apr 15 '20 at 21:55
  • Please check the `colnames(m)`, `colnames(training)` i think it got changed after the model.matrx – akrun Apr 15 '20 at 21:56
  • > colnames(training) [1] "(Intercept)" "price" [3] "month" "year" this is first two lines, it did not change. – Juli Avlokhashvili Apr 15 '20 at 22:00
  • it would be better if you create an example of 'data' so that I can test it. thanks – akrun Apr 15 '20 at 22:03
  • I can send you sample of the data if it is possible, because creating an example of data might be misleading in this case – Juli Avlokhashvili Apr 15 '20 at 22:25
  • can you pllease check my update – akrun Apr 15 '20 at 23:29

1 Answers1

2

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
  • 874,273
  • 37
  • 540
  • 662
  • 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