[英]rmse function issue in R
I have an R
code that contains some nested bracket for loop within which I used rmse()
function from Metrics
package.我有一个R
代码,其中包含一些用于循环的嵌套括号,我在其中使用了来自Metrics
package 的rmse()
function。 I tried it without the function and it worked, but inside my nested R
code it does not.我在没有 function 的情况下尝试了它并且它有效,但在我的嵌套R
代码中它没有。
Here is what I desire to do with R
这是我想用R
做的事情
2,3,...,48,49
making me have 48 different time series formed from step 1 above.我将相同的时间序列数据集分成以下大小的块: 2,3,...,48,49
使我从上面的步骤 1 中形成了 48 个不同的时间序列。train
and test
sets so I can use rmse
function in Metrics
package to get the Root Mean Squared Error (RMSE) for the 48 subseries formed in step 2.我将每个 48 个时间序列数据集划分为train
集和test
集,因此我可以使用Metrics
rmse
中的 rmse function 来获得在步骤 2 中形成的 48 个子序列的均方根误差 (RMSE)。ARIMA
model for each 48 different time series data set.我为每个 48 个不同的时间序列数据集获得了最好的ARIMA
model。My R code我的 R 代码
# simulate arima(1,0,0)
library(forecast)
library(Metrics)
n <- 50
phi <- 0.5
set.seed(1)
wn <- rnorm(n, mean=0, sd=1)
ar1 <- sqrt((wn[1])^2/(1-phi^2))
for(i in 2:n){
ar1[i] <- ar1[i - 1] * phi + wn[i]
}
ts <- ar1
t<-length(ts)# the length of the time series
li <- seq(n-2)+1 # vector of block sizes(i.e to be between 1 and n exclusively)
RMSEblk<-matrix(nrow = 1, ncol = length(li))#vector to store block means
colnames(RMSEblk)<-li
for (b in 1:length(li)){
l<- li[b]# block size
m <- ceiling(t / l) # number of blocks
blk<-split(ts, rep(1:m, each=l, length.out = t)) # divides the series into blocks
singleblock <- vector() #initialize vector to receive result from for loop
for(i in 1:10){
res<-sample(blk, replace=T, 100) # resamples the blocks
res.unlist<-unlist(res, use.names = F) # unlist the bootstrap series
# Split the series into train and test set
train <- head(res.unlist, round(length(res.unlist) * 0.6))
h <- length(res.unlist) - length(train)
test <- tail(res.unlist, h)
# Forecast for train set
model <- auto.arima(train)
future <- forecast(test, model=model,h=h)
nfuture <- as.numeric(out$mean) # makes the `future` object a vector
# use the `rmse` function from `Metrics` package
RMSE <- rmse(test, nn)
singleblock[i] <- RMSE # Assign RMSE value to final result vector element i
}
#singleblock
RMSEblk[b]<-mean(singleblock) #store into matrix
}
RMSEblk
The error I got我得到的错误
#Error in rmse(test, nn): unused argument (nn)
#Traceback:
But when I wrote但是当我写
library(forecast)
train <- head(ar1, round(length(ar1) * 0.6))
h <- length(ar1) - length(train)
test <- tail(ar1, h)
model <- auto.arima(train)
#forecast <- predict(model, h)
out <- forecast(test, model=model,h=h)
nn <- as.numeric(out$mean)
rmse(test, nn)
It did work它确实有效
Please point out what I am missing?请指出我错过了什么?
I am able to run your code after making two very small corrections in your for loop.在您的 for 循环中进行两次非常小的更正后,我能够运行您的代码。 See the two commented lines:请参阅两条注释行:
for (b in 1:length(li)){
l<- li[b]
m <- ceiling(t / l)
blk<-split(ts, rep(1:m, each=l, length.out = t))
singleblock <- vector()
for(i in 1:10){
res<-sample(blk, replace=T, 100)
res.unlist<-unlist(res, use.names = F)
train <- head(res.unlist, round(length(res.unlist) * 0.6))
h <- length(res.unlist) - length(train)
test <- tail(res.unlist, h)
model <- auto.arima(train)
future <- forecast(test, model=model,h=h)
nfuture <- as.numeric(future$mean) # EDITED: `future` instead of `out`
RMSE <- rmse(test, nfuture) # EDITED: `nfuture` instead of `nn`
singleblock[i] <- RMSEi
}
RMSEblk[b]<-mean(singleblock)
}
It is possible that these typos did not result in errors because nn
and out
were defined in the global environment while you ran the for loop.这些拼写错误可能不会导致错误,因为在您运行 for 循环时, nn
和out
是在全局环境中定义的。 A good debugging tip is to restart R and try to reproduce the problem.一个好的调试技巧是重新启动 R 并尝试重现问题。
Your code does not define nn.您的代码没有定义 nn。 Other code that works has nn.其他有效的代码有 nn。 To start code with clean slate use this line as first executable line:要使用干净的 slate 开始代码,请将此行用作第一个可执行行:
rm(list=ls())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.