4

I try to run the following script in R (minimized example):

library(neuralnet)

arrhythmia <- read.csv(file=".../arrhythmia-edited.csv", head=TRUE, sep=",")

# create the first parameter for neuralnet formular 
# format like 'classification ~ attribute1 + attribute2 + ...
# i have so many features, that why i use a for
input <- ""
for (i in 1:259)
  input <- paste(input, paste(paste('arrhythmia[,',i),'] +'))
input <- paste(input, 'arrhythmia[,260]')

# create string for function call
nnet.func <- paste('neuralnet(arrhythmia[,261] ~', input)
nnet.func <- paste(nnet.func, ', data=arrhythmia)')

# call function neuralnet
# should be like: neuralnet(arrhythmia[,261] ~ arrhythmia[,1] + arrhythmia[,2] + ... + arrhytmia[,260], data=arrhythmia)
net.arrhythmia <- eval(parse(nnet.func))

The problem is that R is parsing this as following (using Linux):

'neuralnet(arrhythmia[,261] /home/user  arrhythmia[, 1 ] + ....

How can I prevent R from parsing ~ into /home/[user-directory]?

jonsca
  • 10,218
  • 26
  • 54
  • 62
stfn
  • 105
  • 1
  • 4

1 Answers1

8

Try using the ,text= argument. Right now you're using the ,file= argument to parse since that comes first:

> args('parse')
function (file = "", n = NULL, text = NULL, prompt = "?", srcfile = NULL, 
    encoding = "unknown") 

> parse("test ~ test")
Error in file(filename, "r") : cannot open the connection

> parse(text="test ~ test")
expression(test ~ test)
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • This worked, but now I ran into another problem: _Error in neurons[[i]] %*% weights[[i]] : requires numeric/complex matrix/vector arguments_ Will look for a solution for this on my own. Thanks! – stfn Oct 10 '12 at 12:46
  • Glad it worked. Some hints for your next problem: 1) `x %*% y` is equivalent to `'%*%'(x,y)` where the ' is actually a backtick. 2) Look at `class(neurons[[i]])` and `class(weights[[i]])` as well as their lengths to see if they're non-zero. – Ari B. Friedman Oct 10 '12 at 12:48
  • thanks. this error came when i used more than 10 features for training. i guess i'll use nnet insteat of neuralnet. – stfn Oct 10 '12 at 13:11
  • 1
    @AriB.Friedman `'%*%'(x, y)` also works. So does `"%*%"(x, y)`. But it's probably best to use `\`%*%\`(x, y)` as you mention – Dason Oct 10 '12 at 14:48
  • just a final comment: the latter error occurred because of missing values in my data set. – stfn Oct 10 '12 at 15:54