如何在Windows上的R中将蒙特卡洛用于ARIMA仿真功能

如何解决如何在Windows上的R中将蒙特卡洛用于ARIMA仿真功能

这是我想对R执行的算法:

  1. 通过ARIMA函数从arima.sim()模型中模拟10个时间序列数据集
  2. 将系列划分为可能的2s3s4s5s6s7s,{{ 1}}和8s
  3. 对于每种大小,对块进行重新采样并进行替换,以得到新的序列,并通过9s函数从每个块大小的子序列中获得最佳的ARIMA模型。
  4. 获取每个块大小auto.arima()的每个子系列。

下面的RMSE函数可以完成此任务。

R

调用函数

## Load packages and prepare multicore process
library(forecast)
library(future.apply)
plan(multisession)
library(parallel)
library(foreach)
library(doParallel)
n_cores <- detectCores()
cl <- makeCluster(n_cores)
registerDoParallel(cores = detectCores())
## simulate ARIMA(1,0)
#n=10; phi <- 0.6; order <- c(1,0)
bootstrap1 <- function(n,phi){
  ts <- arima.sim(n,model = list(ar=phi,order = c(1,0)),sd = 1)
  ########################################################
  ## create a vector of block sizes
  t <- length(ts)    # the length of the time series
  lb <- seq(n-2)+1   # vector of block sizes to be 1 < l < n (i.e to be between 1 and n exclusively)
  ########################################################
  ## This section create matrix to store block means
  BOOTSTRAP <- matrix(nrow = 1,ncol = length(lb))
  colnames(BOOTSTRAP) <-lb
  ########################################################
  ## This section use foreach function to do detail in the brace
  BOOTSTRAP <- foreach(b = 1:length(lb),.combine = 'cbind') %do%{
    l <- lb[b]# block size at each instance 
    m <- ceiling(t / l)                                 # number of blocks
    blk <- split(ts,rep(1:m,each=l,length.out = t))  # divides the series into blocks
    ######################################################
    res<-sample(blk,replace=T,10)        # resamples the blocks
    res.unlist <- unlist(res,use.names = FALSE)   # unlist the bootstrap series
    train <- head(res.unlist,round(length(res.unlist) - 10)) # Train set
    test <- tail(res.unlist,length(res.unlist) - length(train)) # Test set
    nfuture <- forecast::forecast(train,model = forecast::auto.arima(train),lambda=0,biasadj=TRUE,h = length(test))$mean        # makes the `forecast of test set
    RMSE <- Metrics::rmse(test,nfuture)      # RETURN RMSE
    BOOTSTRAP[b] <- RMSE
  }
  BOOTSTRAPS <- matrix(BOOTSTRAP,nrow = 1,ncol = length(lb))
  colnames(BOOTSTRAPS) <- lb
  BOOTSTRAPS
  return(list(BOOTSTRAPS))
}

我得到以下结果:

bootstrap1(10,0.6)

我想按时间顺序重复上述## 2 3 4 5 6 7 8 9 ## [1,] 0.8920703 0.703974 0.6990448 0.714255 1.308236 0.809914 0.5315476 0.8175382 step 1,然后想到step 4中的Monte Carlo技术。因此,我加载了它的包并运行以下功能:

R

期望以param_list=list("n"=10,"phi"=0.6) library(MonteCarlo) MC_result<-MonteCarlo(func = bootstrap1,nrep=3,param_list = param_list) 形式获得以下类似结果:

matrix

但是我收到以下错误消息:

MonteCarlo中的错误(func = bootstrap1,nrep = 3,param_list = param_list): func必须返回包含命名组件的列表。每个分量都必须是标量的。

我如何找到获得上述期望结果并使结果可再现的方式?

编辑

我希望预期的## [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] ## [1,] 0.8920703 0.703974 0.6990448 0.714255 1.308236 0.809914 0.5315476 0.8175382 ## [2,] 0.8909836 0.8457537 1.095148 0.8918468 0.8913282 0.7894167 0.8911484 0.8694729 ## [3,] 1.586785 1.224003 1.375026 1.292847 1.437359 1.418744 1.550254 1.30784 将在 Windows

上运行

解决方法

您收到此错误消息是因为MonteCarlo希望bootstrap1()接受模拟的一个参数组合,并且它仅返回一个值({{1} })。这里不是这种情况,因为块长度(RMSE)是由模拟时间序列(lb)的长度确定的( in n,所以您将为每个调用获取bootstrap1个块长度的结果。

一种解决方案是将块长度作为参数传递,并适当地重写n - 2

bootstrap1()

要运行模拟,请将参数以及library(MonteCarlo) library(forecast) library(Metrics) # parameter grids n <- 10 # length of time series lb <- seq(n-2) + 1 # vector of block sizes phi <- 0.6 # autoregressive parameter reps <- 3 # monte carlo replications # simulation function bootstrap1 <- function(n,lb,phi) { #### simulate #### ts <- arima.sim(n,model = list(ar = phi,order = c(1,0)),sd = 1) #### devide #### m <- ceiling(n / lb) # number of blocks blk <- split(ts,rep(1:m,each = lb,length.out = n)) # divide into blocks #### resample #### res <- sample(blk,replace = TRUE,10) # resamples the blocks res.unlist <- unlist(res,use.names = FALSE) # unlist the bootstrap series #### train,forecast #### train <- head(res.unlist,round(length(res.unlist) - 10)) # train set test <- tail(res.unlist,length(res.unlist) - length(train)) # test set nfuture <- forecast(train,# forecast model = auto.arima(train),lambda = 0,biasadj = TRUE,h = length(test))$mean ### metric #### RMSE <- rmse(test,nfuture) # return RMSE return( list("RMSE" = RMSE) ) } param_list = list("n" = n,"lb" = lb,"phi" = phi) 传递到bootstrap1()。为了并行进行仿真,您需要通过MonteCarlo()设置内核数。 MonteCarlo软件包使用snowFall,因此应在Windows上运行。

请注意,我还设置了ncpus(否则结果将是所有复制的平均值)。事先设置种子将使结果可重复。

raw = T

结果是一个数组。我认为最好通过set.seed(123) MC_result <- MonteCarlo(func = bootstrap1,nrep = reps,ncpus = parallel::detectCores() - 1,param_list = param_list,export_also = list( "packages" = c("forecast","Metrics") ),raw = T) 将其转换为data.frame:

MakeFrame()

尽管很容易获得Frame <- MakeFrame(MC_result) 矩阵:

reps x lb

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res