簡體   English   中英

R 中的 rmse function 問題

[英]rmse function issue in R

我有一個R代碼,其中包含一些用於循環的嵌套括號,我在其中使用了來自Metrics package 的rmse() function。 我在沒有 function 的情況下嘗試了它並且它有效,但在我的嵌套R代碼中它沒有。

這是我想用R做的事情

  1. 我已經生成了 50 個時間序列數據集。
  2. 我將相同的時間序列數據集分成以下大小的塊: 2,3,...,48,49使我從上面的步驟 1 中形成了 48 個不同的時間序列。
  3. 我將每個 48 個時間序列數據集划分為train集和test集,因此我可以使用Metrics rmse中的 rmse function 來獲得在步驟 2 中形成的 48 個子序列的均方根誤差 (RMSE)。
  4. 然后根據它們的塊大小將每個系列的 RMSE 制成表格
  5. 我為每個 48 個不同的時間序列數據集獲得了最好的ARIMA model。

我的 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

我得到的錯誤

#Error in rmse(test, nn): unused argument (nn)
#Traceback:

但是當我寫

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)

它確實有效

請指出我錯過了什么?

在您的 for 循環中進行兩次非常小的更正后,我能夠運行您的代碼。 請參閱兩條注釋行:

 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)
 }

這些拼寫錯誤可能不會導致錯誤,因為在您運行 for 循環時, nnout是在全局環境中定義的。 一個好的調試技巧是重新啟動 R 並嘗試重現問題。

您的代碼沒有定義 nn。 其他有效的代碼有 nn。 要使用干凈的 slate 開始代碼,請將此行用作第一個可執行行:

rm(list=ls())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM