No one likes getting error messages when writing a program, but you will run into them sometimes. They can be a real problem in R when dealing with the functions of a new library. The “error in neurons[[i]] %*% weights[[i]] : requires numeric/complex matrix/vector arguments” error message is a good example. Avoiding it is a potential part of the learning curve for using the neuralnet library.
The circumstances of this error.
This is an error message that shows up when using the neuralnet() function. It shows up when one or more components in the data frame you are using are not numeric. If you are using a string in the data frame, it will produce this error.
> library(neuralnet)
> x = rep(1, 5)
> y = rep(2, 5)
> z = rep(3, 5)
> t = rep(4, 5)
> q = c(“a”,”b”,”c”,”d”, “e”)
> df = data.frame(x,y,z,t,q)
> net = neuralnet(t ~ x + y + z + q, df)
Error in neurons[[i]] %*% weights[[i]] :
requires numeric/complex matrix/vector arguments
In this example, the vector “q” is a vector of letters that get added to our data frame. When the data frame is used the neuralnet() function with “q” in the function, it produces an error.
What is causing this error?
What is causing this error is the fact that the neuralnet() function only takes numeric values. If one of the columns, you are using contains a non-numeric value that it produces our error message.
> library(neuralnet)
> x = rep(1, 5)
> y = rep(2, 5)
> z = rep(3, 5)
> t = rep(4, 5)
> df = data.frame(x,y,z,t)
> net = neuralnet(t ~ x + y + z, df)
In this example, the data frame contains only numeric values and does not produce an error message. This error is simply a case where the formula is looking for numeric values and you are giving it one or more non-numeric values.
How to fix this error.
The simplest way to fix this error is to not have any non-numeric values in your data frame. In such a case, you do not have to worry about getting it wrong. Unfortunately, this is not always an option.
> library(neuralnet)
> x = rep(1, 5)
> y = rep(2, 5)
> z = rep(3, 5)
> t = rep(4, 5)
> q = c(“a”,”b”,”c”,”d”, “e”)
> df = data.frame(x,y,z,t,q)
> net = neuralnet(t ~ x + y + z, df)
In this example, “q” which is the last column in our data frame, has letters rather than numbers. This is what caused the error message to be created in the first example. To fix this problem simply delete the ” + q” from the neuralnet() function.
net = neuralnet(t ~ x + y + z + q, df)
net = neuralnet(t ~ x + y + z, df)
Here is a simple comparison of the two lines. It shows the fix and just how simple it is to fix this error.