0

How would one feed say, 100 different cases into the neuralnet() function found in the neuralnet package without maunally inputting them.

If there is a dataframe with colnames(df) = "one","two","three"..."one hundred"

and I want to use each column as an input into a neural network, is there a way to apply the neuralnet function as such:

nn <- neuralnet(one~two+three+four+five+six+seven+eight...+one hundred, data=df, 
      err.fct = 'sse', linear.output = F, likelihood=T)

without actually typing all one hundred colnames.



I have tried inputting:

nn <- neuralnet(one~as.factor(paste(names(df)[-1], collapse="+")) data=df, 
      err.fct = 'sse', linear.output = F, likelihood=T)

and recieved the error

Error in model.frame.default(formula.reverse, data) : 
  variable lengths differ (found for 'as.factor(paste(names(df)[-1], collapse="+"))')
Shai
  • 111,146
  • 38
  • 238
  • 371
Zach Eisner
  • 114
  • 9

1 Answers1

1

Might be a duplicate question. Edit: @Hong Ooi pointed out the "dot solution" doesnt work in neuralnet().

dta <- data.frame(a=rnorm(10), a2=rnorm(10), a3=rnorm(10))
frm <- as.formula(paste(names(dta)[1], " ~ ", paste(names(dta)[-1], collapse= "+")))
nn <- neuralnet(frm, data=dta, err.fct = 'sse', linear.output = F, likelihood=T)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • 1
    Thank you. Sorry If duplicate could not find anything of the likes. If you do please let me know and will delete – Zach Eisner Feb 11 '17 at 20:08
  • 1
    I just remember learning the dot here :D I dont mind about it. Does it work for you? – Tonio Liebrand Feb 11 '17 at 20:12
  • Receiving error message: `Error in terms.formula(formula) : '.' in formula and no 'data' argument` – Zach Eisner Feb 11 '17 at 20:12
  • Did you leave out the `data=df` specification? I made an edit and provided a simple example. – Tonio Liebrand Feb 11 '17 at 20:14
  • No its still in there. Strange error as it looks like its stopping at the period and not reading the following comma. – Zach Eisner Feb 11 '17 at 20:16
  • My actual application is: `neuralnet(CRISPR.Count.....assembled~. , data=Data.Fitted[,-c(1:7)], err.fct = 'sse', linear.output = F, likelihood=T)` – Zach Eisner Feb 11 '17 at 20:16
  • 1
    The neuralnet package is horrible in how it handles formulas. That's why the dot doesn't work. – Hong Ooi Feb 11 '17 at 20:22
  • The name is actually `CRISPR.Count * assembled`, as from the IMG genome database, but for some reason, Rstudio is recognizing it as `CRISPR.Count.....assembled`. Haha forgive me for my lack of knowledge I'm only a high schooler :D – Zach Eisner Feb 11 '17 at 20:23
  • any suggestions? @HongOoi – Zach Eisner Feb 11 '17 at 20:23
  • good point @Hong Ooi. I found a similar question: http://stackoverflow.com/questions/5774813/short-formula-call-for-many-variables-when-building-a-model. But i wouldnt mark it as a duplicate as it is worth knowing that the dot solution doesnt work with `neuralnet()`. I made an edit, with a working solution - assuming your dependent variable is at the first position of the array. You get adapt that to your data. – Tonio Liebrand Feb 11 '17 at 20:56
  • Thank You! It works perfectly. I was not aware of the `as.formula` function. @BigDataScientist – Zach Eisner Feb 12 '17 at 04:20