[英]How to get Stata to produce a dynamic forecast when using lagged outcome as a regressor?
I am currently dealing witha very small data set (20 observations, I know it's terrible).我目前正在处理一个非常小的数据集(20 个观察,我知道这很糟糕)。 But I need to somehow forecast out the values.
但我需要以某种方式预测出这些值。 When I simply regress time on the dependent variable I am able to get a prediction, but when I add lagged or differenced variables it does not predict more than one year into the future.
当我简单地对因变量回归时间时,我能够得到一个预测,但是当我添加滞后或差异变量时,它不会预测未来一年以上。 Is this due to having too few observations?
这是因为观察太少了吗?
Here is my code for context.这是我的上下文代码。 The two lines have have commented out result in a better fitting prediction for present data, but generate only one future prediction.
这两行已经注释掉了对当前数据进行更好拟合预测的结果,但仅生成了一个未来预测。
use "scrappage.dta", clear
drop if year == 1993
tsappend, add(12)
tsset year, y
reg scrappagerate year
*reg scrappagerate year l.scrappagerate l2.scrappagerate
*reg scrappagerate year d.scrappagerate d2.scrappagerate
predict p
predict yp if year>year(2013)
tsline yp p scrappagerate
Sorry if this is a stupid question, this is my first time using Stata to predict values.对不起,如果这是一个愚蠢的问题,这是我第一次使用 Stata 来预测值。
Take a look here for a solution and explanation.看看这里的解决方案和解释。 Essentially, you can use
arima
to estimate a model without AR or MA components (which should be equivalent to OLS with reg
) and create the dynamic/recursive forecast:本质上,您可以使用
arima
来估计没有 AR 或 MA 组件的模型(这应该等同于带有reg
OLS)并创建动态/递归预测:
arima y L(1/2).y, hessian
predict y_dynhat, dyn(tm(2011m2))
Just replace 2011m2 with whatever the actual last monthly date where you observe y.只需将 2011m2 替换为您观察到 y 的实际最后一个月的日期。 The hessian option will force the standard errors to match OLS more closely.
hessian 选项将强制标准错误更紧密地匹配 OLS。
You might consider posting your data on the stats site to see if folks have better modeling advice that OLS.您可能会考虑在统计网站上发布您的数据,看看人们是否有比 OLS 更好的建模建议。
The reason you're obtaining only one prediction has nothing to do with the predict function, but the nature of your data.您只获得一个预测的原因与 predict 函数无关,而是与数据的性质有关。 Let's say you have
N
observations.假设您有
N
观察值。 In your case, you used tsappend, add(12)
, making it so you have N+12
observations.在您的情况下,您使用了
tsappend, add(12)
,因此您有N+12
观察值。 And your l1.y
lagged variable will carry down to the N+1
th row.并且您的
l1.y
滞后变量将向下传递到第N+1
行。
Stata's predict
function will predict on all non-missing data, where there are available predictors. Stata 的
predict
函数将预测所有非缺失数据,其中有可用的预测器。 Therefore, since your independent variable, l1.y
is populated in the N + 1
row, Stata will predict that observation.因此,由于您的自变量
l1.y
填充在N + 1
行中,Stata 将预测该观察结果。 (Similarly, predict
won't predict the 1st observation, since the your lagged predictor will be missing.) (同样,
predict
不会预测第一个观察值,因为您的滞后预测变量将丢失。)
In order to get dynamic prediction using OLS regression in Stata, you need to feed this N+1
th prediction into an X matrix and use the regression coefficient matrix to predict the N+2
observation.为了在 Stata 中使用 OLS 回归进行动态预测,您需要将第
N+1
次预测输入 X 矩阵并使用回归系数矩阵来预测N+2
观测值。 You then iterate.然后你迭代。
* Example of how to do dynamic prediction using OLS regression and lagged variables
clear
set obs 12
gen time = _n
gen y = rnormal(100,100)
tsset time
tsappend, add(12)
gen y_lag1 = l1.y
* Establish the regression relationship and save the coefficients
regress y y_lag1
matrix a = r(table)'
matrix beta = a[1..2,1]
* Predict the N+1 value (notice you have y_lag1 in the 13th row)
predict yhat
* Predict the next values
local lag = 1
forval i = 14/24 {
local last_y = yhat[`i'-`lag']
matrix xinput = [`last_y',1]
* Estimate the next sales
matrix next_y = xinput*beta
replace yhat = next_y[1,1] in `i'
}
Comparing this to using the ARIMA model (as per Dimitriy V. Masterov's comment), and you get nearly identical results.将此与使用 ARIMA 模型(根据 Dimitriy V. Masterov 的评论)进行比较,您会得到几乎相同的结果。
arima y l1.y
predict yhat_ar, dyn(13)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.