Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Looping with the tin() function


From   Steve Samuels <sjsamuels@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping with the tin() function
Date   Sat, 30 Jun 2012 21:48:14 -0400

Welcome to Statalist! I know nothing about time series forecasting,
so I'll leave it to others to  comment on your logic, which I don't
quite follow. 
As tin() apparently does not accept scalar or macro arguments, I suggest 
inrange() which does.
****************************
clear
input qdate
0
1
2
3
4
5
6
7
8
9
10
end
tsset qdate, quarterly
forvalues t = `=tq(1960q3)'/`=tq(1961q2)' {
di `t' " "  %tq `t'
count if inrange(qdate,`t',`t'+1)
}
****************************


Steve


On Jun 29, 2012, at 5:08 PM, Autria Mazda wrote:

Hi again Statalisters!
 
I'm a first-timer and this is a re-post of a previously poorly explained problem I'm having, my apologies.
(I'm trying to replicate Stock and Watson 2007, "Why Has Inflation Become Harder to Forecast" in case anyone is familiar with the paper).
 
 
Data: I have a quarterly time series of gdp deflator data from 1960-2010.

Objective: Ultimately, I'm trying to run a pseudo-out-of-sample forecast.
 
What I'm currently doing:
1) Obtain the optimal lag length (based on AIC) over an initial sub-sample (e.g. 1960q1-1970q1) using varsoc.
2) Once I obtain the optimal lag length, I feed that into an AR(n) model. (Technically, n can change every quarter based on the varsoc results.)
3) I am able to do the above steps successfully but not efficiently with the code below. (Right now I'm just deleting the observations that are 
outside the sub-sample period before I run varsoc.)
 
Problem:
I need to run the regression over the sub-sample (1960-1970q1) use those parameters and model specification to forecast for the next quarter (1970q2). 
Then I need to store the forecast in a variable (e.g. f_cast_gdp_def) as the observation for 1970q2.
Next I need to repeat steps 1 and 2 above and re-run the regression (this time over the sample period 1960q1-1970q2).
Then forecast for 1970q3 and save that as observation 2 in variable f_cast_gdp_def, and so on and so forth for every quarter until the end of the sample.
 
I looked into trying the rolling reg command but from my understanding of the documentation I don't think I can keep changing the model specification at each iteration.
I think if I can figure out how to have tin() accept a variable, scalar or my loop var (t), then I can get what I need. My code is below.
 
I've tried many syntax variants and either I get an error message: invalid syntax or stata just ignores the tin() function altogether and uses the entire sample.
 
 
Any tips or advice would be greatly appreciated. Thanks again!!!
 
Autria Christensen
autria@yahoo.com
 
 
use "C:\Stata Files\Thesis\Data\SW_Econ_Activity_Vars_Tab1-3.dta", clear
forvalues t = `=tq(1970q1)'/`=tq(2010q4)' {
     preserve
    
     *set date range for optimal lag length selection
     drop if date < tq(1960q1)
     drop if date > `t'
     set more off
     varsoc gdpc96_yoy, maxlag(20)

     *Select optimal lag length and save as new scalar AIC_LAG
     matrix D = r(stats)
     svmat D, name(col)
     egen min_aic = min(AIC)
     gen opt_lag_aic = lag if min_aic == AIC
     egen store_a = mean(opt_lag_aic)
     scalar aic_lag = store_a
     disp "OPTIMAL LAG (BASED ON AIC) = " aic_lag
 
     *Run regression and store/output parameter estimates
     eststo one: arima gdpc96_yoy L(0/`=aic_lag').gdpc96_yoy, robust
     esttab one using "C:\Stata Files\Thesis\Data\Stats\test_`=end_date'.csv", cells("b se z p ci") stats(N ll chi2) nomtitle nonumber replace wide plain 
 
     *Now I need to forecast for the period t+1 using parameter estimates and lags from period t
     gen fcast_date = `t' + 1
     scalar f_date = fcast_date
 
     *I've tried using fcast_date and f_date but neither work the "predict" code below which doesn't really work
     predict static_yhat_AR if tin(`t', `t')
     predict dynamic_yhat_AR if tin(1970q2,2010q2), dyn(tq(1970q1))
 
     est clear
     restore
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index