Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neural network using all input variables?

I am new to using neural networks in R and been trying out the algorithm with some wide data sets. Is there a way to include all the input variables into the network without having to type all the names? For example I have around 30 variables which I'd like to use as input to predict the output. Is there a shortcut for the following command?

net <- neuralnet(Output~Var1+Var2+Var3+Var4+.....upto Var30, data, hidden=0)
like image 439
Subha Yoganandan Avatar asked Dec 30 '25 08:12

Subha Yoganandan


1 Answers

There are 3 ways to insert variables in the formula part of a function:

First by using . which will include all of the variables in the data data.frame apart from the response variable (variable Output in this case):

net <- neuralnet(Output ~ ., data, hidden=0) #apart from Output all of the other variables in data are included

Use this if your data.frame has Output and another 30 variables only.

Second if you want to use a vector of names to include from the data data.frame you can try:

names <- c('var1','var2','var3') #choose the names you want
a <- as.formula(paste('Output ~ ' ,paste(names,collapse='+')))

> a
Output ~ var1 + var2 + var3 #this is what goes in the neuralnet function below

so you can use:

net <- neuralnet( a , data, hidden=0) #use a in the function

Use this if you can provide a vector of the names of the 30 variables

Third just subset the data data.frame using the columns you want in the function e.g.:

net <- neuralnet(Output ~ ., data=data[,1:31] , hidden=0)

Use this to (or any other subset that is convenient) and choose the 30 variables you need along with the Output variable. Then use . to include everything.

Hope it helps!

like image 57
LyzandeR Avatar answered Dec 31 '25 23:12

LyzandeR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!