r - 如何针对多个输入运行脚本并根据输入名称保存结果对象?

标签 r time-series

为了运行相同的命令集并保存每个时间序列的结果对象,我按以下方式编写了脚本:

# Specify time series to be used 

dat <- tsname

# Run a set of commands and fit models with different parameters 
dat.1 <- model1(dat)
dat.2 <- model2(dat)
dat.3 <- model3(dat)

# Save objects for further analysis 

tsname.1 <- dat.1
tsname.2 <- dat.2


save(tsname.1, tsname.2, tsname.3, file = paste0("tsname", ".rda")

这样,我们只需要更改开头和结尾的脚本,保存每个时间序列的脚本并单独运行它们或在主脚本中运行它们。

使用此方法的主要原因是因为我找不到重命名创建的对象的方法,并且一些搜索表明上述是唯一的方法。

现在,随着系列数量的增加,最好使用 for 循环、foreach、批处理脚本或 commandArgs() 来运行一个脚本并将所有时间系列指定为参数。 为了实现这一点,脚本必须找到一种方法来为这些对象分配系列本身的名称,以便稍后加载它们以进行进一步分析。

我们如何使这样的脚本工作或者有更好的方法吗?在这种情况下哪种循环方法有效?

MWE

set.seed(1)
tsdata <- ts(rnorm(250), start = c(1980,1), frequency = 12)
dat <- tsdata
dat.11 <- arima(dat, order = c(1, 1, 1))
dat.21 <- arima(dat, order = c(2, 1, 0))

tsname.11 <- dat.11  # problem is to specify this step in each script
tsname.21 <- dat.21
save(tsname.11, , file = "tsname.rda")

修改了代码

我们如何针对多个时间序列执行此脚本并存储结果和结果对象以供进一步分析?如果可以使用Batch命令,输入多个时间序列集的最佳方式是什么?

我们如何在一组相同或混合长度的时间序列上运行一个系列的脚本?

最佳答案

我展示了使用assignget创建和检索单个对象的几种方法,但也提供了一种替代方法,其中所有模型运​​行都存储为列表的不同元素。同样,我将展示如何将运行的每个模型保存在单独的文件(soi.1.rda 等)中,但您也可以一步将所有内容保存在一起:)

# ===========================================
# = Set up model params, generate test data =
# ===========================================
mod.param <- 1:5 # orders of AR to try ...
test.soi <- arima.sim(model=list(ar=c(0.5, -0.2)), n=20)

# ===========================================================
# = Create empty vectors/ list to store data and data names =
# ===========================================================
dat.names <- c() # a place to store the names of the individual objects that we'll create
save.names <- c() # the names of the files to save, e.g., "soi.1"
dat.all <- list() # as an altnerative, you can save each analysis in different elements of a list

# ===================================================
# = Loop through each type of model, saving results =
# ===================================================
for(i in 1:length(mod.param)){ # loop through each model you want to run
    temp.dat <- arima(test.soi, order=c(mod.param[i], 0, 0)) # temp.dat is the current model result
    dat.names[i] <- paste("dat", i, sep=".") # dat.names stores the names of all the dat.x objects

    assign(dat.names[i], temp.dat) # use assign() to create an object with name of temp.dat.name

    # dat.all[[dat.names[i]]] <- temp.dat # store the object in a list
    dat.all[[dat.names[i]]] <- get(dat.names[i]) # same as above, but using get(), which complements assign() nicely

    save.name <- paste("soi", i, "rda", sep=".") # I'm assuming the file should be named soi.1.rda, not soi.rda
    save(list=dat.names[i], file=save.name) # save soi.1.rda, soi.2.rda ... etc.
}

# But we don't have to save each file individually!
# We can save a file that contains our list of models (dat.all), as well as each model object (dat.1, dat.2 ... etc.)
all.objs <- ls() # what are all of the object names in our working memory?
dat.objs <- all.objs[all.objs%in%c(dat.names, "dat.all")] # subset to the names of objects we want to save
save(list=dat.objs, file="everything.rda") # save all relevant objects in 1 .rda file


print(dat.1)
print(dat.all$dat.1)

编辑:将多个模型中的每个模型应用于多个时间序列的不同方法

请注意,该方法可能会略有不同,具体取决于您想要将哪些模型应用于哪个时间序列。我假设每个时间序列都应应用多个模型,并且这些模型仅 ARIMA 阶数不同。

结果可以保存为 1 个嵌套列表(不同的模型结果分组在不同的时间序列下),或者将每个时间序列的模型结果保存为单独的文件。

# ============================================================
# = Generate many time series, many sets of model parameters =
# ============================================================
# Model parameters
n.Params <- 5
ar.orders <- 1:n.Params # orders of AR to try ...
i.orders <- rep(0, n.Params)
ma.orders <- rep(0,n.Params)
arima.params <- as.list(as.data.frame(rbind(ar.orders, i.orders, ma.orders)))

# Time Series Data
n.ts <- 10 # number of time series
test.soi <- quote(as.numeric(arima.sim(model=list(ar=c(0.2, 0.4)), n=sample(20:30, 1))))
all.soi.ts <- replicate(n.ts, eval(test.soi))
names(all.soi.ts) <- paste("soi", 1:n.ts, sep=".")

# ==============================================
# = Function to be applied to each time series =
# ==============================================
# Analyze time series
ats <- function(TS, arimaParams){
    dat.all <- list() # as an altnerative, you can save each analysis in different elements of a list
    for(i in 1:length(arimaParams)){ # loop through each model you want to run
        temp.dat <- arima(TS, order=arimaParams[[i]]) # temp.dat is the current model result
        dat.all[[i]] <- temp.dat # store the object in a list
    }
    dat.all 
}

# =========================
# = All Results in 1 List =
# =========================
AllResults <- lapply(all.soi.ts, ats, arima.params) # multilevel list – top level is each TS, within each TS group are the results of all models applied to that time series
save(AllResults, file="everything.rda") # save this big list as 1 file

# ========================================================================
# = Each time series gets its own file and its own list of model results =
# ========================================================================
for(i in 1:length(all.soi.ts)){ # if you want many files, 1 file per time series, use this for loop
    temp.ts <- all.soi.ts[[i]]
    soi.name <- paste("soi", i, sep=".")
    assign(soi.name, ats(temp.ts, arima.params))
    save(list=soi.name, file=paste(soi.name, "rda", sep=".")) # each file will have a name like "soi.1.rda", containing the results of all models applied to the first time series
}

关于r - 如何针对多个输入运行脚本并根据输入名称保存结果对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569666/

相关文章:

R通过添加行创建一个data.frame,每次一个

Cassandra:大量传感器数据澄清

algorithm - 实时时间序列数据中的峰值信号检测

r - 使用 data.table 滞后面板数据

r - 如何折叠键 id 上的行,但在新变量中保留唯一的测量值?

javascript - shinyBS 中的动态弹出窗口或工具提示

r - 如何一次执行多个 RSQLite 语句或如何转储整个文件?

从数据创建 R 向量

python - 如何绘制时间序列的倒数

r - "Spread"na.locf数据为不规则时间数据