R:如何拟合时间序列模型,例如"Y(t)=αX+βY(t-1)"? [英] R: How to fit a time series model such as "Y(t) = αX + βY(t-1)"?
问题描述
如何逐步将此模型拟合到R中?我的范围是对t + 1进行预测.
How do I fit this model in R, step by step? My scope is to make a forecast for t+1.
- Y(t)<-1900年至2000年.
- X<-分数从0到100.
- Y(t-1)<-Y的1阶滞后值.
谢谢.
推荐答案
您的模型是带有协变量 x
的 y
的AR(1)时间序列.我们可以只使用R base中的 arima0
(无缺失值)或 arima
(允许缺少值):
Your model is an AR(1) time series for y
with covariate x
. We can just use arima0
(no missing value) or arima
(missing value allowed) from R base:
fit <- arima0(y, order = c(1, 0, 0), xreg = x)
让我们考虑一个小例子:
Let's consider a small example:
set.seed(0)
x <- runif(100)
## intercept: 0.1
## slope of `x`: 1.2
## AR(1) with coefficient 0.5
y <- 0.1 + 1.2 * x + arima.sim(list(ar = 0.5), n = 100, sd = 0.2)
fit <- arima0(y, order = c(1, 0, 0), xreg = x)
#Call:
#arima0(x = y, order = c(1, 0, 0), xreg = x)
#
#Coefficients:
# ar1 intercept xreg
# 0.4639 0.0645 1.2139
#s.e. 0.0879 0.0448 0.0590
#
#sigma^2 estimated as 0.03046: log likelihood = 32.55, aic = -57.11
请注意,估算值与我们的真实模型相符.
Note the estimate is consistent with our true model.
谢谢.我如何插入更多协变量(x1,x2等),以防万一?
Thanks. How do I insert more covariates (x1,x2,etc.), just in case?
看看?arima0
(或?arima
):
xreg: Optionally, a vector or matrix of external regressors, which
must have the same number of rows as ‘x’.
您可以通过 xreg
指定模型矩阵.假设在数据框 dat
中有回归变量 x1
, x2
, x3
,则可以生成此模型矩阵通过:
You can specify a model matrix via xreg
. Suppose you have regressors x1
, x2
, x3
, in a data frame dat
, you can generate this model matrix via:
X <- model.matrix(~ x1 + x2 + x3, dat)
然后
fit <- arima0(y, order = c(1, 0, 0), xreg = X)
这篇关于R:如何拟合时间序列模型,例如"Y(t)=αX+βY(t-1)"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!