compute function of neuralnet package gives an error

Hi experts,

I am trying to build a financial model to predict stock price using neuralnet package in R. When executing following code, I am getting an error. It should be noted that in below code train_data is a data frame containing columns "return" and "adj_close". Same columns are available in the data frame test_data as well.

# Train a neural network using the training data
nn <- neuralnet(return ~ adj_close, train_data)

# Use the trained neural network to make predictions on the test data
predictions <- compute(nn, test_data[,"adj_close"])$net.result

The error generated is as follows:

Error in if (ncol(newdata) == length(object$model.list$variables)) { : 
  argument is of length zero

Please note that test_data as well as train_data both have values in them 402 and 102 rows respectively.

What could be going wrong here with the code. Really appreciate your help and advice to resolve the issue.

Thanks in advance.
TSK

How many columns?

Dear @technocrat,

Thanks for your response. Both the data frames contain exactly two columns "adj_close" and "return". Just so that you're aware, I have changed the column name from "return" to "vreturn" to ensure there is no conflict of keyword return. However, the error still persists.

Best Regards,
TSK

Could you provide the return for

str(nn)

And this is with {infer}, right?

@technocrat

Please find below:

> str(nn)
List of 14
 $ call               : language neuralnet(formula = vreturn ~ adj_close, data = train_data, hidden = 10, linear.output = FALSE)
 $ response           : num [1:402, 1] -0.00972 0.00797 -0.0047 0.01609 0.02124 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:402] "1" "2" "3" "4" ...
  .. ..$ : chr "vreturn"
 $ covariate          : num [1:402, 1] 0.143 0.148 0.145 0.154 0.167 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr ""
 $ model.list         :List of 2
  ..$ response : chr "vreturn"
  ..$ variables: chr "adj_close"
 $ err.fct            :function (x, y)  
  ..- attr(*, "type")= chr "sse"
 $ act.fct            :function (x)  
  ..- attr(*, "type")= chr "logistic"
 $ linear.output      : logi FALSE
 $ data               :'data.frame':	402 obs. of  2 variables:
  ..$ adj_close: num [1:402] 0.143 0.148 0.145 0.154 0.167 ...
  ..$ vreturn  : num [1:402] -0.00972 0.00797 -0.0047 0.01609 0.02124 ...
 $ exclude            : NULL
 $ net.result         :List of 1
  ..$ : num [1:402, 1] 0.00401 0.00399 0.004 0.00395 0.00389 ...
 $ weights            :List of 1
  ..$ :List of 2
  .. ..$ : num [1:2, 1:10] 1.323 -0.787 -0.402 0.48 0.785 ...
  .. ..$ : num [1:11, 1] -0.252 -0.71 -0.186 0.684 -1.362 ...
 $ generalized.weights:List of 1
  ..$ : num [1:402, 1] -1.25 -1.25 -1.25 -1.24 -1.23 ...
 $ startweights       :List of 1
  ..$ :List of 2
  .. ..$ : num [1:2, 1:10] 1.254 -0.855 0.598 1.48 1.985 ...
  .. ..$ : num [1:11, 1] 0.948 0.49 1.014 1.884 -0.162 ...
 $ result.matrix      : num [1:34, 1] 0.13019 0.00172 13 1.32281 -0.78701 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:34] "error" "reached.threshold" "steps" "Intercept.to.1layhid1" ...
  .. ..$ : NULL
 - attr(*, "class")= chr "nn"```

I think the problem may be here, because the last object has length one so

evaluates to FALSE, but I’ll need to find some suitable data for a reprex and look at the calling function source to be sure

Thanks for your feedback. Is there any way to send you direct message with full code? Please let me know, if there is.

You can just post a a reprex (see the FAQ). For the data, just enough to reproduce the error is needed and it doesn't need to be the actual data so long as its the same type and variable names. If the data is fake, obviously the results will be bogus, but for now we just need to trace the place where the train goes off the tracks.

Thanks a lot for your guidance @technocrat. Please find reprex below and let me know in case anything more is needed:

library(neuralnet)

date_column <- c("2020-01-03", "2020-01-06", "2020-01-07", "2020-01-08", "2020-01-09", "2020-01-10")
adj_close_column <- c(72.73531, 73.31489, 72.97009, 74.14391, 75.71879, 75.88996)
vreturn_column <- c(-0.009722082, 0.007968275, -0.004702933, 0.016086316, 0.021240786, 0.002260641)

tempstockdata <- data.frame(date = date_column, adj_close = adj_close_column,
                        vreturn = vreturn_column)

stockdata <- tempstockdata                              # Replicating data
rownames(stockdata) <- 1:nrow(tempstockdata)+1            # Applying rownames function
head(stockdata)

#>         date adj_close      vreturn
#> 2 2020-01-03  72.73531 -0.009722082
#> 3 2020-01-06  73.31489  0.007968275
#> 4 2020-01-07  72.97009 -0.004702933
#> 5 2020-01-08  74.14391  0.016086316
#> 6 2020-01-09  75.71879  0.021240786
#> 7 2020-01-10  75.88996  0.002260641

# Split the data into input and output variables
input_data <- stockdata[,"adj_close"]
output_data <- stockdata[,"vreturn"]

head(input_data)
#> [1] 72.73531 73.31489 72.97009 74.14391 75.71879 75.88996

head(output_data)
#> [1] -0.009722082  0.007968275 -0.004702933  0.016086316  0.021240786
#> [6]  0.002260641

# Perform min-max normalization on the input data
normalized_data <- (input_data - min(input_data)) / (max(input_data) - min(input_data))

head(normalized_data)
#> [1] 0.00000000 0.18372244 0.07442347 0.44651546 0.94574041 1.00000000

# Combine the normalized input data with the output data
data_norm <- cbind(normalized_data, output_data)

colnames(data_norm) = c("adj_close", "vreturn")

# Split the normalized data into training and test sets
train_size <- floor(0.8 * nrow(data_norm))
train_data <- data_norm[1:train_size,]
test_data <- data_norm[(train_size + 1):nrow(data_norm),]

# Train a neural network using the training data
nn <- neuralnet(vreturn ~ adj_close, data = train_data, hidden= 10, linear.output = FALSE)

# Use the trained neural network to make predictions on the test data
predictions <- compute(nn, test_data[,"adj_close"])$net.result

# Evaluate the performance of the neural network
mse <- mean((predictions - test_data[,"vreturn"])^2)
cat("Mean squared error:", mse)
1 Like

Good reprex, thanks. Mine below works in the sense of not throwing the error; but is it the return value you expected?

library(neuralnet)

date_column <- c("2020-01-03", "2020-01-06", "2020-01-07", "2020-01-08", "2020-01-09", "2020-01-10")
adj_close_column <- c(72.73531, 73.31489, 72.97009, 74.14391, 75.71879, 75.88996)
vreturn_column <- c(-0.009722082, 0.007968275, -0.004702933, 0.016086316, 0.021240786, 0.002260641)

tempstockdata <- data.frame(
  date = date_column, adj_close = adj_close_column,
  vreturn = vreturn_column
)

stockdata <- tempstockdata # Replicating data
rownames(stockdata) <- 1:nrow(tempstockdata) + 1 # Applying rownames function

# Split the data into input and output variables
input_data <- stockdata[, "adj_close"]
output_data <- stockdata[, "vreturn"]

# Perform min-max normalization on the input data
normalized_data <- (input_data - min(input_data)) / (max(input_data) - min(input_data))

# Combine the normalized input data with the output data
data_norm <- cbind(normalized_data, output_data)

colnames(data_norm) <- c("adj_close", "vreturn")

# Split the normalized data into training and test sets
train_size <- floor(0.8 * nrow(data_norm))
train_data <- data_norm[1:train_size, ]
test_data <- data_norm[(train_size + 1):nrow(data_norm), ]

# Train a neural network using the training data
nn <- neuralnet(vreturn ~ adj_close, data = train_data, hidden = 10, linear.output = FALSE)

# plot(nn)

# compute is deprecated in favor of predict
predict(nn, newdata = test_data)
#>            [,1]
#> [1,] 0.01992513
#> [2,] 0.01944926

Created on 2023-05-02 with reprex v2.0.2

1 Like

@technocrat, you are God sent! :slight_smile:
I figured after looking at your code that I was supposed to send full data frame instead of one column in compute function. However, if compute is deprecated, I would go with predict function.

Below code is also working now:

library(neuralnet)

date_column <- c("2020-01-03", "2020-01-06", "2020-01-07", "2020-01-08", "2020-01-09", "2020-01-10")
adj_close_column <- c(72.73531, 73.31489, 72.97009, 74.14391, 75.71879, 75.88996)
vreturn_column <- c(-0.009722082, 0.007968275, -0.004702933, 0.016086316, 0.021240786, 0.002260641)

tempstockdata <- data.frame(
  date = date_column, adj_close = adj_close_column,
  vreturn = vreturn_column
)

stockdata <- tempstockdata # Replicating data
rownames(stockdata) <- 1:nrow(tempstockdata) + 1 # Applying rownames function

# Split the data into input and output variables
input_data <- stockdata[, "adj_close"]
output_data <- stockdata[, "vreturn"]

# Perform min-max normalization on the input data
normalized_data <- (input_data - min(input_data)) / (max(input_data) - min(input_data))

# Combine the normalized input data with the output data
data_norm <- cbind(normalized_data, output_data)

colnames(data_norm) <- c("adj_close", "vreturn")

# Split the normalized data into training and test sets
train_size <- floor(0.8 * nrow(data_norm))
train_data <- data_norm[1:train_size, ]
test_data <- data_norm[(train_size + 1):nrow(data_norm), ]

# Train a neural network using the training data
nn <- neuralnet(vreturn ~ adj_close, data = train_data, hidden = 10, linear.output = FALSE)

# plot(nn)
prediction <- compute(nn, testdata, rep = 1)$net.result

Really appreciate your feedback and help. Thank you so much. :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.