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 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.
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.
@technocrat, you are God sent!
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.