0
votes

i am trying to the neural network method on my data and i am stuck. i am allways getting the message:

in neurons[[i]] %*% weights[[i]] : requires numeric/complex matrix/vector arguments

the facts are:

  1. i am reading my data using read.csv i am adding a link to a file with some of my data, i hope it helps https://www.dropbox.com/s/b1btx0cnhmj229p/collineardata0.4%287.2.2017%29.csv?dl=0
  2. i have no NA in my data (i checked twice)
  3. the outcome of str(data) is:

    'data.frame': 20 obs. of 457 variables: $ X300.5_alinine.sulphate : num 0.351 0.542 0.902 0.656 1 ... $ X300.5_bromocresol.green : num 0.435 0.603 0.749 0.314 0.922 ... $ X300.5_bromophenol.blue : num 0.415 0.662 0.863 0.345 0.784 ... $ X300.5_bromothymol.blue : num 0.2365 0.0343 0.4106 0.3867 0.8037 ... $ X300.5_chlorophenol.red : num 0.465 0.1998 0.7786 0.0699 1 ... $ X300.5_cresol.red : num 0.534 0.311 0.678 0.213 0.821 ... continued

  4. i have tried to do use model.matrix

  5. the code i have was tried on different datasets (i.e iris) and it was good.

can anyone please try and suggest what is wrong with my data/data reading?

the code is

require(neuralnet)
require(MASS) 
require(grid)

require(nnet)

#READ IN DATA
data<-read.table("data.csv", sep=",", dec=".", head=TRUE)
dim(data)

# Create Vector of Column Max and Min Values
maxs <- apply(data[,3:459], 2, max)
mins <- apply(data[,3:459], 2, min)

# Use scale() and convert the resulting matrix to a data frame
scaled.data <- as.data.frame(scale(data[,3:459],center = mins, scale =   maxs - mins))

# Check out results
print(head(scaled.data,2))

#create formula
feats <- names(scaled.data)

# Concatenate strings
f <- paste(feats,collapse=' + ')
f <- paste('data$Type ~',f)

# Convert to formula
f <- as.formula(f)

f

#creating neural net
nn <- neuralnet(f,model,hidden=c(21,15),linear.output=FALSE)
str(scaled.data)

apply(scaled.data,2,function(x) sum(is.na(x)))
1
Without a small reproducible subset of your data it is difficult to say. Can you try a smaller subset of variables that reproduces your problem that you can post some here? - cdeterman

1 Answers

0
votes

There are multiple things wrong with your code.

1.There are multiple factors in your dependent variable Type. The neuralnet only accepts numeric input so you must convert it to a binary matrix with model.matrix.

y <- model.matrix(~ Type + 0, data = data[,1,drop=FALSE])

# fix up names for as.formula
y_feats <- gsub(" |\\+", "", colnames(y))
colnames(y) <- y_feats

scaled.data <- cbind(y, scaled.data)

# Concatenate strings
f <- paste(feats,collapse=' + ')
y_f <- paste(y_feats,collapse=' + ')
f <- paste(y_f, '~',f)

# Convert to formula
f <- as.formula(f)

2.You didn't even pass in your scaled.data to the neuralnet call anyway.

nn <- neuralnet(f,scaled.data,hidden=c(21,15),linear.output=FALSE)

The function will run now but you will need to look in to the multiclass problems (beyond the scope of this question). This package does not output straight probabilities so you must be cautious.