Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于MLP神经网络的预测_R_Neural Network_Mlp - Fatal编程技术网

R 基于MLP神经网络的预测

R 基于MLP神经网络的预测,r,neural-network,mlp,R,Neural Network,Mlp,我正试图用R编写一个代码,用MLP神经网络预测美元/欧元的汇率我遇到了一个函数neuralnet的问题,它显示了一个错误: 神经元[[i]]%*%权重[[i]]中的错误: 需要数字/复数矩阵/向量参数 这是迄今为止我编写的代码 library(readxl) ExchangeUSD <- read_excel("C:/Users/GTS/Desktop/ML project/ExchangeUSD.xlsx") plot(ExchangeUSD$USD) #traning and tes

我正试图用R编写一个代码,用MLP神经网络预测美元/欧元的汇率我遇到了一个函数neuralnet的问题,它显示了一个错误:

神经元[[i]]%*%权重[[i]]中的错误:
需要数字/复数矩阵/向量参数

这是迄今为止我编写的代码

library(readxl)
ExchangeUSD <- read_excel("C:/Users/GTS/Desktop/ML project/ExchangeUSD.xlsx")
plot(ExchangeUSD$USD)

#traning and test data 
trainset <- ExchangeUSD[1:350,]
testset <- ExchangeUSD[351:500,]

set.seed(12345)
library(neuralnet)
nn <- neuralnet(USD ~ date + Wdy, data = trainset,hidden = 2)
库(readxl)

ExchangeUSD在上面的示例中,您试图使用date来训练模型,这是错误的,因为
neuralnet
只能理解
因子
数字
。如果您想在模型中包含一些时间序列因素,请使用R提供的
时间序列分析

此外,您试图仅使用一个或两个预测值来训练
神经网络,因此它将严重拟合过度,您的分析将有偏差

你的问题只包括三栏,即:<代码>日期
wdy
(实际上是星期一)和
美元
(价格)。在这种情况下,使用神经网络是没有意义的,因为您没有训练模型的功能。您的数据基本上是一个时间序列,因此请使用回归和其他线性算法。(也可以选择前面提到的
timeseries

尽管我在下面分享了如何训练一个好的
mlp
模型

下面是使用
RSNNS
包在R中使用多层感知器模型的简单示例。我使用了非常基本的
iris
数据集

代码如下:

library(RSNNS)
data(iris)


iris <- iris[sample(1:nrow(iris),length(1:nrow(iris))),1:ncol(iris)]

irisValues <- iris[,1:4]
irisTargets <- decodeClassLabels(iris[,5])


iris <- splitForTrainingAndTest(irisValues, irisTargets, ratio=0.15)
iris <- normTrainingAndTestSet(iris)

model <- mlp(iris$inputsTrain, iris$targetsTrain, size=5, learnFuncParams=c(0.1), 
             maxit=50, inputsTest=iris$inputsTest, targetsTest=iris$targetsTest)

summary(model)
#model
#weightMatrix(model)
#extractNetInfo(model)

par(mfrow=c(2,2))
#plotIterativeError(model)

#predictions <- predict(model,iris$inputsTest)
#plotRegressionError(predictions[,2], iris$targetsTest[,2])

confusionMatrix(iris$targetsTrain,fitted.values(model))
confusionMatrix(iris$targetsTest,predictions)
neuralnet
软件包的主要问题是,它只创建简单且非常基本的神经网络,而很少进行定制。与
neuralnet
相比,上述
RSNNS
软件包提供了更多的附加功能


但是如果你想尝试更深入的网络,我建议你使用
mxnet
,以及
keras
R扩展。

对于这个时间序列分析,你可以使用自回归模型。首先,您必须创建滞后输入集并创建一个数据帧。在下面的代码块中,有4个输入集包含一个滞后、两个滞后和三个滞后。(阅读更多有关自回归模型的信息-[https://otexts.com/fpp2/AR.html][1] )

exchangeEUR%
清洁工::清洁_名称()%>%
突变(日期=年月日(yyyy)月日))%>%
选择(-1)%>%
选择(日期,所有内容())
欧元兑换额=欧元%>%
突变(前一天设置=滞后(兑换欧元$usd欧元,1),
前一天设置=滞后(兑换美元欧元,1),
前两天设置=滞后(兑换美元欧元,2),
前一天设置=滞后(兑换欧元$usd\eur,1),
前两天设置=滞后(兑换欧元$usd\U eur,2),
前三天设置=滞后(兑换欧元$usd\U eur,3),
前一天设置=滞后(兑换美元欧元,1),
前两天设置=滞后(兑换美元欧元,2),
五天滚动=滚动平均值(美元欧元,5,填充=NA),
十天滚动=滚动平均值(美元欧元,10,填充=NA))%>%
滴_na()
规范化数据

# We can create a function to normalize the data from 0 to 1
normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x))) }
# All the variables are normalized
normalized_eur = eur_exchange_full %>%
  mutate(across(2:12, ~normalize(.x)))
# Look at the data that has been normalized
summary(normalized_eur)

boxplot(normalized_eur$usd_eur)

set.seed(123)
eur_train <- normalized_eur[1:400,]
eur_test <- normalized_eur[401:491,]

# We can create a function to unnormalize the data=
unnormalize <- function(x, min, max) {
  return( (max - min)*x + min ) }
# Get the min and max of the original training values
eur_min_train <- min(eur_exchange_full[1:400,2])
eur_max_train <- max(eur_exchange_full[1:400,2])
# Get the min and max of the original testing values
eur_min_test <- min(eur_exchange_full[401:491,2])
eur_max_test <- max(eur_exchange_full[401:491,2])
# Check the range of the min and max of the training dataset
eur_min_test

eur_min_train

eur_max_test
eur_max_train
#我们可以创建一个函数,将数据从0规范化为1
正常化%
变异(跨越(2:12,~normalize(.x)))
#查看已规范化的数据
汇总(欧元)
箱线图(标准化欧元$usd欧元)
种子集(123)

如果问题得到解决,请投票并接受答案
       predictions
targets  1  2  3
      1 40  0  0
      2  0 43  3
      3  0  1 40
       predictions
targets  1  2  3
      1 10  0  0
      2  0  4  0
      3  0  0  9
exchangeEUR <- read_excel("ExchangeUSD.xlsx") %>%
  janitor::clean_names() %>%
  mutate(date_in_ymd = ymd(yyyy_mm_dd)) %>%
  select(-1) %>%
  select(date_in_ymd,everything())

eur_exchange_full = exchangeEUR %>%
      mutate(previous_one_day_set_a = lag(exchangeEUR$usd_eur,1),
             previous_one_day_set_b = lag(exchangeEUR$usd_eur,1),
             previous_two_day_set_b = lag(exchangeEUR$usd_eur,2),
             previous_one_day_set_c = lag(exchangeEUR$usd_eur,1),
             previous_two_day_set_c = lag(exchangeEUR$usd_eur,2),
             previous_three_day_set_c = lag(exchangeEUR$usd_eur,3),
             previous_one_day_set_d = lag(exchangeEUR$usd_eur,1),
             previous_two_day_set_d = lag(exchangeEUR$usd_eur,2),
             five_day_rolling = rollmean(usd_eur,5, fill = NA),
             ten_day_rolling = rollmean(usd_eur,10, fill = NA)) %>%
      
   drop_na()
# We can create a function to normalize the data from 0 to 1
normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x))) }
# All the variables are normalized
normalized_eur = eur_exchange_full %>%
  mutate(across(2:12, ~normalize(.x)))
# Look at the data that has been normalized
summary(normalized_eur)

boxplot(normalized_eur$usd_eur)

set.seed(123)
eur_train <- normalized_eur[1:400,]
eur_test <- normalized_eur[401:491,]

# We can create a function to unnormalize the data=
unnormalize <- function(x, min, max) {
  return( (max - min)*x + min ) }
# Get the min and max of the original training values
eur_min_train <- min(eur_exchange_full[1:400,2])
eur_max_train <- max(eur_exchange_full[1:400,2])
# Get the min and max of the original testing values
eur_min_test <- min(eur_exchange_full[401:491,2])
eur_max_test <- max(eur_exchange_full[401:491,2])
# Check the range of the min and max of the training dataset
eur_min_test

eur_min_train

eur_max_test
eur_max_train
set.seed(12345)
# function setup that creates 2 layer model
model_two_hidden_layers = function(hidden,sec_hidden) {
  nn_model_true = neuralnet(usd_eur ~ previous_one_day_set_b+previous_two_day_set_b, data=eur_train, hidden=c(
    hidden,sec_hidden), linear.output=TRUE)
  
  #plot(nn_model_true)
  pred <- predict(nn_model_true, eur_test)
  
  validation_df <- data.frame(c(eur_test$date_in_ymd),c(pred),c(eur_test$usd_eur))
  

  p = ggplot() + 
    geom_line(data = validation_df, aes(x = c.eur_test.date_in_ymd., y = c.pred.), color = "blue") +
    geom_line(data = validation_df, aes(x = c.eur_test.date_in_ymd., y = c.eur_test.usd_eur.), color = "red") +
    xlab('Dates') +
    ylab('percent.change')
  print(p)
  
  train_results = compute(nn_model_true,eur_test[,2:3])
  truthcol = eur_exchange_full[401:491,2]$usd_eur
  predcol = unnormalize(train_results$net.result,eur_min_train, eur_max_train)[,1]
  relevant_pred_stat(truthcol,predcol,
                     "Two Hidden Layers") %>%
    mutate(hiddel_layers = paste0(hidden, " and ",sec_hidden),
           input_set = "B") %>%
    filter(.metric != "rsq")
}

model_two_hidden_layers(2,3)

# save the stat indices to a dataframe
set_a_models_two_layers = results_two_hidden_layers %>%
  select(-estimator) %>%
  pivot_wider(names_from = metric, values_from = estimate) %>%
  arrange(rmse)
kable(set_a_models_two_layers[1:10,])


##########################################################################
# three layer model
set.seed(12345)
# function setup that creates 3 layer model
model_three_hidden_layers = function(hidden,sec_hidden,third_hidden) {
  nn_model_true = neuralnet(usd_eur ~ previous_one_day_set_b+previous_two_day_set_b, data=eur_train, hidden=c(hidden,sec_hidden,third_hidden), linear.output=TRUE)
  
  #plot(nn_model_true)
  pred <- predict(nn_model_true, eur_test)
  
  validation_df <- data.frame(c(eur_test$date_in_ymd),c(pred),c(eur_test$usd_eur))
  
  
  ################
  p = ggplot() + 
    geom_line(data = validation_df, aes(x = c.eur_test.date_in_ymd., y = c.pred.), color = "blue") +
    geom_line(data = validation_df, aes(x = c.eur_test.date_in_ymd., y = c.eur_test.usd_eur.), color = "red") +
    xlab('Dates') +
    ylab('percent.change')
  print(p)
  ################
  
  train_results = compute(nn_model_true,eur_test[,2:3])
  truthcol = eur_exchange_full[401:491,2]$usd_eur
  predcol = unnormalize(train_results$net.result,eur_min_train, eur_max_train)[,1]
  relevant_pred_stat(truthcol,predcol,
                     "Three Hidden Layers") %>%
    mutate(hiddel_layers = paste0(hidden, " and ",sec_hidden," and ",third_hidden),
           input_set = "A") %>%
    filter(.metric != "rsq")
}


model_three_hidden_layers(7,4,1)