1

DNN was performed using the neuralnet function. However, if I enter medv ~., In the formula parameter in the neuralnet function as below, the following error appears.

library("MASS")
data("Boston", package="MASS")
data<-Boston
keeps<-c("crim","indus","nox","rm","age","dis","tax","ptratio","lstat","medv")
data<-data[,keeps]

set.seed(2016)
train<-sample(1:nrow(data),400,FALSE)

fit<-neuralnet(medv~.,data=data[train,],hidden=c(10,12,20),algorithm="rprop+",err.fct="sse",
               act.fct="logistic",threshold=0.1,linear.output = TRUE)
Error in terms.formula(formula) : 
  formula 안에  '.'가 사용되었는데 'data' 인자가 없습니다

However, there is no problem if I input the following explanatory variables.

fit<-neuralnet(medv~crim+indus+nox+rm+age+dis+tax+ptratio+lstat, data=data[train,],hidden=c(10,12,20),algorithm="rprop+",err.fct="sse",
               act.fct="logistic",threshold=0.1,linear.output = TRUE)

If there are a lot of explanatory variables, it is impossible to write them one by one. what's the problem? and what can i do to consider all variables as short code like medv~. .

이순우
  • 79
  • 1
  • 1
  • 10

1 Answers1

1

you can try this.

f = as.formula(paste('medv~', paste(names(Boston.scaled)[!names(Boston.scaled) %in% c('medv')], collapse='+')))

it work for me. and here is my example.

Boston.scaled <- as.data.frame(scale(Boston))
min.medv <- min(Boston$medv)

max.medv <- max(Boston$medv)
Boston.scaled$medv <- scale(Boston$medv
                            , center = min.medv
                            , scale = max.medv - min.medv)
f = as.formula(paste('medv~', paste(names(Boston.scaled)[!names(Boston.scaled) %in% c('medv')], collapse='+')))
Boston.nn.5.3 <- neuralnet(f
                           , data=Boston.scaled
                           , hidden=c(5,3), rep=3
                           , linear.output=TRUE)

error.v = which.min(Boston.nn.5.3$result.matrix[1, ])
nn.5.3.predY = compute(Boston.nn.5.3, Boston.scaled[, -14], rep=error.v)$net.result
nn.5.3.predY.scaled = nn.5.3.predY*(max.medv - min.medv) + min.medv
nn.5.3.MSE = mean((nn.5.3.predY.scaled-Boston$medv)^2)
plot(Boston$medv,nn.5.3.predY.scaled,col='red',main='Real vs predicted NN with linear.output',pch=18,cex=0.7)
abline(0,1,lwd=2)
legend('bottomright',legend=c('with'),pch=18,col='red', bty='n')

nn, neuralnet, neuralnetwork

Teddy
  • 11
  • 1