r语言 - 使用lag(x,1)或lag(x,-1)进行动态回归?



我有一个关于动态回归和滞后自变量的简单但有些令人困惑的问题。我有3个时间序列,我想研究前一周的3个自变量(即PSVI, NSVI和BTC_Ret)对当前周比特币日志回报的影响。例如,我想分析上周PSVI(积极情绪指数)的负变化是否可以告诉我们下周BTC回报的方向。

我遇到了延迟函数,它正好可以做到这一点。

如果我正确地理解了这个函数,我会将lag函数与dyn包中的dyn$lm函数结合使用,以获得我想要的结果。

我的代码看起来像这样:

test1 <- dyn$lm(BTC_Ret~lag(PSVI,1)+lag(NSVI,1)+lag(BTC_Ret,1))
summary(test1)

我是否可以假设我需要来使用lag(x,1)andnot lag(x,-1)

?我应该使用dyn$lm来研究效果,还是有更好的方法来做所有这些?

我的数据如下:

structure(c(0.151825062532955, -0.179352391776254, -0.171610266403897, 
0.0159227765884022, -0.353420091085592, -0.0179223189753976, 
0.260710954985742, -0.0878045204765083, 0.17494222283881, -0.183889954532262, 
-0.15249960475038, 0.0325479482522972, -0.216135243885031, 0.0258548317723122, 
0.170469815313808, 0.0552681180119521, 0.0676987678252168, 0.0247151614282206, 
-0.101373110320685, -0.0244444101458825, -0.363995910827583, 
-0.819549195465083, -0.311532754839479, -0.661660753934884, -0.036159476713393, 
-0.0116417252109642, -0.219357256430676, -0.386169350367107, 
-0.468384245564164, 0.226420789220966, -0.2366560332375, 0.2425676656972, 
-0.351430535471613, -0.287492079068963, 0.548071569094531, -0.228973857164721, 
-0.139490538928287, 0.247548840497568, -0.361502742177194, 0.0604938285432965, 
0.619445016304069, 0.0947076213861557, -0.887137767470338, 0.0485516007581502, 
0.0429273907756451, -0.701341407090506, 0.34191134646093, -0.428167056300805, 
-0.298917079322128, 0.517537828051947, 0.0474069010338689, -0.118044838446349, 
-0.414289228784203, 0.143198527419672, 0.0733053148180489, 0.0131259707878403, 
-0.106103445964187, 0.107827719520595, -0.604074345624302, 0.444400965939648
), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("BTC_Ret", "PSVI", 
"NSVI")), .Tsp = c(2018, 2018.36538461538, 52), class = c("mts", 
"ts", "matrix"))

多谢!

假设tt在最后的注释中定义(从问题中复制),我们使用如下:

ts类通常与R的延迟一起使用。其中的-1表示将序列向前移动1,以便之前的值与当前行对齐。?lag中有更多的信息

不要使用dplyr的延迟,它不能与ts类一起工作,而且是不同的,使用相反的约定,或者如果你想加载dplyr,使用library(dplyr, exclude = c("filter", "lag"))来确保你正在使用R的延迟。

library(dyn)
test1 <- dyn$lm(BTC_Ret ~ lag(PSVI,-1) + lag(NSVI,-1) + lag(BTC_Ret,-1), tt)

这些选项也可以使用:

Lag <- function(x, k = 1) lag(x, -k)
test2 <- dyn$lm(BTC_Ret ~ Lag(PSVI) + Lag(NSVI) + Lag(BTC_Ret), tt)
test3 <- dyn$lm(BTC_Ret ~ lag(tt, -1), tt)

注意

tt <- structure(c(0.151825062532955, -0.179352391776254, -0.171610266403897,  0.0159227765884022, -0.353420091085592, -0.0179223189753976,  0.260710954985742, -0.0878045204765083, 0.17494222283881, -0.183889954532262,  -0.15249960475038, 0.0325479482522972, -0.216135243885031, 0.0258548317723122,  0.170469815313808, 0.0552681180119521, 0.0676987678252168, 0.0247151614282206,  -0.101373110320685, -0.0244444101458825, -0.363995910827583,  -0.819549195465083, -0.311532754839479, -0.661660753934884, -0.036159476713393,  -0.0116417252109642, -0.219357256430676, -0.386169350367107,  -0.468384245564164, 0.226420789220966, -0.2366560332375, 0.2425676656972,  -0.351430535471613, -0.287492079068963, 0.548071569094531, -0.228973857164721,  -0.139490538928287, 0.247548840497568, -0.361502742177194, 0.0604938285432965,  0.619445016304069, 0.0947076213861557, -0.887137767470338, 0.0485516007581502,  0.0429273907756451, -0.701341407090506, 0.34191134646093, -0.428167056300805,  -0.298917079322128, 0.517537828051947, 0.0474069010338689, -0.118044838446349,  -0.414289228784203, 0.143198527419672, 0.0733053148180489, 0.0131259707878403,  -0.106103445964187, 0.107827719520595, -0.604074345624302, 0.444400965939648 ), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("BTC_Ret", "PSVI",  "NSVI")), .Tsp = c(2018, 2018.36538461538, 52), class = c("mts",  "ts", "matrix"))

相关内容

  • 没有找到相关文章