简体   繁体   English

使用滞后结果作为回归量时,如何让 Stata 产生动态预测?

[英]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 更好的建模建议。

Here's your problem:这是你的问题:

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不会预测第一个观察值,因为您的滞后预测变量将丢失。)

Here's your solution:这是您的解决方案:

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.

相关问题 如何使用 fable::VAR 对滞后的外部回归变量进行预测 - How to forecast with lagged external regressors using fable::VAR 使用带有外部回归器和并行处理的forecast.gts(包hts) - Using forecast.gts (package hts) with external regressor and parallel processing Stata时间序列滚动预测 - Stata timeseries rolling forecast 在 pyhton 中使用 ARIMA 并进行大量集成时,如何使预测不被集成? - When using a ARIMA in pyhton, and have a garde of integration how do you get the forecast to be not integrated? FBProphet:了解回归量对多元预测的影响 - FBProphet: Understanding Regressor Impact on Multivariate Forecast 在R中生成滞后变量时使用循环避免循环(并使用动物园) - avoiding for loops when generating lagged variables in R (and using zoo) Auto.Arima 变换时间序列和 xreg 相关性与滞后预测时间序列 - Auto.Arima transform timeseries and xreg correlation with lagged forecast timeseries 在可用数据很少的情况下如何预测收入? - How to forecast revenue when there is little data available? 为什么在使用Prais-Winsten估计时在SAS和Stata中获得不同的回归输出? - Why do I get different regression outputs in SAS and in Stata when using Prais-Winsten estimation? 如何使用泡菜保存 fbprophet 预测 model 以便 model 在加载时接受用户输入 - How to save a fbprophet forecast model using pickle such that the model takes user input when loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM