【问题标题】:looping for a time series object循环时间序列对象
【发布时间】:2015-05-04 03:50:58
【问题描述】:

我知道这可能看起来很长,但这是一个与动物园循环相关的简单问题。请继续阅读:

# required libraries:

library(vars)
library(zoo)

# create time series
model <- zoo(x = cbind(rnorm(10),rnorm(10)*2, rnorm(10)*0.2), order.by = as.Date(1:10))

> model
1970-01-02 -0.32247034 -2.8667554 -0.08572468
1970-01-03 -1.33880074 -2.1103700 -0.13123590
1970-01-04  0.68815603 -1.4662238  0.19187887
1970-01-05  0.07128065  0.4218145  0.31121053
1970-01-06  2.18975236 -1.9978415 -0.20815929
1970-01-07 -1.15770760  2.1557006  0.18611448
1970-01-08  1.18168806 -2.3979488 -0.01508919
1970-01-09 -0.52736836  0.4332741 -0.39343907
1970-01-10 -1.45662801  0.2861741 -0.15118073
1970-01-11  0.57296737 -2.1315002  0.09222983

我想创建一个动物园时间序列对象,其中包含每个日期的 VAR 方程中的系数,仅使用当时可用的数据。我只想要变量“X”的系数。具体来说,我想循环这些函数:

> coef(VAR(model[1:5]))$x[,1]
       x.l1        y.l1        z.l1       const 
   6.366133   51.897180 -273.933190  -28.856147 

> coef(VAR(model[1:6]))$x[,1]
      x.l1       y.l1       z.l1      const 
-0.1726954  0.5972525 -1.9151799 -0.4963311 

> coef(VAR(model[1:7]))$x[,1]
      x.l1       y.l1       z.l1      const 
-0.3567360 -0.1969814  1.0163298 -0.1171288 

> coef(VAR(model[1:8]))$x[,1]
       x.l1        y.l1        z.l1       const 
-0.54919705 -0.09963062  0.47934378 -0.20755763 

> coef(VAR(model[1:9]))$x[,1]
      x.l1       y.l1       z.l1      const 
-0.4623637 -0.2161147  1.5129717 -0.3003821 

> coef(VAR(model[1:10]))$x[,1]
      x.l1       y.l1       z.l1      const 
-0.5041168 -0.2164998  1.5813547 -0.2684235 

并将它们放入适当索引的动物园对象中,例如:

                  x.l1       y.l2        z.l1       const
1970-01-02          NA         NA           NA         NA
1970-01-03          NA         NA           NA         NA
1970-01-04          NA         NA           NA         NA
1970-01-05          NA         NA           NA         NA
1970-01-06    6.366133    51.897180 -273.933190  -28.856147 
1970-01-07  -0.1726954    0.5972525  -1.9151799  -0.4963311 
1970-01-08  -0.3567360   -0.1969814   1.0163298  -0.1171288 
1970-01-09 -0.54919705  -0.09963062  0.47934378 -0.20755763 
1970-01-10  -0.4623637   -0.2161147   1.5129717  -0.3003821 
1970-01-11  -0.5041168   -0.2164998   1.5813547  -0.2684235 

我已经尝试过了,但它没有创建时间序列对象(仅是最终迭代的结果):

for (i in 2:(nrow(model)-3))
{
        b <- coef(VAR(model[1:(ncol(model)+i)]))$x[,1]
}

我也尝试了 rollapply,它创建了一个动物园对象,但数字看起来很奇怪:

rollapply(model, width = seq_along(model[,1])[5:10], 
                 function(x) coef(VAR(x))$x[,1],
                 by.column = FALSE, align = "right")

                 x.l1       y.l1       z.l1      const
1970-01-08  0.8259929 -0.4151388  1.1475067 -0.1905136
1970-01-09 -1.5161721  0.6774460 -7.0410817  1.0445796
1970-01-10 -0.1054592 -0.1699834  0.3326742 -0.2718832
1970-01-11 -0.2773018 -0.1760636  0.6023782 -0.1737401

请帮忙。我已经花了两天时间!非常感谢

【问题讨论】:

  • i&lt;-list(1:5,1:6,1:7,1:8,1:9,1:10); lapply(i,function(j)coef(VAR(model[j]))$x[,1]) ?
  • 不错,但它不包括时间索引。另外,如果可能的话,我需要一个更通用的解决方案(而不是键入 1:5、1:6 等,这取决于特定的数据系列)。

标签: r variables zoo rollapply


【解决方案1】:

我不知道包 VAR。
我运行时得到一个 NULL coef(VAR(模型[1:5]))$x[,1]
#这是我的 sessionInfo()

# R version 3.1.2 (2014-10-31)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# 
# other attached packages:
# [1] vars_1.5-2    zoo_1.7-11 

library(vars)
library(zoo)

# create time series
model <- zoo(x = cbind(rnorm(10),rnorm(10)*2, rnorm(10)*0.2), order.by = as.Date(1:10))

coef(VAR(model[1:10]))$x[,1]
# NULL

#This is what i use instead
coef(VAR(model[1:5]))[, 1]

# assign your starting point
starting.point <- 5

# run everything else without changing anything
coefs <- lapply(starting.point:dim(model)[1], function(x){
  coef(VAR(model[1:x]))[[1]][, 1]
})

coefs <- do.call(rbind, coefs)
coefs <- rbind(matrix(NA, starting.point-1, dim(model)[2]+1), coefs)

model2 <- zoo(x = coefs, order.by = as.Date(1:10))
model2

【讨论】:

  • 尽管它比下面的长,但它在样本数据上完成了工作!知道如何将它推广到任意数量的变量和日期吗?谢谢
  • model2 &lt;- zoo(x = coefs, order.by = index(model))替换model2 然后你每次运行时唯一需要定义的就是starting.point &lt;- 5
【解决方案2】:

尝试rollapplyr 与指定的Coef 函数。请注意,我们将列名添加到 model

library(vars)
library(zoo)

# input
set.seed(123)
n <- 10
model <- zoo(cbind(x = rnorm(n), rnorm(n)*2, rnorm(n)*0.2), order.by = as.Date(1:n))
colnames(model) <- c("x", "y", "z")

Coef <- function(m) coef(VAR(m))[[1]][, 1]
rollapplyr(model, pmax(ncol(model)+2, 1:nrow(model)), Coef, by.column = FALSE)

给予:

                  x.l1        y.l1      z.l1      const
1970-01-06  0.35854742  0.34666660 13.353457  1.9739107
1970-01-07 -0.09281548 -0.31745594  7.959080  2.0291699
1970-01-08  0.28089541 -0.05197486  5.983763  1.6170001
1970-01-09 -0.21759494 -0.42397646 -4.403415  0.3414522
1970-01-10 -0.01879181 -0.09968207 -3.504636 -0.1123601
1970-01-11  0.18226642 -0.14726950 -2.821511 -0.1400125

请参阅?rollapply 了解更多信息。

在发布使用随机数的示例时,请使用set.seed 以获得可重复性。

【讨论】:

  • 这是一个很好的解决方案,谢谢。知道如何将它推广到任意数量的变量和日期吗?
猜你喜欢
  • 2016-07-16
  • 2020-11-16
  • 2023-02-22
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多