【发布时间】:2021-07-13 14:59:09
【问题描述】:
我正在尝试使用 plot() 进行绘图以显示实际与预测,但我不断收到 x 和 y 长度不同的错误,但我不知道如何解决此错误。我对 R 很陌生,我不太明白如何纠正这个问题。 models_predictions_unnorm 的长度显示为 6,而 currencyDelay_test[4] 显示长度为 4。但是打印 currencyDelay_test[4],您会发现情况并非如此。
library(neuralnet)
library(grid)
library(MASS)
library(ggplot2)
library(reshape2)
library(gridExtra)
library(neuralnet)
normalize <- function(x){
return ((x - min(x)) / (max(x) - min (x)))
}
data <-
structure(
list(
`USD/EUR` = c(
1.373,
1.386,
1.3768,
1.3718,
1.3774,
1.3672,
1.3872,
1.3932,
1.3911,
1.3838,
1.4171,
1.4164,
1.3947,
1.3675,
1.3801,
1.3744,
1.3759,
1.3743,
1.3787,
1.3595,
1.3599,
1.3624,
1.3523,
1.3506,
1.3521
)
),
row.names = c(NA,-25L),
class = c("tbl_df",
"tbl", "data.frame")
)
#Time delayed data
currencyDelay <- embed(data[[1]], 4)[, 4:1]
currencyNorm <- embed(normalize(data[[1]]), 4)[, 4:1]
currencyNorm
currencyNorm <- as.data.frame(currencyNorm)
train <- currencyNorm[1:16,]
test <- currencyNorm[17:22,]
set.seed(12345)
currency_model <- neuralnet(V4 ~ ., hidden = 12, data = train, act.fct = "logistic")
#plot(currency_model)
model_results <- compute(currency_model, test)
predicted_result <- model_results$net.result
#RMSE here still in 1x4 format
currencyDelay_train <- currencyDelay[1:16,]
currencyDelay_test <- currencyDelay[17:22,]
#Find min and max
cur_min <- min(currencyDelay_train[,4])
cur_max <- max(currencyDelay_train[,4])
currencyDelay_train[,4]
#reversed normalizatrion
unnormalize <- function(x, min, max){
return ((max-min)*x + min)
}
#Unormalize the predictions
models_predictions_unnorm <- unnormalize(predicted_result, cur_min, cur_max)
--- PLOT ---
par(mfrow=c(1,1))
plot(currencyDelay_test[4], models_predictions_unnorm,col='red',main='Real vs predicted NN',pch=18,cex=0.7)
abline(0,1,lwd=2)
legend('bottomright',legend='NN', pch=18,col='red', bty='n')
【问题讨论】:
-
在您的问题的最小示例中真的需要上面的所有代码吗?
-
我认为是这样,因为神经网络预测需要正确执行才能在最后绘制。我可能是错的。
标签: r plot neural-network