1

これは長い投稿のように見えるかもしれませんが、動物園でのループに関連する単純な問題です。読んでください:

# 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 

次のように、適切にインデックス付けされた Zoo オブジェクトに配置します。

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

また、zoo オブジェクトを作成する 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

助けてください。私はこれに2日間費やしました!どうもありがとう

4

2 に答える 2

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乱数を使用した例を投稿する際の再現性のために 使用してください。

于 2015-03-03T14:10:35.947 に答える
2

パッケージの VAR がわかりません。
coef(VAR(model[1:5]))$x[,1]
#This is my sessionInfo()を実行すると NULL が返されます

# 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
于 2015-03-03T13:49:06.853 に答える