R-具有一个滞后项的简单dyn预测模型 [英] R - predicting simple dyn model with one lag term

查看:71
本文介绍了R-具有一个滞后项的简单dyn预测模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的dyn库预测简单的滞后时间序列回归.这个问题是一个有用的起点,但是我得到了一些奇怪的行为,希望有人能解释一下.

I'm trying to predict a simple lagged time series regression with the dyn library in R. This question was a helpful starting point, but I'm getting some weird behaviour that I'm hoping someone can explain.

这是一个最低限度的工作示例.

Here's a minimum working example.

library(dyn)

# Initial data
y.orig <- arima.sim(model=list(ar=c(.9)),n=10)
x1.orig <- rnorm(10)
data <- cbind(y=y.orig, x1=x1.orig)

# This model, with a single lag term, predicts from t=2
mod1 <- dyn$lm(y ~ lag(y, -1), data)
y.new <- window(y.orig, end=end(y.orig) + c(5,0), extend=TRUE)
newdata1 <- cbind(y=y.new)
predict(mod1, newdata1)

# This one, with a lag plus another predictor, predicts from t=1 on
mod2 <- dyn$lm(y ~ lag(y, -1) + x1, data)
y.new <- window(y.orig, end=end(y.orig) + c(5,0), extend=TRUE)
x1.new <- c(x1.orig, rnorm(5))
newdata2 <- cbind(y=y.new, x1=x1.new)
predict(mod2, newdata2)

为什么两者之间有区别?有人可以建议如何使用dyn预测我的"mod1"吗?预先感谢.

Why is there the difference between the two? Can anyone suggest how to predict my ''mod1'' using dyn? Thanks in advance.

推荐答案

mod1 mod2 都开始在 t = 2 进行预测. mod2 的预测向量始于 t = 1 ,但其 NA .关于为什么一个从2开始,另一个从1开始的问题,请注意 predict 将公式右侧的变量合并在一起,对于 mod1 ,我们看到 lag(y,-1)从t = 2开始,因为 y 从t = 1开始.另一方面,对于 mod2 ,当我们合并 lag(y,-1) x1 时,我们得到一个从t开始的序列= 1(因为 x1 从t = 1开始).试试这个不涉及dyn的方法:

Both mod1 and mod2 start predicting at t=2. The prediction vector for mod2 starts at t=1 but its NA. Regarding why one starts at 2 and the other at 1 note that predict merges together the variables on the right hand side of the formula and in the case of mod1 we see that lag(y, -1) starts at t=2 since y starts at t=1. On the other hand in the case of mod2 when we merge lag(y, -1) and x1 we get a series that starts at t=1 (since x1 starts at t=1). Try this which does not involve dyn:

> start(with(as.list(newdata1), merge.zoo(lag(y, -1))))
[1] 2
> start(with(as.list(newdata2), merge.zoo(lag(y, -1), x1)))
[1] 1

如果我们希望 predict(mod1,newdata1)从t = 1开始,我们可以添加自己的Intercept列并删除默认的拦截器以避免重复.这将迫使它从1开始,因为现在RHS的序列从1开始:

If we wanted predict(mod1, newdata1) to start at t=1 we could add our own Intercept column and remove the default intercept to avoid duplication. That would force it to start at 1 since now the RHS has a series which starts at 1:

data.b <- cbind(y=y.orig, x1=x1.orig, Intercept = 1)
mod.b <- dyn$lm(y ~ Intercept + lag(y, -1) - 1, data.b)

newdata.b <- cbind(Intercept = 1, y = y.new)
predict(mod.b, newdata.b)

关于第二个问题,如果要预测 mod1 ,请使用 fitted(mod1).

Regarding the second question, if you want to predict mod1 then use fitted(mod1) .

似乎还存在第三个问题,那就是它基本上是如何工作的,因此也许可以澄清这一点.dyn所做的全部工作就是在公式中对齐时间序列,然后可以像往常一样运行 lm predict .例如,如果我们使用 dyn $ model.frame 创建对齐的模型框架,则仅使用普通的 lm 和普通的 predict 即可完成其他所有操作从那时起,就不再涉及 dyn 了.问题下方的 mod1a mod1 相似,不同之处在于它在对齐的模型框架上运行普通的 lm .如果您了解 mod1a lm 和其 predict ,则 mod1 predict 相似

It seems there is lurking some third question about how it basically all works so maybe this clarifies it. All dyn does is to align the time series in the formula and then lm and predict can be run as usual. For example, if we create an aligned model frame using dyn$model.frame then everything else can be done using just ordinary lm and ordinary predict and dyn is not involved from that point onwards. Below mod1a is similar to mod1 from the question except it runs an ordinary lm on the aligned model frame. If you understand the mod1a lm and its predict then mod1 and predict are similar.

## mod1 and mod1a are similar

# from code in the question
mod1 <- dyn$lm(y ~ lag(y, -1), data = data)
mod1

# redo it using a plain lm by applying dyn to model.frame
mf <- dyn$model.frame(y ~ lag(y, -1), data = data)
mod1a <- lm(y ~ `lag(y, -1)`, mf)
mod1a

## the two predicts below are similar

# the 1 ensures its an mts rather than ts but is otherwise not used
newdata1 <- cbind(y=y.new, 1) 
predict(mod1, newdata1)

newdata1a <- cbind(1, `lag(y, -1)` = lag(y.new, -1))
predict(mod1a, newdata1a)

这篇关于R-具有一个滞后项的简单dyn预测模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆