如何解决使我的两个不同的R函数成为一个函数
我想在MonteCarlo
的{{1}}包中使用MonteCarlo
函数,除了向R
包中供应just one single function
之外,还有其他要求。
要运行模拟研究,用户必须将一个样本(生成样本和计算该样本所需的统计数据)两者嵌套在一个函数中。此函数传递给MonteCarlo()。不需要其他编程(Vignette: The MonteCarlo Package)。
与这种重要条件相反,我有两个适合我算法的不同功能。我已使用this question中正确答案提供的MonteCarlo
函数作为方法。
我想使用一种不同的方法,因此,我编写了以下函数(function1和function2),以便将其传递给MonteCarlo
函数,如下所示:
这是我想对MonteCarlo
执行的算法:
- 通过
R
函数从ARIMA
模型中模拟10个时间序列数据集 - 将系列划分为可能的
arima.sim()
,2s
,3s
,4s
,5s
,重叠子系列,6s
,7s
和8s
。 - 对于每种大小,对块进行重新采样并进行替换,以得到新的序列,并通过
9s
函数从每个块大小的子序列中获得最佳的ARIMA
模型。 - 获取每个块大小
auto.arima()
的每个子系列。
下面的RMSE
函数可以完成此任务。
R
我在执行上述操作时收到此错误:
在降雪中:: sfExport(“ func2”,“ func”,“ libloc_strings”,“ function1”,: 未知/未找到的变量以导出结束。 (local = TRUE)
我想将library(MonteCarlo)
library(forecast)
library(Metrics)
############################################
function1 <- function(lb,ov,n) {
starts <- unique(sort(c(seq(1,n,lb),seq(lb-ov+1,lb))))
ends <- pmin(starts + lb - 1,n)
# truncate starts and ends to the first num elements
num <- match(n,ends)
head(data.frame(starts,ends),num)
}
#############################################
# 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
function2 <- function(n,lb,phi) {
#### simulate ####
ov <- ceiling(lb/2)
vblocks <- Vectorize(function1,c("lb","ov"),SIMPLIFY = FALSE)
d <- vblocks(lb = lb,ov = ov,n = n)
ts <- arima.sim(n,model = list(ar = phi,order = c(1,0)),sd = 1)
#### devide ####
blk <- lapply(d,function(x) with(x,Map(function(i,j) ts[i:j],starts,ends)))
#### 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)
set.seed(123,kind = "L'Ecuyer-CMRG")
MC_result <- MonteCarlo(func = bootstrap4,nrep = reps,ncpus = parallel::detectCores() - 1,param_list = param_list,export_also = list(
"packages" = c("forecast","Metrics")
),raw = T)
集成到function1
中,以使function2
不会成为function2中的函数。
这是我的审判书
function1
当我将其传递给这个:
function2 <- function(n,phi) {
#### simulate ####
ov <- ceiling(lb/2)
function1 <- head(data.frame(unique(sort(c(seq(1,lb)))),pmin(unique(sort(c(seq(1,lb)))) + lb - 1,n)),match(n,n)))
vblocks <- Vectorize(function1,sd = 1)
#### devide ####
blk <- lapply(d,unique(sort(c(seq(1,n))))
#### resample ####
res <- sample(blk,nfuture) # return RMSE
return(
list("RMSE" = RMSE)
)
}
我收到此错误消息:
3个节点产生错误;第一个错误:找不到函数“ vblocks”
我在审判中所做的只是将整个set.seed(123,kind = "L'Ecuyer-CMRG")
MC_result <- MonteCarlo(func = function2,raw = T)
作为单个语句放在function1
解决方法
您可以将function1
的内容放入function2
的正文中-包括变量分配等。
library(MonteCarlo)
library(forecast)
library(ModelMetrics)
mc_f <- function(n,lb,phi) {
# Generate data
ov <- ceiling(lb / 2)
starts <- unique(sort(c(seq(1,n,lb),seq(lb - ov + 1,lb))))
ends <- pmin(starts + lb - 1,n)
num <- match(n,ends)
d <- head(data.frame(starts,ends),num)
ts <- arima.sim(n,model = list(ar = phi,order = c(1,0)),sd = 1)
blk <- mapply(
function(start,end) ts[start:end],d$starts,d$ends,SIMPLIFY = FALSE
)
# Resample
res <- sample(blk,replace = TRUE,10)
res.unlist <- unlist(res,use.names = FALSE)
# Train and forecast
train <- head(res.unlist,round(length(res.unlist) - 10))
test <- tail(res.unlist,length(res.unlist) - length(train))
nfuture <- forecast(train,model = auto.arima(train),lambda = 0,biasadj = TRUE,h = length(test))$mean
# Extract metric
RMSE <- rmse(test,nfuture)
list("RMSE" = RMSE)
}
reps <- 3
param_list <- list(n = 10,lb = seq(n - 2) + 1,phi = 0.6)
mc_result <- MonteCarlo(
func = mc_f,nrep = reps,ncpus = parallel::detectCores() - 1,param_list = param_list
)
#> Grid of 8 parameter constellations to be evaluated.
#>
#> Simulation parallelized using 3 cpus.
#>
#> Progress:
#>
#> |==================================================================================| 100%
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。