R 不同长度的绘图函数
我尝试使用plot()绘图以显示实际的vs预测值,但我不断得到x和y长度不同的错误,但我不知道如何修复此错误。我对R很陌生,我不太明白如何纠正这一点。模型的长度显示为6,而currencyDelay测试[4]表示长度为4。但是打印currencyDelay_test[4],您会发现情况并非如此R 不同长度的绘图函数,r,plot,neural-network,R,Plot,Neural Network,我尝试使用plot()绘图以显示实际的vs预测值,但我不断得到x和y长度不同的错误,但我不知道如何修复此错误。我对R很陌生,我不太明白如何纠正这一点。模型的长度显示为6,而currencyDelay测试[4]表示长度为4。但是打印currencyDelay_test[4],您会发现情况并非如此 library(neuralnet) library(grid) library(MASS) library(ggplot2) library(reshape2) library(gridExtra) l
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')
库(neuralnet)
图书馆(网格)
图书馆(弥撒)
图书馆(GG2)
图书馆(E2)
图书馆(gridExtra)
图书馆(neuralnet)
规范化您的变量currencyDelay\u test
是一个矩阵。使用currencyDelay\u test[4]
您只能从对应于第一行和第四列的矩阵(当然是第四行)中获得一个元素。如果要选择第四列或第四行,请分别使用currencyDelay\u test[,4]
或currencyDelay\u test[4,]
。请注意,对于数据帧,即使使用currencyDelay\u test[4]
选择第四列,也会出现错误。但是,使用单括号时,您不会删除周围的数据帧,因此您会尝试将一列的数据帧与一个没有意义的向量进行对比。对于数据帧,您还必须编写currencyDelay\u test[,4]
,或者使用双括号删除周围的结构currencyDelay\u test[[4]
在您的问题的最小示例中是否真的需要上述所有代码?我想是的,因为神经网络预测需要正确地进行,才能在最后进行绘图。我可能错了。