我正在尝试构建一个动态回归模型,到目前为止我是用 dynlm 包完成的。基本上模型看起来像这样
y_t = a*x1_t + b*x2_t + ... + c*y_(t-1).
Run Code Online (Sandbox Code Playgroud)
y_t应预测,x1_t并且x2_t将给予,所以是y_(t-1)。
使用 dynlm 包构建模型工作正常,但是当涉及到预测时,y_t我感到困惑......
我发现了这个,这似乎是一个非常相似的问题,但它并没有帮助我处理我自己的问题。
这是我面临的问题(基本上是什么predict(),似乎很奇怪。请参阅评论!):
library(dynlm)
# Create Data
set.seed(1)
y <- arima.sim(model = list(ar = c(.9)), n = 11) #Create AR(1) dependant variable
A <- rnorm(11) #Create independent variables
B <- rnorm(11)
y <- y + .5 * A + .2 * B #Add relationship to independent variables
data = cbind(y, A, B) …Run Code Online (Sandbox Code Playgroud) 依据如下数据表:
library(data.table)
dt <- data.table(Position = 1:3, Price = c(50, 45, 40), Volume = c(10, 10, 10))
dt
Position Price Volume
1: 1 50 10
2: 2 45 10
3: 3 40 10
Run Code Online (Sandbox Code Playgroud)
现在我想计算每个头寸的加权平均值,同时考虑到"<="当前头寸的所有头寸。结果应该是:
dt[, Vwa := c(50, 47.5, 45)]
dt
Position Price Volume Vwa
1: 1 50 10 50.0
2: 2 45 10 47.5
3: 3 40 10 45.0
Run Code Online (Sandbox Code Playgroud)
知道如何有效地实现这一目标吗?